fix(rspack): set externals for target node (#194)

This commit is contained in:
Katerina Skroumpelou 2023-03-28 19:02:34 +03:00 committed by GitHub
parent f7fb57cec4
commit cc6ac25045
2 changed files with 22 additions and 25 deletions

View File

@ -86,7 +86,7 @@ function addBuildTarget(tree: Tree, options: ConfigurationSchema) {
}, },
production: { production: {
mode: 'production', mode: 'production',
optimization: true, optimization: options.target === 'web' ? true : undefined,
sourceMap: false, sourceMap: false,
}, },
}, },

View File

@ -1,4 +1,4 @@
import { Configuration } from '@rspack/core'; import { Configuration, ExternalItem } from '@rspack/core';
import * as path from 'path'; import * as path from 'path';
import { getCopyPatterns } from './get-copy-patterns'; import { getCopyPatterns } from './get-copy-patterns';
import { SharedConfigContext } from './model'; import { SharedConfigContext } from './model';
@ -11,12 +11,7 @@ export function withNx(_opts = {}) {
): Configuration { ): Configuration {
const isProd = const isProd =
process.env.NODE_ENV === 'production' || options.mode === 'production'; 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( const sourceRoot = path.join(
context.root, context.root,
context.projectGraph.nodes[context.projectName].data.sourceRoot context.projectGraph.nodes[context.projectName].data.sourceRoot
@ -30,6 +25,20 @@ export function withNx(_opts = {}) {
return acc; 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 = { const updated: Configuration = {
...config, ...config,
target: options.target, target: options.target,
@ -49,7 +58,10 @@ export function withNx(_opts = {}) {
output: { output: {
path: path.join(context.root, options.outputPath), path: path.join(context.root, options.outputPath),
publicPath: '/', publicPath: '/',
filename: isProd ? '[name].[contenthash:8][ext]' : '[name][ext]', filename:
isProd && options.target !== 'node'
? '[name].[contenthash:8][ext]'
: '[name][ext]',
}, },
devServer: { devServer: {
port: 4200, port: 4200,
@ -80,24 +92,9 @@ export function withNx(_opts = {}) {
normalizeAssets(options.assets, context.root, sourceRoot) 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: {}, progress: {},
// TODO(jack): This should go to withReact.
react: {
runtime: 'automatic',
development: isDev,
refresh: isDev,
},
}, },
externals,
stats: { stats: {
colors: true, colors: true,
preset: 'normal', preset: 'normal',