Skip to content

Commit d7cd153

Browse files
authoredMar 6, 2023
fix(bundling): default Node scriptType to CommonJS since it has the widest compatibility (#15483)
1 parent b74fc1b commit d7cd153

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed
 

‎e2e/webpack/src/webpack.test.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,25 @@ describe('Webpack Plugin', () => {
2222
runCLI(
2323
`generate @nrwl/webpack:webpack-project ${myPkg} --target=node --tsConfig=libs/${myPkg}/tsconfig.lib.json --main=libs/${myPkg}/src/index.ts`
2424
);
25+
26+
// Test `scriptType` later during during.
27+
updateFile(
28+
`libs/${myPkg}/webpack.config.js`,
29+
`
30+
const { composePlugins, withNx } = require('@nrwl/webpack');
31+
32+
module.exports = composePlugins(withNx(), (config) => {
33+
console.log('scriptType is ' + config.output.scriptType);
34+
return config;
35+
});
36+
`
37+
);
38+
2539
rmDist();
26-
runCLI(`build ${myPkg}`);
40+
41+
const buildOutput = runCLI(`build ${myPkg}`);
42+
// Ensure scriptType is not set if we're in Node (it only applies to Web).
43+
expect(buildOutput).toContain('scriptType is undefined');
2744
let output = runCommand(`node dist/libs/${myPkg}/main.js`);
2845
expect(output).toMatch(/Hello/);
2946
expect(output).not.toMatch(/Conflicting/);

‎packages/webpack/src/generators/webpack-project/webpack-project.ts

+30
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ function addBuildTarget(tree: Tree, options: WebpackProjectGeneratorSchema) {
5555
main: options.main ?? joinPathFragments(project.root, 'src/main.ts'),
5656
tsConfig:
5757
options.tsConfig ?? joinPathFragments(project.root, 'tsconfig.app.json'),
58+
webpackConfig: joinPathFragments(project.root, 'webpack.config.js'),
5859
};
5960

6061
if (options.webpackConfig) {
@@ -67,6 +68,35 @@ function addBuildTarget(tree: Tree, options: WebpackProjectGeneratorSchema) {
6768
buildOptions.babelUpwardRootMode = true;
6869
}
6970

71+
if (options.target === 'node') {
72+
tree.write(
73+
joinPathFragments(project.root, 'webpack.config.js'),
74+
`
75+
const { composePlugins, withNx } = require('@nrwl/webpack');
76+
77+
// Nx plugins for webpack.
78+
module.exports = composePlugins(withNx(), (config) => {
79+
// Update the webpack config as needed here.
80+
// e.g. \`config.plugins.push(new MyPlugin())\`
81+
return config;
82+
});
83+
`
84+
);
85+
} else {
86+
tree.write(
87+
joinPathFragments(project.root, 'webpack.config.js'),
88+
`
89+
const { composePlugins, withNx, withWeb } = require('@nrwl/webpack');
90+
91+
// Nx plugins for webpack.
92+
module.exports = composePlugins(withNx(), withWeb(), (config) => {
93+
// Update the webpack config as needed here.
94+
// e.g. \`config.plugins.push(new MyPlugin())\`
95+
return config;
96+
});
97+
`
98+
);
99+
}
70100
updateProjectConfiguration(tree, options.project, {
71101
...project,
72102
targets: {

‎packages/webpack/src/utils/with-nx.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ export function withNx(pluginOptions?: WithNxOptions): NxWebpackPlugin {
167167
hashFunction: 'xxhash64',
168168
// Disabled for performance
169169
pathinfo: false,
170-
scriptType: 'module' as const,
170+
// Use CJS for Node since it has the widest support.
171+
scriptType: options.target === 'node' ? undefined : ('module' as const),
171172
},
172173
watch: options.watch,
173174
watchOptions: {

0 commit comments

Comments
 (0)
Please sign in to comment.