Skip to content

Commit

Permalink
fix(bundling): default Node scriptType to CommonJS since it has the w…
Browse files Browse the repository at this point in the history
…idest compatibility (#15483)

(cherry picked from commit d7cd153)
  • Loading branch information
jaysoo authored and FrozenPandaz committed Mar 8, 2023
1 parent 6c34f40 commit d4a0554
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
19 changes: 18 additions & 1 deletion e2e/webpack/src/webpack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,25 @@ describe('Webpack Plugin', () => {
runCLI(
`generate @nrwl/webpack:webpack-project ${myPkg} --target=node --tsConfig=libs/${myPkg}/tsconfig.lib.json --main=libs/${myPkg}/src/index.ts`
);

// Test `scriptType` later during during.
updateFile(
`libs/${myPkg}/webpack.config.js`,
`
const { composePlugins, withNx } = require('@nrwl/webpack');
module.exports = composePlugins(withNx(), (config) => {
console.log('scriptType is ' + config.output.scriptType);
return config;
});
`
);

rmDist();
runCLI(`build ${myPkg}`);

const buildOutput = runCLI(`build ${myPkg}`);
// Ensure scriptType is not set if we're in Node (it only applies to Web).
expect(buildOutput).toContain('scriptType is undefined');
let output = runCommand(`node dist/libs/${myPkg}/main.js`);
expect(output).toMatch(/Hello/);
expect(output).not.toMatch(/Conflicting/);
Expand Down
30 changes: 30 additions & 0 deletions packages/webpack/src/generators/webpack-project/webpack-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ function addBuildTarget(tree: Tree, options: WebpackProjectGeneratorSchema) {
main: options.main ?? joinPathFragments(project.root, 'src/main.ts'),
tsConfig:
options.tsConfig ?? joinPathFragments(project.root, 'tsconfig.app.json'),
webpackConfig: joinPathFragments(project.root, 'webpack.config.js'),
};

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

if (options.target === 'node') {
tree.write(
joinPathFragments(project.root, 'webpack.config.js'),
`
const { composePlugins, withNx } = require('@nrwl/webpack');
// Nx plugins for webpack.
module.exports = composePlugins(withNx(), (config) => {
// Update the webpack config as needed here.
// e.g. \`config.plugins.push(new MyPlugin())\`
return config;
});
`
);
} else {
tree.write(
joinPathFragments(project.root, 'webpack.config.js'),
`
const { composePlugins, withNx, withWeb } = require('@nrwl/webpack');
// Nx plugins for webpack.
module.exports = composePlugins(withNx(), withWeb(), (config) => {
// Update the webpack config as needed here.
// e.g. \`config.plugins.push(new MyPlugin())\`
return config;
});
`
);
}
updateProjectConfiguration(tree, options.project, {
...project,
targets: {
Expand Down
3 changes: 2 additions & 1 deletion packages/webpack/src/utils/with-nx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ export function withNx(pluginOptions?: WithNxOptions): NxWebpackPlugin {
hashFunction: 'xxhash64',
// Disabled for performance
pathinfo: false,
scriptType: 'module' as const,
// Use CJS for Node since it has the widest support.
scriptType: options.target === 'node' ? undefined : ('module' as const),
},
watch: options.watch,
watchOptions: {
Expand Down

0 comments on commit d4a0554

Please sign in to comment.