I have a Node js code which I try to build together with nestjs and webpack.
I try to inject global variables using webpack.DefinePlugin, but in the build result I can see the variables not being replaces.
nest-cli.json file:
{
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"webpack": true,
"webpackConfigPath": "./webpack.config.ts"
}
}
webpack.config.ts file:
import webpack from 'webpack';
import WebpackShellPluginNext from 'webpack-shell-plugin-next';
import isCI from 'is-ci';
const isDev = process.env.NODE_ENV === 'development' && !isCI;
const cliApiDomain = isDev ? 'localhost' : 'xxx.xxx';
const cliApiUrl = isDev ? 'http://localhost:5000' : 'https://www.xxx.xxx';
const dashboardUrl = isDev ? 'http://localhost:8080' : 'https://xxx.xxxx';
const configuration = (options: webpack.Configuration): webpack.Configuration => ({
...options,
plugins: [
...options.plugins!,
new WebpackShellPluginNext({
onBuildStart: {
scripts: ['rimraf dist'],
blocking: true,
},
safe: true,
}),
new webpack.DefinePlugin({
__CLI_API_DOMAIN__: `'${cliApiDomain}'`,
__CLI_API_URL__: `'${cliApiUrl}'`,
__DASHBOARD_URL__: `'${dashboardUrl}'`,
}),
],
});
export default configuration;
I have package.json scripts for running in dev mode and production mode:
.
.
.
"build": "nest build",
"build:dev": "cross-env NODE_ENV=development nest build",
.
.
.
I use the configured variables in my source code:
private config: IConfig = {
API_URL: __CLI_API_URL__,
DASHBOARD_URL: __DASHBOARD_URL__,
};
However, these variables are not replaced with webpack. When I inspect my build code, I can see those variables are still in my code.
I expect to see them replaced. It seems like Nestjs does not even use my file
After switching file of webpack.config.ts to use same logic but with js: webpack.config.js it worked. Nestjs won't support TS config file?