Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix double-loading packages on npm #132

Merged
merged 3 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 2.4.7
g
* Fix a bug where npm packages could crash on Node.js if loaded both through
`require()` and `import`.

## 2.4.6

* Properly mark NPM packages as `"type": "module"` when `pkg.jsEsmExports` is
Expand Down
27 changes: 26 additions & 1 deletion lib/src/npm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,11 @@ void _writePlatformWrapper(String path, JSRequireSet requires,
{bool node = false}) {
var exports = jsEsmExports.value;
if (exports != null) {
_writeImportWrapper('$path.${node ? 'mjs' : 'js'}', requires, exports);
if (node) {
_writeNodeImportWrapper('$path.mjs', exports);
} else {
_writeImportWrapper('$path.${node ? 'mjs' : 'js'}', requires, exports);
}
_writeRequireWrapper('$path.${node ? 'js' : 'cjs'}', requires);
} else {
_writeRequireWrapper('$path.js', requires);
Expand Down Expand Up @@ -613,6 +617,27 @@ String _loadRequires(JSRequireSet requires) {
return buffer.toString();
}


/// Writes a wrapper to [path] that loads and re-exports `$_npmName.node.js`
/// using ESM imports.
///
/// Rather than having a totally separate ESM wrapper, for Node we load ESM
/// exports *through* the require wrapper. This ensures that we don't run into
/// issues like sass/dart-sass#2017 if both are loaded in the same Node process.
///
/// [exports] is the value of [jsEsmExports].
void _writeNodeImportWrapper(
String path, Set<String> exports) {
var cjsUrl = './' + p.setExtension(p.basename(path), '.js');
var buffer = StringBuffer("import cjs from ${json.encode(cjsUrl)};\n\n");

for (var export in exports) {
buffer.writeln("export const $export = cjs.$export;");
}

writeString(path, buffer.toString());
}

/// Writes a wrapper to [path] that loads and re-exports `$_npmName.dart.js`
/// using ESM imports with [requires] injected.
///
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: cli_pkg
version: 2.4.6
version: 2.4.7
description: Grinder tasks for releasing Dart CLI packages.
homepage: https://github.com/google/dart_cli_pkg

Expand Down
11 changes: 11 additions & 0 deletions test/npm_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,11 @@ void main() {
const myApp = require("my_app");

console.log(myApp.hello);
"""),
// Regression test for sass/dart-sass#2017
d.file("both.mjs", """
import "./test.mjs";
import "./test.cjs";
""")
]).create();

Expand All @@ -717,6 +722,12 @@ void main() {
await TestProcess.start("node$dotExe", [d.path("depender/test.cjs")]);
expect(cjsProcess.stdout, emits("true"));
await cjsProcess.shouldExit(0);

var bothProcess =
await TestProcess.start("node$dotExe", [d.path("depender/both.mjs")]);
expect(bothProcess.stdout, emits("true"));
expect(bothProcess.stdout, emits("true"));
await bothProcess.shouldExit(0);
});

test("overwrite existing string value", () async {
Expand Down