Merge pull request #727 from sass/static-require

Require dependencies in the preamble rather than through JS-interop
This commit is contained in:
Natalie Weizenbaum 2019-06-28 14:32:18 -07:00 committed by GitHub
commit 532358d7fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 10 deletions

View File

@ -27,6 +27,11 @@
* Add a top-level `warn()` function for custom functions and importers to print
warning messages.
### JavaScript API
* Avoid re-assigning the `require()` function to make the code statically
analyzable by Webpack.
## 1.20.3
* No user-visible changes.

View File

@ -87,10 +87,8 @@ class Stderr {
void flush() {}
}
@JS("require")
external _FS _require(String name);
final _fs = _require("fs");
@JS("fs")
external _FS get _fs;
@JS("process")
external _Process get _process;

View File

@ -4,9 +4,6 @@
import 'package:js/js.dart';
@JS("require")
external Chokidar _require(String name);
@JS()
class Chokidar {
external ChokidarWatcher watch(String path, ChokidarOptions options);
@ -30,4 +27,5 @@ class ChokidarWatcher {
/// The Chokidar module.
///
/// See [the docs on npm](https://www.npmjs.com/package/chokidar).
final chokidar = _require("chokidar");
@JS("chokidar")
external Chokidar get chokidar;

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,7 +56,16 @@ void _js({@required bool release}) {
text.replaceFirst(RegExp(r"\n*//# sourceMappingURL=[^\n]+\n*$"), "\n");
}
destination.writeAsStringSync(preamble.getPreamble() + text);
// Reassigning require() makes Webpack complain.
var preambleText =
preamble.getPreamble().replaceFirst("self.require = require;\n", "");
destination.writeAsStringSync("""
$preambleText
self.fs = require("fs");
self.chokidar = require("chokidar");
self.readline = require("readline");
$text""");
}
@Task('Build a pure-JS dev-mode npm package.')