Open a88zach opened 2 months ago
It is also worth noting that you currently cannot specify the importType for the nodeExternals package that is used to exclude 3rd party packages. To get around this, I had to fallback to composing the webpack configuration so I could set the externals after nx modified the config
// ./tools/createWebpackConfig.cjs
const { join } = require('path');
const { composePlugins, withNx } = require('@nx/webpack');
const {
NxTsconfigPathsWebpackPlugin,
} = require('@nx/webpack/tsconfig-paths-plugin');
const nodeExternals = require('webpack-node-externals');
module.exports = ({
packageRoot,
outputPath,
additionalEntryPoints,
assets,
}) => {
return composePlugins(
() => ({
output: {
path: join(packageRoot, outputPath),
library: {
type: 'module',
},
},
target: 'node20',
externalsPresets: {
node: true,
},
experiments: {
outputModule: true,
},
plugins: [
new NxTsconfigPathsWebpackPlugin({
tsConfig: join(packageRoot, 'tsconfig.app.json'),
}),
],
}),
withNx({
compiler: 'tsc',
main: join(packageRoot, 'src/main.ts'),
optimization: false,
outputHashing: false,
tsConfig: join(packageRoot, 'tsconfig.app.json'),
outputFileName: 'main.js',
generatePackageJson: true,
additionalEntryPoints,
assets,
}),
(config) => {
const externals = [];
externals.push(
nodeExternals({
modulesDir: join(__dirname, '../node_modules'),
importType: 'node-commonjs',
}),
);
config.externals = externals;
return config;
},
);
};
// webpack.config.cjs
const createWebpackConfig = require('../../../tools/createWebpackConfig.cjs');
module.exports = createWebpackConfig({
packageRoot: __dirname,
outputPath: '../../dist/apps/app-name',
});
Current Behavior
When targeting a specific version version of node in a webpack config and setting
externalDependencies: 'all'
, the external dependencies are still bundled in the outputExpected Behavior
External dependencies should not be in the output bundle
GitHub Repo
No response
Steps to Reproduce
const { join } = require('path');
const { NxAppWebpackPlugin } = require('@nx/webpack/app-plugin');
module.exports = { output: { path: join( __dirname, '../../dist/apps/app-name', ), target: 'node20', // <- HERE library: { type: 'commonjs', }, }, plugins: [ new NxAppWebpackPlugin({ externalDependencies: 'all', compiler: 'tsc', main: './src/main.ts', optimization: false, outputHashing: false, tsConfig: './tsconfig.app.json', generatePackageJson: true, }), ], };
Failure Logs
No response
Package Manager Version
No response
Operating System
Additional Information
The issue is here: https://github.com/nrwl/nx/blob/master/packages/webpack/src/plugins/nx-webpack-plugin/lib/apply-base-config.ts#L337
Webpack allows you to target a specific version of node: https://webpack.js.org/configuration/target/