Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Refactor flutter.js to do dart2wasm bootstrapping and CanvasKit/Skwasm preloading. #49037

Merged
merged 34 commits into from
Jan 10, 2024

Conversation

eyebrowsoffire
Copy link
Contributor

@eyebrowsoffire eyebrowsoffire commented Dec 14, 2023

This PR makes some major revisions to our flutter.js bootstrapper.

  • Modularize flutter.js into multiple files to make it a little simpler to manage from source code. They are still bundled into a single .js file by esbuild.
  • Added a types.d.ts file which contains declarations of the types of some of the objects used in the flutter.js API
  • Deprecated the old FlutterLoader.loadEntrypoint API and added a new function simply called FlutterLoader.load, which has a few more capabilities:
    • A build tool can inject a build config, that may describe multiple builds that FlutterLoader.load can attempt to use. It will use the first one that is compatible with the browser environment and the user's configuration.
    • It can also load wasm flutter apps.
    • It also pre-loads and instantiates CanvasKit (and Skwasm) as necessary depending on the build configuration.
    • FlutterLoader.load also immediately takes a flutter configuration object. If an onEntrypointLoaded callback is not provided by the user, it just does the expected thing and initializes the engine and immediately starts the app, passing the configuration along as needed.
  • flutter.js has the engine hash built into it now, which allows it to ascertain the correct CDN URLs for both CanvasKit and Skwasm.

@github-actions github-actions bot added the platform-web Code specifically for the web engine label Dec 14, 2023
@eyebrowsoffire eyebrowsoffire marked this pull request as ready for review December 21, 2023 17:54
@eyebrowsoffire eyebrowsoffire changed the title [WIP] Refactor flutter.js to do dart2wasm bootstrapping and CanvasKit/Skwasm preloading. Refactor flutter.js to do dart2wasm bootstrapping and CanvasKit/Skwasm preloading. Dec 21, 2023
"// Use of this source code is governed by a BSD-style license that can be",
"// found in the LICENSE file.",
"",
"export const engineRevision = \"$engine_version\";",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This scares me for a couple of reasons:

  • The output of web build rules depends on every byte in the engine codebase. For example, changing a README file will result in altering the git revision and invalidating the build.
  • Git revision does not fully describe the engine revision. You can have local source changes while the git revision is the same.

This probably invalidates one of the core principles of a build system, but I'm failing to name one, maybe hermeticity.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this is an interesting one, I get your concerns. Could we maybe have a GVC and chat about the subtleties here at some point if you have a moment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We had some discussion in a GVC. I think I mostly agree that it would be better to leave the engine revision out of flutter.js and instead have the tool inject the engine hash as part of the build config.

Copy link
Member

@ditman ditman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly nitpicks, I think only createWasmInstantiator needs a little bit more love.

@eyebrowsoffire eyebrowsoffire requested a review from mdebbar January 3, 2024 22:00
Copy link
Member

@ditman ditman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, this is a fantastic improvement! (All my comments are minor/questions, feel free to disregard!)

"src/service_worker_loader.js",
"src/skwasm_loader.js",
"src/trusted_types.js",

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

Remove extra line?

baseUrl = `${baseUrl}/chromium/`;
}
let canvasKitUrl = `${baseUrl}canvaskit.js`;
if (deps.flutterTT.policy) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QQ: how do we decide what goes in deps vs what is passed as a parameter to the function?

For example, the browserEnvironment looks like something that could be a dep too, and not need its own specific positional parameter in the loadCanvasKit function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it's mostly arbitrary. The browser environment just logically felt like something different than a "dependency" so I have it as a separate thing. Perhaps I don't even need the browserEnvironment to be passed as a parameter anymore now that it's just a global variable vended by the browser_environment.js module...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now that it's just a global variable vended by the browser_environment.js module

I thought the same, but also if you grab it from deps (or the parameter) it's easy(er) to mock for testing maybe?

jsSupportRuntimePath: string;
}

export type ApplicationBuild = JSApplicationBuild | WasmApplicationBuild;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need this union? Wouldn't it be equivalent that interface ApplicationBuildBase is called interface ApplicationBuild?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A union like this is super useful in TS. An interface basically has open polymorphism, but a union has closed polymorphism. This basically means that if you're using the union (ApplicationBuild), you can actually differentiate based on the compileTarget field, and the compiler will actually refine the type in the the subsequent branches. This won't happen when using the interface (and in fact, the compileTarget field doesn't exist on the interface, so you couldn't check for it). Example:

const build: ApplicationBuild = //something;
// build is an ApplicationBuild type
if (build.compileTarget === "dart2wasm") {
  // build is actually a WasmApplicationBuild in this branch, so I can access fields specific to WasmApplicationBuild
  console.log(build.mainWasmPath);
} else {
  // the compiler can even tell that the only remaining possible values are of type JSApplicationBuild
  console.log(build.mainJsPath);
}

@eyebrowsoffire eyebrowsoffire added the autosubmit Merge PR when tree becomes green via auto submit App label Jan 10, 2024
@auto-submit auto-submit bot merged commit 5b9d213 into flutter:main Jan 10, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Jan 10, 2024
auto-submit bot pushed a commit to flutter/flutter that referenced this pull request Jan 10, 2024
…141228)

flutter/engine@941f268...5b9d213

2024-01-10 [email protected] Refactor flutter.js to do dart2wasm bootstrapping and CanvasKit/Skwasm preloading. (flutter/engine#49037)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-engine-flutter-autoroll
Please CC [email protected],[email protected],[email protected] on the revert to ensure that a human
is aware of the problem.

To file a bug in Flutter: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
@bdero
Copy link
Member

bdero commented Jan 11, 2024

@eyebrowsoffire
Copy link
Contributor Author

@bdero Definitely expected. Thanks for the heads up though!

@rafaelbenedetti1
Copy link

I'm having a problem after update my flutter version to 3.22, I modify the index.html to new mode of app initialization. My application have some textfields, and after upgrade the number keyboard doesn't work, because the six and four keys moves the cursor to right and left and not show the number on field, how can i resolve this?

@eyebrowsoffire
Copy link
Contributor Author

@rafaelbenedetti1 Could you file a separate issue with a minimum reproducible case?

@rafaelbenedetti1
Copy link

@eyebrowsoffire where can i create?

@eyebrowsoffire
Copy link
Contributor Author

@rafaelbenedetti1
Copy link

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
autosubmit Merge PR when tree becomes green via auto submit App platform-web Code specifically for the web engine
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants