Skip to content

Commit

Permalink
fix: support multiple namespace imports
Browse files Browse the repository at this point in the history
  • Loading branch information
j4k0xb committed Jan 13, 2024
1 parent 34c5938 commit 1f137d6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
10 changes: 10 additions & 0 deletions packages/webcrack/src/unpack/test/exports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,16 @@ describe('webpack 4', () => {
export * as lib from "lib";
`));

test('multiple re-export all as named', () =>
expectJS(`
__webpack_require__.d(__webpack_exports__, "lib", function() { return lib; });
__webpack_require__.d(__webpack_exports__, "lib2", function() { return lib; });
var lib = __webpack_require__("lib");
`).toMatchInlineSnapshot(`
export * as lib from "lib";
export * as lib2 from "lib";
`));

test('re-export all as default', () =>
expectJS(`
__webpack_require__.d(__webpack_exports__, "default", function() { return lib; });
Expand Down
27 changes: 15 additions & 12 deletions packages/webcrack/src/unpack/webpack/import-export-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ interface RequireVar {
defaultImport?: t.ImportDefaultSpecifier;
namespaceImport?: t.ImportNamespaceSpecifier;
namedImports: t.ImportSpecifier[];
namespaceExport?: t.ExportNamespaceSpecifier;
namespaceExports: t.ExportNamespaceSpecifier[];
namedExports: t.ExportSpecifier[];
}

Expand Down Expand Up @@ -66,16 +66,18 @@ export class ImportExportManager {
requireVar.namedExports,
t.stringLiteral(requireVar.moduleId),
);
// TODO: resolve module id to path
const namespaceExports = requireVar.namespaceExports.map((specifier) =>
t.exportNamedDeclaration(
undefined,
[specifier],
t.stringLiteral(requireVar.moduleId),
),
);
if (namedExports.specifiers.length > 0) {
requireVar.binding.path.parentPath!.insertAfter(namedExports);
}
if (requireVar.namespaceExport) {
// TODO: resolve module id to path
const namespaceExports = t.exportNamedDeclaration(
undefined,
[requireVar.namespaceExport],
t.stringLiteral(requireVar.moduleId),
);
if (namespaceExports.length > 0) {
requireVar.binding.path.parentPath!.insertAfter(namespaceExports);
}
});
Expand Down Expand Up @@ -108,7 +110,8 @@ export class ImportExportManager {
!!requireVar.namespaceImport ||
requireVar.namedImports.length > 0;
const hasExports =
!!requireVar.namespaceExport || requireVar.namedExports.length > 0;
requireVar.namespaceExports.length > 0 ||
requireVar.namedExports.length > 0;

// side-effect import
if (!requireVar.binding.referenced && !hasImports && !hasExports) {
Expand Down Expand Up @@ -369,8 +372,8 @@ export class ImportExportManager {
* ```
*/
private addExportNamespace(requireVar: RequireVar, exportName: string) {
requireVar.namespaceExport ??= t.exportNamespaceSpecifier(
t.identifier(exportName),
requireVar.namespaceExports.push(
t.exportNamespaceSpecifier(t.identifier(exportName)),
);
}

Expand Down Expand Up @@ -405,7 +408,7 @@ export class ImportExportManager {
defaultImport: undefined,
namespaceImport: undefined,
namedImports: [],
namespaceExport: undefined,
namespaceExports: [],
namedExports: [],
});
}
Expand Down

0 comments on commit 1f137d6

Please sign in to comment.