Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(bundling): default Node scriptType to CommonJS since it has the widest compatibility #15483

Merged
merged 1 commit into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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