From 1f137d6a308ef1fa88c1f20ea587ad9500c88df1 Mon Sep 17 00:00:00 2001 From: j4k0xb <55899582+j4k0xb@users.noreply.github.com> Date: Sat, 13 Jan 2024 03:38:30 +0100 Subject: [PATCH] fix: support multiple namespace imports --- .../webcrack/src/unpack/test/exports.test.ts | 10 +++++++ .../unpack/webpack/import-export-manager.ts | 27 ++++++++++--------- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/packages/webcrack/src/unpack/test/exports.test.ts b/packages/webcrack/src/unpack/test/exports.test.ts index ca40c96f..6cec855c 100644 --- a/packages/webcrack/src/unpack/test/exports.test.ts +++ b/packages/webcrack/src/unpack/test/exports.test.ts @@ -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; }); diff --git a/packages/webcrack/src/unpack/webpack/import-export-manager.ts b/packages/webcrack/src/unpack/webpack/import-export-manager.ts index f4c25bcc..8e8de41a 100644 --- a/packages/webcrack/src/unpack/webpack/import-export-manager.ts +++ b/packages/webcrack/src/unpack/webpack/import-export-manager.ts @@ -25,7 +25,7 @@ interface RequireVar { defaultImport?: t.ImportDefaultSpecifier; namespaceImport?: t.ImportNamespaceSpecifier; namedImports: t.ImportSpecifier[]; - namespaceExport?: t.ExportNamespaceSpecifier; + namespaceExports: t.ExportNamespaceSpecifier[]; namedExports: t.ExportSpecifier[]; } @@ -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); } }); @@ -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) { @@ -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)), ); } @@ -405,7 +408,7 @@ export class ImportExportManager { defaultImport: undefined, namespaceImport: undefined, namedImports: [], - namespaceExport: undefined, + namespaceExports: [], namedExports: [], }); }