From 95a2e587ec475f019a7fd695fdf95f9a523bd53c Mon Sep 17 00:00:00 2001 From: KenTandrian Date: Fri, 3 Jul 2026 21:49:36 +0700 Subject: [PATCH] feat(api): configure webpack plugin --- apps/api/project.json | 27 +--------------------- apps/api/webpack.config.js | 46 ++++++++++++++++++++++++++++++++++---- nx.json | 10 +++++++++ 3 files changed, 53 insertions(+), 30 deletions(-) diff --git a/apps/api/project.json b/apps/api/project.json index 4e1affb13..d6c3171e1 100644 --- a/apps/api/project.json +++ b/apps/api/project.json @@ -7,32 +7,7 @@ "generators": {}, "targets": { "build": { - "executor": "@nx/webpack:webpack", - "options": { - "compiler": "tsc", - "deleteOutputPath": false, - "main": "apps/api/src/main.ts", - "outputPath": "dist/apps/api", - "sourceMap": true, - "target": "node", - "tsConfig": "apps/api/tsconfig.app.json", - "webpackConfig": "apps/api/webpack.config.js" - }, - "configurations": { - "production": { - "generatePackageJson": true, - "optimization": true, - "extractLicenses": true, - "inspect": false, - "fileReplacements": [ - { - "replace": "apps/api/src/environments/environment.ts", - "with": "apps/api/src/environments/environment.prod.ts" - } - ] - } - }, - "outputs": ["{options.outputPath}"] + "outputs": ["{workspaceRoot}/dist/apps/api"] }, "copy-assets": { "executor": "nx:run-commands", diff --git a/apps/api/webpack.config.js b/apps/api/webpack.config.js index 2cc38b985..29594c426 100644 --- a/apps/api/webpack.config.js +++ b/apps/api/webpack.config.js @@ -1,6 +1,44 @@ -const { composePlugins, withNx } = require('@nx/webpack'); +const { NxAppWebpackPlugin } = require('@nx/webpack/app-plugin'); -module.exports = composePlugins(withNx(), (config, { options, context }) => { - // Customize webpack config here - return config; +// These options were migrated by @nx/webpack:convert-to-inferred from +// the project.json file and merged with the options in this file +const configValues = { + build: { + default: { + compiler: 'tsc', + deleteOutputPath: false, + main: './src/main.ts', + outputPath: 'dist/apps/api', + sourceMap: true, + target: 'node', + tsConfig: './tsconfig.app.json' + }, + production: { + generatePackageJson: true, + optimization: true, + extractLicenses: true, + inspect: false, + fileReplacements: [ + { + replace: './src/environments/environment.ts', + with: './src/environments/environment.prod.ts' + } + ] + } + } +}; + +// Determine the correct configValue to use based on the configuration +const configuration = process.env.NX_TASK_TARGET_CONFIGURATION || 'default'; + +const buildOptions = { + ...configValues.build.default, + ...configValues.build[configuration] +}; + +/** + * @type {import('webpack').WebpackOptionsNormalized} + */ +module.exports = async () => ({ + plugins: [new NxAppWebpackPlugin(buildOptions)] }); diff --git a/nx.json b/nx.json index 6b90f437c..1deb022df 100644 --- a/nx.json +++ b/nx.json @@ -67,6 +67,16 @@ } }, "$schema": "./node_modules/nx/schemas/nx-schema.json", + "plugins": [ + { + "plugin": "@nx/webpack/plugin", + "options": { + "buildTargetName": "build", + "serveTargetName": "serve", + "previewTargetName": "preview" + } + } + ], "targetDefaults": { "build": { "dependsOn": ["^build"],