Replace dynamic require()s in dependencies with static alternatives

This commit is contained in:
Natalie Weizenbaum 2019-06-20 19:05:23 +01:00
parent 494e208067
commit 31f3ca2ead

View File

@ -42,7 +42,13 @@ void _js({@required bool release}) {
// * We thoroughly test edge cases in user input.
if (release) ...["-O4", "--fast-startup"]
]);
var text = destination.readAsStringSync();
var text = destination
.readAsStringSync()
// Some dependencies dynamically invoke `require()`, which makes Webpack
// complain. We replace those with direct references to the modules, which
// we load explicitly after the preamble.
.replaceAllMapped(RegExp(r'self\.require\("([a-zA-Z0-9_-]+)"\)'),
(match) => "self.${match[1]}");
if (release) {
// We don't ship the source map, so remove the source map comment.
@ -50,10 +56,15 @@ void _js({@required bool release}) {
text.replaceFirst(RegExp(r"\n*//# sourceMappingURL=[^\n]+\n*$"), "\n");
}
// Reassigning require() makes Webpack complain.
var preambleText =
preamble.getPreamble().replaceFirst("self.require = require;\n", "");
destination.writeAsStringSync("""
${preamble.getPreamble()}
$preambleText
self.fs = require("fs");
self.chokidar = require("chokidar");
self.readline = require("readline");
$text""");
}