From 4bce612ff68d555ee062fc0a3a15cdb0a02404a7 Mon Sep 17 00:00:00 2001 From: targeral Date: Fri, 8 Dec 2023 15:19:40 +0800 Subject: [PATCH] fix(module-tools): set isModule is true in swc transform (#5053) --- .changeset/olive-keys-destroy.md | 6 ++++++ packages/solutions/module-tools/src/builder/feature/swc.ts | 6 +++++- .../fixtures/build/autoExternal/autoExternal.test.ts | 7 +++++++ .../module/fixtures/build/autoExternal/config.ts | 7 +++++++ .../module/fixtures/build/autoExternal/src/umd.ts | 3 +++ 5 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 .changeset/olive-keys-destroy.md create mode 100644 tests/integration/module/fixtures/build/autoExternal/src/umd.ts diff --git a/.changeset/olive-keys-destroy.md b/.changeset/olive-keys-destroy.md new file mode 100644 index 000000000000..82f81efbf85d --- /dev/null +++ b/.changeset/olive-keys-destroy.md @@ -0,0 +1,6 @@ +--- +'@modern-js/module-tools': patch +--- + +fix(module-tools): Modify the configuration of isModule when converting umd products. +fix(module-tools): 修改转换 umd 产物过程中 isModule 的配置 diff --git a/packages/solutions/module-tools/src/builder/feature/swc.ts b/packages/solutions/module-tools/src/builder/feature/swc.ts index 9926c9e834ce..91795b4ab083 100644 --- a/packages/solutions/module-tools/src/builder/feature/swc.ts +++ b/packages/solutions/module-tools/src/builder/feature/swc.ts @@ -166,7 +166,11 @@ export const swcRenderChunk = { type: 'umd', } : undefined, - isModule: 'unknown', + // If `chunk.contents` is recognized as [Script](https://swc.rs/docs/configuration/compilation#ismodule), + // then it will not be converted to umd format content. + // so we need to set `isModule` to `true`. + // eg: when `autoExternal` is false, then chunk.contents will be recognized as [Script] + isModule: format === 'umd' ? true : 'unknown', }); const result = await swcCompiler.transform( name, diff --git a/tests/integration/module/fixtures/build/autoExternal/autoExternal.test.ts b/tests/integration/module/fixtures/build/autoExternal/autoExternal.test.ts index 351869fc2f72..7dae0c6fe9e0 100644 --- a/tests/integration/module/fixtures/build/autoExternal/autoExternal.test.ts +++ b/tests/integration/module/fixtures/build/autoExternal/autoExternal.test.ts @@ -50,4 +50,11 @@ describe('autoExternal usage', () => { expect(content.includes(`require("postcss")`)).toBeTruthy(); expect(content.includes(`require("path-browserify")`)).toBeFalsy(); }); + + it('autoExternal is false, and format is umd', async () => { + const distFilePath = path.join(fixtureDir, './dist/6/umd.js'); + const content = await fs.readFile(distFilePath, 'utf-8'); + expect(await fs.pathExists(distFilePath)).toBeTruthy(); + expect(content.includes(`function(global, factory)`)).toBeTruthy(); + }); }); diff --git a/tests/integration/module/fixtures/build/autoExternal/config.ts b/tests/integration/module/fixtures/build/autoExternal/config.ts index b9604db5a30a..e63b97d02d4f 100644 --- a/tests/integration/module/fixtures/build/autoExternal/config.ts +++ b/tests/integration/module/fixtures/build/autoExternal/config.ts @@ -36,5 +36,12 @@ export default defineConfig({ }, outDir: './dist/5', }, + { + buildType: 'bundle', + input: ['./src/umd.ts'], + format: 'umd', + autoExternal: false, + outDir: './dist/6', + }, ], }); diff --git a/tests/integration/module/fixtures/build/autoExternal/src/umd.ts b/tests/integration/module/fixtures/build/autoExternal/src/umd.ts new file mode 100644 index 000000000000..bcf442ec1b13 --- /dev/null +++ b/tests/integration/module/fixtures/build/autoExternal/src/umd.ts @@ -0,0 +1,3 @@ +import react from 'react'; + +console.info(react);