diff --git a/src/generators/configuration/configuration.ts b/src/generators/configuration/configuration.ts index 74bff21604..08d775a151 100644 --- a/src/generators/configuration/configuration.ts +++ b/src/generators/configuration/configuration.ts @@ -86,7 +86,7 @@ function addBuildTarget(tree: Tree, options: ConfigurationSchema) { }, production: { mode: 'production', - optimization: true, + optimization: options.target === 'web' ? true : undefined, sourceMap: false, }, }, diff --git a/src/utils/with-nx.ts b/src/utils/with-nx.ts index 7ccaf777b5..7644eb59ca 100644 --- a/src/utils/with-nx.ts +++ b/src/utils/with-nx.ts @@ -1,4 +1,4 @@ -import { Configuration } from '@rspack/core'; +import { Configuration, ExternalItem } from '@rspack/core'; import * as path from 'path'; import { getCopyPatterns } from './get-copy-patterns'; import { SharedConfigContext } from './model'; @@ -11,12 +11,7 @@ export function withNx(_opts = {}) { ): Configuration { const isProd = process.env.NODE_ENV === 'production' || options.mode === 'production'; - const isDev = - process.env.NODE_ENV === 'development' || options.mode === 'development'; - const projectRoot = path.join( - context.root, - context.projectGraph.nodes[context.projectName].data.root - ); + const sourceRoot = path.join( context.root, context.projectGraph.nodes[context.projectName].data.sourceRoot @@ -30,6 +25,20 @@ export function withNx(_opts = {}) { return acc; }, {}); + const externals: ExternalItem = {}; + if (options.target === 'node') { + const projectDeps = + context.projectGraph.dependencies[context.projectName]; + for (const dep of Object.values(projectDeps)) { + const externalNode = context.projectGraph.externalNodes[dep.target]; + if (externalNode) { + externals[ + externalNode.data.packageName + ] = `"${externalNode.data.packageName}"`; + } + } + } + const updated: Configuration = { ...config, target: options.target, @@ -49,7 +58,10 @@ export function withNx(_opts = {}) { output: { path: path.join(context.root, options.outputPath), publicPath: '/', - filename: isProd ? '[name].[contenthash:8][ext]' : '[name][ext]', + filename: + isProd && options.target !== 'node' + ? '[name].[contenthash:8][ext]' + : '[name][ext]', }, devServer: { port: 4200, @@ -80,24 +92,9 @@ export function withNx(_opts = {}) { normalizeAssets(options.assets, context.root, sourceRoot) ), }, - html: [ - { - template: options.indexHtml - ? path.join(context.root, options.indexHtml) - : path.join(projectRoot, 'src/index.html'), - }, - ], - define: { - 'process.env.NODE_ENV': isProd ? "'production'" : "'development'", - }, progress: {}, - // TODO(jack): This should go to withReact. - react: { - runtime: 'automatic', - development: isDev, - refresh: isDev, - }, }, + externals, stats: { colors: true, preset: 'normal',