Adding support for DDC modules when running Flutter Web in debug mode (#141423)
### Context: DDC modules are abstractions over how libraries are loaded/updated. The entirety of google3 uses the DDC/legacy module system due to its flexibility extensibility over the other two (ES6 and AMD/RequireJS). Unifying DDC's module system saves us from duplicating work and will allow us to have finer grained control over how JS modules are loaded. This is a a prerequisite to features such as hot reload. ### Overview: This change plumbs a boolean flag through flutter_tools that switches between DDC (new) and AMD (current) modules. This mode is automatically applied when `--extra-front-end-options=--dartdevc-module-format=ddc` is specified alongside `flutter run`. Other important additions include: * Splitting Flutter artifacts between DDC and AMD modules * Adding unit tests for the DDC module system * Additional bootstrapper logic for the DDC module system We don't expect to see any user-visible behavior or performance differences. This is dependent on [incoming module system support in DWDS](https://github.com/dart-lang/webdev/pull/2295) and [additional artifacts in the engine](https://github.com/flutter/engine/pull/47783). This is part of a greater effort to deprecate the AMD module system: https://github.com/dart-lang/sdk/issues/52361
This commit is contained in:
@@ -4,6 +4,136 @@
|
||||
|
||||
import 'package:package_config/package_config.dart';
|
||||
|
||||
String generateDDCBootstrapScript({
|
||||
required String entrypoint,
|
||||
required String ddcModuleLoaderUrl,
|
||||
required String mapperUrl,
|
||||
required bool generateLoadingIndicator,
|
||||
String appRootDirectory = '/',
|
||||
}) {
|
||||
return '''
|
||||
${generateLoadingIndicator ? _generateLoadingIndicator() : ""}
|
||||
// TODO(markzipan): This is safe if Flutter app roots are always equal to the
|
||||
// host root '/'. Validate if this is true.
|
||||
var _currentDirectory = "$appRootDirectory";
|
||||
|
||||
window.\$dartCreateScript = (function() {
|
||||
// Find the nonce value. (Note, this is only computed once.)
|
||||
var scripts = Array.from(document.getElementsByTagName("script"));
|
||||
var nonce;
|
||||
scripts.some(
|
||||
script => (nonce = script.nonce || script.getAttribute("nonce")));
|
||||
// If present, return a closure that automatically appends the nonce.
|
||||
if (nonce) {
|
||||
return function() {
|
||||
var script = document.createElement("script");
|
||||
script.nonce = nonce;
|
||||
return script;
|
||||
};
|
||||
} else {
|
||||
return function() {
|
||||
return document.createElement("script");
|
||||
};
|
||||
}
|
||||
})();
|
||||
|
||||
// Loads a module [relativeUrl] relative to [root].
|
||||
//
|
||||
// If not specified, [root] defaults to the directory serving the main app.
|
||||
var forceLoadModule = function (relativeUrl, root) {
|
||||
var actualRoot = root ?? _currentDirectory;
|
||||
return new Promise(function(resolve, reject) {
|
||||
var script = self.\$dartCreateScript();
|
||||
let policy = {
|
||||
createScriptURL: function(src) {return src;}
|
||||
};
|
||||
if (self.trustedTypes && self.trustedTypes.createPolicy) {
|
||||
policy = self.trustedTypes.createPolicy('dartDdcModuleUrl', policy);
|
||||
}
|
||||
script.onload = resolve;
|
||||
script.onerror = reject;
|
||||
script.src = policy.createScriptURL(actualRoot + relativeUrl);
|
||||
document.head.appendChild(script);
|
||||
});
|
||||
};
|
||||
|
||||
// A map containing the URLs for the bootstrap scripts in debug.
|
||||
let _scriptUrls = {
|
||||
"mapper": "$mapperUrl",
|
||||
"moduleLoader": "$ddcModuleLoaderUrl"
|
||||
};
|
||||
|
||||
(function() {
|
||||
let appName = "$entrypoint";
|
||||
|
||||
// A uuid that identifies a subapp.
|
||||
// Stubbed out since subapps aren't supported in Flutter.
|
||||
let uuid = "00000000-0000-0000-0000-000000000000";
|
||||
|
||||
window.postMessage(
|
||||
{type: "DDC_STATE_CHANGE", state: "initial_load", targetUuid: uuid}, "*");
|
||||
|
||||
// Load pre-requisite DDC scripts.
|
||||
// We intentionally use invalid names to avoid namespace clashes.
|
||||
let prerequisiteScripts = [
|
||||
{
|
||||
"src": "$ddcModuleLoaderUrl",
|
||||
"id": "ddc_module_loader \x00"
|
||||
},
|
||||
{
|
||||
"src": "$mapperUrl",
|
||||
"id": "dart_stack_trace_mapper \x00"
|
||||
}
|
||||
];
|
||||
|
||||
// Load ddc_module_loader.js to access DDC's module loader API.
|
||||
let prerequisiteLoads = [];
|
||||
for (let i = 0; i < prerequisiteScripts.length; i++) {
|
||||
prerequisiteLoads.push(forceLoadModule(prerequisiteScripts[i].src));
|
||||
}
|
||||
Promise.all(prerequisiteLoads).then((_) => afterPrerequisiteLogic());
|
||||
|
||||
// Save the current script so we can access it in a closure.
|
||||
var _currentScript = document.currentScript;
|
||||
|
||||
var afterPrerequisiteLogic = function() {
|
||||
window.\$dartLoader.rootDirectories.push(_currentDirectory);
|
||||
let scripts = [
|
||||
{
|
||||
"src": "dart_sdk.js",
|
||||
"id": "dart_sdk"
|
||||
},
|
||||
{
|
||||
"src": "main_module.bootstrap.js",
|
||||
"id": "data-main"
|
||||
}
|
||||
];
|
||||
let loadConfig = new window.\$dartLoader.LoadConfiguration();
|
||||
loadConfig.bootstrapScript = scripts[scripts.length - 1];
|
||||
|
||||
loadConfig.loadScriptFn = function(loader) {
|
||||
loader.addScriptsToQueue(scripts, null);
|
||||
loader.loadEnqueuedModules();
|
||||
}
|
||||
loadConfig.ddcEventForLoadStart = /* LOAD_ALL_MODULES_START */ 1;
|
||||
loadConfig.ddcEventForLoadedOk = /* LOAD_ALL_MODULES_END_OK */ 2;
|
||||
loadConfig.ddcEventForLoadedError = /* LOAD_ALL_MODULES_END_ERROR */ 3;
|
||||
|
||||
let loader = new window.\$dartLoader.DDCLoader(loadConfig);
|
||||
|
||||
// Record prerequisite scripts' fully resolved URLs.
|
||||
prerequisiteScripts.forEach(script => loader.registerScript(script));
|
||||
|
||||
// Note: these variables should only be used in non-multi-app scenarios since
|
||||
// they can be arbitrarily overridden based on multi-app load order.
|
||||
window.\$dartLoader.loadConfig = loadConfig;
|
||||
window.\$dartLoader.loader = loader;
|
||||
loader.nextAttempt();
|
||||
}
|
||||
})();
|
||||
''';
|
||||
}
|
||||
|
||||
/// The JavaScript bootstrap script to support in-browser hot restart.
|
||||
///
|
||||
/// The [requireUrl] loads our cached RequireJS script file. The [mapperUrl]
|
||||
@@ -157,6 +287,39 @@ document.addEventListener('dart-app-ready', function (e) {
|
||||
''';
|
||||
}
|
||||
|
||||
String generateDDCMainModule({
|
||||
required String entrypoint,
|
||||
required bool nullAssertions,
|
||||
required bool nativeNullAssertions,
|
||||
String? exportedMain,
|
||||
}) {
|
||||
final String entrypointMainName = exportedMain ?? entrypoint.split('.')[0];
|
||||
// The typo below in "EXTENTION" is load-bearing, package:build depends on it.
|
||||
return '''
|
||||
/* ENTRYPOINT_EXTENTION_MARKER */
|
||||
|
||||
(function() {
|
||||
// Flutter Web uses a generated main entrypoint, which shares app and module names.
|
||||
let appName = "$entrypoint";
|
||||
let moduleName = "$entrypoint";
|
||||
|
||||
// Use a dummy UUID since multi-apps are not supported on Flutter Web.
|
||||
let uuid = "00000000-0000-0000-0000-000000000000";
|
||||
|
||||
let child = {};
|
||||
child.main = function() {
|
||||
let dart = self.dart_library.import('dart_sdk', appName).dart;
|
||||
dart.nonNullAsserts($nullAssertions);
|
||||
dart.nativeNonNullAsserts($nativeNullAssertions);
|
||||
self.dart_library.start(appName, uuid, moduleName, "$entrypointMainName");
|
||||
}
|
||||
|
||||
/* MAIN_EXTENSION_MARKER */
|
||||
child.main();
|
||||
})();
|
||||
''';
|
||||
}
|
||||
|
||||
/// Generate a synthetic main module which captures the application's main
|
||||
/// method.
|
||||
///
|
||||
|
||||
@@ -219,35 +219,69 @@ enum WebRendererMode implements CliEnum {
|
||||
}
|
||||
}
|
||||
|
||||
/// The correct precompiled artifact to use for each build and render mode.
|
||||
const Map<WebRendererMode, Map<NullSafetyMode, HostArtifact>> kDartSdkJsArtifactMap = <WebRendererMode, Map<NullSafetyMode, HostArtifact>>{
|
||||
/// The correct precompiled artifact to use for each build and render mode for DDC with AMD modules.
|
||||
// TODO(markzipan): delete this when DDC's AMD module system is deprecated, https://github.com/flutter/flutter/issues/142060.
|
||||
const Map<WebRendererMode, Map<NullSafetyMode, HostArtifact>> kAmdDartSdkJsArtifactMap = <WebRendererMode, Map<NullSafetyMode, HostArtifact>>{
|
||||
WebRendererMode.auto: <NullSafetyMode, HostArtifact> {
|
||||
NullSafetyMode.sound: HostArtifact.webPrecompiledCanvaskitAndHtmlSoundSdk,
|
||||
NullSafetyMode.unsound: HostArtifact.webPrecompiledCanvaskitAndHtmlSdk,
|
||||
NullSafetyMode.sound: HostArtifact.webPrecompiledAmdCanvaskitAndHtmlSoundSdk,
|
||||
NullSafetyMode.unsound: HostArtifact.webPrecompiledAmdCanvaskitAndHtmlSdk,
|
||||
},
|
||||
WebRendererMode.canvaskit: <NullSafetyMode, HostArtifact> {
|
||||
NullSafetyMode.sound: HostArtifact.webPrecompiledCanvaskitSoundSdk,
|
||||
NullSafetyMode.unsound: HostArtifact.webPrecompiledCanvaskitSdk,
|
||||
NullSafetyMode.sound: HostArtifact.webPrecompiledAmdCanvaskitSoundSdk,
|
||||
NullSafetyMode.unsound: HostArtifact.webPrecompiledAmdCanvaskitSdk,
|
||||
},
|
||||
WebRendererMode.html: <NullSafetyMode, HostArtifact> {
|
||||
NullSafetyMode.sound: HostArtifact.webPrecompiledSoundSdk,
|
||||
NullSafetyMode.unsound: HostArtifact.webPrecompiledSdk,
|
||||
NullSafetyMode.sound: HostArtifact.webPrecompiledAmdSoundSdk,
|
||||
NullSafetyMode.unsound: HostArtifact.webPrecompiledAmdSdk,
|
||||
},
|
||||
};
|
||||
|
||||
/// The correct source map artifact to use for each build and render mode.
|
||||
const Map<WebRendererMode, Map<NullSafetyMode, HostArtifact>> kDartSdkJsMapArtifactMap = <WebRendererMode, Map<NullSafetyMode, HostArtifact>>{
|
||||
/// The correct source map artifact to use for each build and render mode for DDC with AMD modules.
|
||||
// TODO(markzipan): delete this when DDC's AMD module system is deprecated, https://github.com/flutter/flutter/issues/142060.
|
||||
const Map<WebRendererMode, Map<NullSafetyMode, HostArtifact>> kAmdDartSdkJsMapArtifactMap = <WebRendererMode, Map<NullSafetyMode, HostArtifact>>{
|
||||
WebRendererMode.auto: <NullSafetyMode, HostArtifact> {
|
||||
NullSafetyMode.sound: HostArtifact.webPrecompiledCanvaskitAndHtmlSoundSdkSourcemaps,
|
||||
NullSafetyMode.unsound: HostArtifact.webPrecompiledCanvaskitAndHtmlSdkSourcemaps,
|
||||
NullSafetyMode.sound: HostArtifact.webPrecompiledAmdCanvaskitAndHtmlSoundSdkSourcemaps,
|
||||
NullSafetyMode.unsound: HostArtifact.webPrecompiledAmdCanvaskitAndHtmlSdkSourcemaps,
|
||||
},
|
||||
WebRendererMode.canvaskit: <NullSafetyMode, HostArtifact> {
|
||||
NullSafetyMode.sound: HostArtifact.webPrecompiledCanvaskitSoundSdkSourcemaps,
|
||||
NullSafetyMode.unsound: HostArtifact.webPrecompiledCanvaskitSdkSourcemaps,
|
||||
NullSafetyMode.sound: HostArtifact.webPrecompiledAmdCanvaskitSoundSdkSourcemaps,
|
||||
NullSafetyMode.unsound: HostArtifact.webPrecompiledAmdCanvaskitSdkSourcemaps,
|
||||
},
|
||||
WebRendererMode.html: <NullSafetyMode, HostArtifact> {
|
||||
NullSafetyMode.sound: HostArtifact.webPrecompiledSoundSdkSourcemaps,
|
||||
NullSafetyMode.unsound: HostArtifact.webPrecompiledSdkSourcemaps,
|
||||
NullSafetyMode.sound: HostArtifact.webPrecompiledAmdSoundSdkSourcemaps,
|
||||
NullSafetyMode.unsound: HostArtifact.webPrecompiledAmdSdkSourcemaps,
|
||||
},
|
||||
};
|
||||
|
||||
/// The correct precompiled artifact to use for each build and render mode for DDC with DDC modules.
|
||||
const Map<WebRendererMode, Map<NullSafetyMode, HostArtifact>> kDdcDartSdkJsArtifactMap = <WebRendererMode, Map<NullSafetyMode, HostArtifact>>{
|
||||
WebRendererMode.auto: <NullSafetyMode, HostArtifact> {
|
||||
NullSafetyMode.sound: HostArtifact.webPrecompiledDdcCanvaskitAndHtmlSoundSdk,
|
||||
NullSafetyMode.unsound: HostArtifact.webPrecompiledDdcCanvaskitAndHtmlSdk,
|
||||
},
|
||||
WebRendererMode.canvaskit: <NullSafetyMode, HostArtifact> {
|
||||
NullSafetyMode.sound: HostArtifact.webPrecompiledDdcCanvaskitSoundSdk,
|
||||
NullSafetyMode.unsound: HostArtifact.webPrecompiledDdcCanvaskitSdk,
|
||||
},
|
||||
WebRendererMode.html: <NullSafetyMode, HostArtifact> {
|
||||
NullSafetyMode.sound: HostArtifact.webPrecompiledDdcSoundSdk,
|
||||
NullSafetyMode.unsound: HostArtifact.webPrecompiledDdcSdk,
|
||||
},
|
||||
};
|
||||
|
||||
/// The correct source map artifact to use for each build and render mode for DDC with DDC modules.
|
||||
const Map<WebRendererMode, Map<NullSafetyMode, HostArtifact>> kDdcDartSdkJsMapArtifactMap = <WebRendererMode, Map<NullSafetyMode, HostArtifact>>{
|
||||
WebRendererMode.auto: <NullSafetyMode, HostArtifact> {
|
||||
NullSafetyMode.sound: HostArtifact.webPrecompiledDdcCanvaskitAndHtmlSoundSdkSourcemaps,
|
||||
NullSafetyMode.unsound: HostArtifact.webPrecompiledDdcCanvaskitAndHtmlSdkSourcemaps,
|
||||
},
|
||||
WebRendererMode.canvaskit: <NullSafetyMode, HostArtifact> {
|
||||
NullSafetyMode.sound: HostArtifact.webPrecompiledDdcCanvaskitSoundSdkSourcemaps,
|
||||
NullSafetyMode.unsound: HostArtifact.webPrecompiledDdcCanvaskitSdkSourcemaps,
|
||||
},
|
||||
WebRendererMode.html: <NullSafetyMode, HostArtifact> {
|
||||
NullSafetyMode.sound: HostArtifact.webPrecompiledDdcSoundSdkSourcemaps,
|
||||
NullSafetyMode.unsound: HostArtifact.webPrecompiledDdcSdkSourcemaps,
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user