Skip to content

Commit

Permalink
fix(module-tools): set isModule is true in swc transform (#5053)
Browse files Browse the repository at this point in the history
  • Loading branch information
targeral authored Dec 8, 2023
1 parent ebc90f6 commit 4bce612
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changeset/olive-keys-destroy.md
Original file line number Diff line number Diff line change
@@ -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 的配置
6 changes: 5 additions & 1 deletion packages/solutions/module-tools/src/builder/feature/swc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,12 @@ export default defineConfig({
},
outDir: './dist/5',
},
{
buildType: 'bundle',
input: ['./src/umd.ts'],
format: 'umd',
autoExternal: false,
outDir: './dist/6',
},
],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import react from 'react';

console.info(react);

0 comments on commit 4bce612

Please sign in to comment.