Skip to content

Commit

Permalink
fix(turbopack-ecmascript): use direct namespace re-export (#7963)
Browse files Browse the repository at this point in the history
### Description

Also has a fix for async modules with `__turbopack_export_namespace__`.

Could still break if `__turbopack_export_namespace__` is called before
an `await`.


Closes PACK-2962
  • Loading branch information
ForsakenHarmony authored Apr 15, 2024
1 parent 95fdcaa commit 984a635
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,11 @@ function exportValue(module: Module, value: any) {
}

function exportNamespace(module: Module, namespace: any) {
module.exports = module.namespaceObject = namespace;
if (isAsyncModuleExt(module.exports)) {
module.exports[turbopackExports] = namespace;
} else {
module.exports = module.namespaceObject = namespace;
}
}

function createGetter(obj: Record<string | symbol, any>, key: string | symbol) {
Expand Down Expand Up @@ -443,7 +447,7 @@ function asyncModule(
if (err) {
reject((promise[turbopackError] = err));
} else {
resolve(exports);
resolve(promise[turbopackExports]);
}

resolveQueue(queue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl CachedExternalModule {
if self.external_type == CachedExternalType::CommonJs {
writeln!(code, "module.exports = mod;")?;
} else {
writeln!(code, "__turbopack_dynamic__(mod);")?;
writeln!(code, "__turbopack_export_namespace__(mod);")?;
}

Ok(EcmascriptModuleContent {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { a, b, default as def } from "esm-external/package";

it(`should reexport all exports from an external esm module`, async () => {
expect(def).toBe("default");

expect(a).toBe("a");
expect(b).toBe("b");
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

0 comments on commit 984a635

Please sign in to comment.