Skip to content

Commit

Permalink
fix(module-tools): alias not working on re-export (#4784)
Browse files Browse the repository at this point in the history
  • Loading branch information
10Derozan authored Oct 16, 2023
1 parent 4d9ee16 commit 94671a1
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .changeset/perfect-apples-look.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@modern-js/module-tools': patch
---

fix: alias not working on re-export
fix: 别名在重导出不生效
12 changes: 10 additions & 2 deletions packages/solutions/module-tools/src/builder/feature/redirect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"`,
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";`),
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/module/fixtures/build/redirect/src/alias.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const alias = true;

export const a = 42;
export const b = 42;
export default alias;
14 changes: 12 additions & 2 deletions tests/integration/module/fixtures/build/redirect/src/index.ts
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit 94671a1

Please sign in to comment.