Skip to content

Commit 34b42ac

Browse files
authored
[web] Hides that Flutter uses requireJS in debug. (#129032)
Flutter web uses requireJS in `debug` mode to assemble a DDC-compiled app from a bunch of small files ("modules"). This caused that `canvaskit.js` (and all other modules that used a browserify-like loading header) didn't work because they attempted to use the `define` function provided by Flutter's instance of `requireJS` (which kept the defined modules private, rather than as globals on the page, as the users of the JS expected). A [fix](flutter/engine#27342) was added to `flutter/engine` to trick loaders into *not* using the `requireJS` module loader, but a recent change in the fix's js-interop layer *subtly* changed its JS output on the page (objects went from `undefined` to `null`), causing this: * flutter/flutter#126131 (and others) This PR hides a bit of code that is commonly used by module loaders to decide that they may use the `define` function provided by requireJS (so the engine workaround can be removed). ## Next steps * flutter/engine#42941 ## Issues Partially addresses: flutter/flutter#126131 (and others) ## Tests * Added a unit test to ensure the `delete` stays * Manually tested with the Gallery app in `debug` mode with a bunch of user-supplied scripts that currently fail to load. * Also tested hot restart as suggested by @nshahan
1 parent fcbc53b commit 34b42ac

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

packages/flutter_tools/lib/src/web/bootstrap.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,12 @@ define("$bootstrapModule", ["$entrypoint", "dart_sdk"], function(app, dart_sdk)
196196
return dart.getSourceMap(url);
197197
});
198198
}
199+
// Prevent DDC's requireJS to interfere with modern bundling.
200+
if (typeof define === 'function' && define.amd) {
201+
// Preserve a copy just in case...
202+
define._amd = define.amd;
203+
delete define.amd;
204+
}
199205
});
200206
''';
201207
}

packages/flutter_tools/test/general.shard/web/bootstrap_test.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@ void main() {
6262
expect(result, matches(regex), reason: 'require.config must have a waitSeconds: 0 config entry');
6363
});
6464

65+
test('generateMainModule hides requireJS injected by DDC', () {
66+
final String result = generateMainModule(
67+
entrypoint: 'foo/bar/main.js',
68+
nullAssertions: false,
69+
nativeNullAssertions: false,
70+
);
71+
expect(result, contains('''define._amd = define.amd;'''),
72+
reason: 'define.amd must be preserved as _amd so users may restore it if needed.');
73+
expect(result, contains('''delete define.amd;'''),
74+
reason: "define.amd must be removed so packages don't attempt to use Dart's instance.");
75+
});
76+
6577
test('generateMainModule embeds urls correctly', () {
6678
final String result = generateMainModule(
6779
entrypoint: 'foo/bar/main.js',

0 commit comments

Comments
 (0)