typescript - Nestjs använder inte webpack-konfigurationsfilen när man kör ett build-kommando
Jag har en Node js-kod som jag försöker bygga tillsammans med nestjs och webpack. Jag försöker injicera globala variabler med webpack.DefinePlugin, men i byggresultatet kan jag se att variablerna inte blir ersatta.
nest-cli.json fil:
{
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"webpack": true,
"webpackConfigPath": "./webpack.config.ts"
}
}
Fil webpack.config.ts:
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;
Jag har package.json-skript för att köra i utvecklingsläge och produktionsläge:
.
.
.
"build": "nest build",
"build:dev": "cross-env NODE_ENV=development nest build",
.
.
.
Jag använder de konfigurerade variablerna i min källkod:
private config: IConfig = {
API_URL: __CLI_API_URL__,
DASHBOARD_URL: __DASHBOARD_URL__,
};
Dessa variabler ersätts dock inte med webpack. När jag inspekterar min byggkod kan jag se att dessa variabler fortfarande finns i min kod.
Jag förväntar mig att se dem ersatta. Det verkar som att Nestjs inte ens använder min fil
Efter att ha bytt fil från webpack.config.ts för att använda samma logik men med js: webpack.config.js fungerade det. Kommer Nestjs inte att stödja TS-konfigurationsfil?
Problemet var. NestJS ignorerar Webpack-filer skrivna i TypeScript.
Svar #1
Jag löste problemet genom att byta ut webpack-konfigurationsfilen mot en JavaScript-fil, eftersom NestJS inte stöder konfigurationsfiler skrivna i TypeScript.