I have set up an Nx monorepo for a Nest.js Standalone.
I noticed that when I changed my project.json
configurations to change Nx Commands and add modes (Dev, Prod) so I toggle optimization in webpack config, Nx didn't apply those changes.
Here is my new project.json
configuration:
{
"name": "name",
"$schema": "node_modules/nx/schemas/project-schema.json",
"sourceRoot": "src",
"projectType": "application",
"targets": {
"build": {
"executor": "@nx/webpack:webpack",
"outputs": [
"{options.outputPath}"
],
"defaultConfiguration": "development",
"options": {
"target": "node",
"outputPath": "dist/name",
"main": "src/main.ts",
"tsConfig": "tsconfig.app.json",
"assets": [
"src/assets"
],
"generatePackageJson": true,
"webpackConfig": "./webpack.config.js"
},
"configurations": {
"development": {
"inspection": true,
"env": {
"NODE_ENV": "development"
}
},
"production": {
"inspection": false,
"env": {
"NODE_ENV": "production"
},
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
]
}
}
},
"serve": {
// not necessary for this
},
},
"tags": []
}
for more context, here is my webpack.config.js
:
const { NxAppWebpackPlugin } = require('@nx/webpack/app-plugin');
const { join } = require('path');
const nodeExternals = require('webpack-node-externals');
const isProd = process.env.NODE_ENV === 'production';
console.log(isProd);
module.exports = {
target: 'node',
mode: isProd ? 'production' : 'development',
devtool: isProd ? 'source-map' : 'eval-source-map',
output: {
path: join(__dirname, './dist/el-marchi-api'),
filename: 'main.js',
clean: true,
},
optimization: {
minimize: isProd,
moduleIds: 'deterministic',
splitChunks: isProd ? {
chunks: 'all',
minSize: 20000,
minChunks: 1,
} : false,
},
externals: [nodeExternals()],
plugins: [
new NxAppWebpackPlugin({
target: 'node',
compiler: 'swc',
main: './src/main.ts',
tsConfig: './tsconfig.app.json',
assets: ['./src/assets'],
optimization: false,
outputHashing: 'none',
generatePackageJson: true,
sourceMap: true,
}),
],
experiments: {
topLevelAwait: true,
},
};
when running the command nx build
here is the output:
nx build
> nx run name:build:development
true
...
as you can see it shows true which means isProd was evaluated to true.
Note:
Here are the fixes that I tried:
nx clean
The problem still persists.
Any idea how to solve this problem or what's the source of it?