From 94671a12fe3170234f0b12d55554fdd28f6cfe55 Mon Sep 17 00:00:00 2001 From: 10Derozan Date: Mon, 16 Oct 2023 11:38:57 +0800 Subject: [PATCH] fix(module-tools): alias not working on re-export (#4784) --- .changeset/perfect-apples-look.md | 6 ++++++ .../module-tools/src/builder/feature/redirect.ts | 12 ++++++++++-- .../fixtures/build/redirect/redirect.test.ts | 2 +- .../module/fixtures/build/redirect/src/alias.ts | 2 ++ .../module/fixtures/build/redirect/src/index.ts | 14 ++++++++++++-- 5 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 .changeset/perfect-apples-look.md diff --git a/.changeset/perfect-apples-look.md b/.changeset/perfect-apples-look.md new file mode 100644 index 000000000000..2f4d2d95de55 --- /dev/null +++ b/.changeset/perfect-apples-look.md @@ -0,0 +1,6 @@ +--- +'@modern-js/module-tools': patch +--- + +fix: alias not working on re-export +fix: 别名在重导出不生效 diff --git a/packages/solutions/module-tools/src/builder/feature/redirect.ts b/packages/solutions/module-tools/src/builder/feature/redirect.ts index cd9ba033161d..fdff852f0f9e 100644 --- a/packages/solutions/module-tools/src/builder/feature/redirect.ts +++ b/packages/solutions/module-tools/src/builder/feature/redirect.ts @@ -204,9 +204,17 @@ export const redirect = { try { const sgNode = js.parse(code).root(); const funcPattern = [`require($MATCH)`, `import($MATCH)`]; + // `export $VAR from` is invalid, so we need `{$$$VAR}`, `*` and `* as $VAR` + // But `import $VAR from` is valid. const staticPattern = [ - `import $$VAR from '$MATCH'`, - `import $$VAR from "$MATCH"`, + `import $VAR from '$MATCH'`, + `import $VAR from "$MATCH"`, + `export {$$$VAR} from '$MATCH'`, + `export {$$$VAR} from "$MATCH"`, + `export * from '$MATCH'`, + `export * from "$MATCH"`, + `export * as $VAR from '$MATCH'`, + `export * as $VAR from "$MATCH"`, `import '$MATCH'`, `import "$MATCH"`, ]; diff --git a/tests/integration/module/fixtures/build/redirect/redirect.test.ts b/tests/integration/module/fixtures/build/redirect/redirect.test.ts index 0444342da4ae..90dfe5cb1cde 100644 --- a/tests/integration/module/fixtures/build/redirect/redirect.test.ts +++ b/tests/integration/module/fixtures/build/redirect/redirect.test.ts @@ -30,7 +30,7 @@ describe('redirect', () => { const distJsFilePath = path.join(fixtureDir, './dist/redirect/index.js'); const jsContent = await fs.readFile(distJsFilePath, 'utf-8'); // redirect alias - expect(jsContent.includes(`import alias from "./alias"`)).toBeTruthy(); + expect(jsContent.includes(`@/alias`)).toBeFalsy(); // redirect style expect( jsContent.includes(`import css from "./index.module";`), diff --git a/tests/integration/module/fixtures/build/redirect/src/alias.ts b/tests/integration/module/fixtures/build/redirect/src/alias.ts index 6924e06fb97b..bca0e4a22727 100644 --- a/tests/integration/module/fixtures/build/redirect/src/alias.ts +++ b/tests/integration/module/fixtures/build/redirect/src/alias.ts @@ -1,3 +1,5 @@ const alias = true; +export const a = 42; +export const b = 42; export default alias; diff --git a/tests/integration/module/fixtures/build/redirect/src/index.ts b/tests/integration/module/fixtures/build/redirect/src/index.ts index 8ddc0f5fd71f..749999588095 100644 --- a/tests/integration/module/fixtures/build/redirect/src/index.ts +++ b/tests/integration/module/fixtures/build/redirect/src/index.ts @@ -1,5 +1,15 @@ +// css module import css from './index.module.css'; + +// asset import svg from './logo.svg'; -import alias from '@/alias'; -console.log(css, svg, alias); +// alias +import namedImport, { a, b } from '@/alias'; +import * as wildcardImport from '@/alias'; + +export { a, b } from '@/alias'; +export * from '@/alias'; +export * as c from '@/alias'; + +console.log(css, svg, a, b, namedImport, wildcardImport);