Skip to content

Commit

Permalink
fix(rspack): only apply optimizations during production build
Browse files Browse the repository at this point in the history
  • Loading branch information
Coly010 committed Nov 12, 2024
1 parent e625cf1 commit 7907bdd
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 68 deletions.
69 changes: 36 additions & 33 deletions packages/rspack/src/plugins/utils/apply-base-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ import { getTerserEcmaVersion } from './get-terser-ecma-version';
import nodeExternals = require('webpack-node-externals');
import { NormalizedNxAppRspackPluginOptions } from './models';

const IGNORED_WEBPACK_WARNINGS = [
const IGNORED_RSPACK_WARNINGS = [
/The comment file/i,
/could not find any license/i,
];

const extensions = ['.ts', '.tsx', '.mjs', '.js', '.jsx'];
const extensions = ['...', '.ts', '.tsx', '.mjs', '.js', '.jsx'];
const mainFields = ['module', 'main'];

export function applyBaseConfig(
Expand All @@ -46,6 +46,7 @@ export function applyBaseConfig(
options.fileReplacements ??= [];
options.memoryLimit ??= 2048;
options.transformers ??= [];
options.progress ??= true;

applyNxIndependentConfig(options, config);

Expand Down Expand Up @@ -135,41 +136,43 @@ function applyNxIndependentConfig(

config.ignoreWarnings = [
(x) =>
IGNORED_WEBPACK_WARNINGS.some((r) =>
IGNORED_RSPACK_WARNINGS.some((r) =>
typeof x === 'string' ? r.test(x) : r.test(x.message)
),
];

config.optimization = {
...(config.optimization ?? {}),
sideEffects: true,
minimize:
typeof options.optimization === 'object'
? !!options.optimization.scripts
: !!options.optimization,
minimizer: [
new SwcJsMinimizerRspackPlugin({
extractComments: false,
minimizerOptions: {
module: true,
mangle: {
keep_classnames: true,
},
format: {
ecma: getTerserEcmaVersion(
path.join(options.root, options.projectRoot)
),
ascii_only: true,
comments: false,
webkit: true,
safari10: true,
},
},
}),
],
runtimeChunk: false,
concatenateModules: true,
};
config.optimization = !isProd
? undefined
: {
...(config.optimization ?? {}),
sideEffects: true,
minimize:
typeof options.optimization === 'object'
? !!options.optimization.scripts
: !!options.optimization,
minimizer: [
new SwcJsMinimizerRspackPlugin({
extractComments: false,
minimizerOptions: {
module: true,
mangle: {
keep_classnames: true,
},
format: {
ecma: getTerserEcmaVersion(
path.join(options.root, options.projectRoot)
),
ascii_only: true,
comments: false,
webkit: true,
safari10: true,
},
},
}),
],
runtimeChunk: false,
concatenateModules: true,
};

config.stats = {
hash: true,
Expand Down
72 changes: 37 additions & 35 deletions packages/rspack/src/plugins/utils/apply-web-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export function applyWebConfig(
}

const minimizer: RspackPluginInstance[] = [];
if (stylesOptimization) {
if (isProd && stylesOptimization) {
minimizer.push(
new LightningCssMinimizerRspackPlugin({
test: /\.(?:css|scss|sass|less|styl)$/,
Expand Down Expand Up @@ -363,41 +363,43 @@ export function applyWebConfig(
}
});

config.optimization = {
...(config.optimization ?? {}),
minimizer: [...(config.optimization?.minimizer ?? []), ...minimizer],
emitOnErrors: false,
moduleIds: 'deterministic' as const,
runtimeChunk: options.runtimeChunk ? { name: 'runtime' } : false,
splitChunks: {
defaultSizeTypes:
config.optimization?.splitChunks !== false
? config.optimization?.splitChunks?.defaultSizeTypes
: ['...'],
maxAsyncRequests: Infinity,
cacheGroups: {
default: !!options.commonChunk && {
chunks: 'async' as const,
minChunks: 2,
priority: 10,
},
common: !!options.commonChunk && {
name: 'common',
chunks: 'async' as const,
minChunks: 2,
enforce: true,
priority: 5,
},
vendors: false as const,
vendor: !!options.vendorChunk && {
name: 'vendor',
chunks: (chunk) => chunk.name === 'main',
enforce: true,
test: /[\\/]node_modules[\\/]/,
config.optimization = !isProd
? undefined
: {
...(config.optimization ?? {}),
minimizer: [...(config.optimization?.minimizer ?? []), ...minimizer],
emitOnErrors: false,
moduleIds: 'deterministic' as const,
runtimeChunk: options.runtimeChunk ? { name: 'runtime' } : false,
splitChunks: {
defaultSizeTypes:
config.optimization?.splitChunks !== false
? config.optimization?.splitChunks?.defaultSizeTypes
: ['...'],
maxAsyncRequests: Infinity,
cacheGroups: {
default: !!options.commonChunk && {
chunks: 'async' as const,
minChunks: 2,
priority: 10,
},
common: !!options.commonChunk && {
name: 'common',
chunks: 'async' as const,
minChunks: 2,
enforce: true,
priority: 5,
},
vendors: false as const,
vendor: !!options.vendorChunk && {
name: 'vendor',
chunks: (chunk) => chunk.name === 'main',
enforce: true,
test: /[\\/]node_modules[\\/]/,
},
},
},
},
},
};
};

config.resolve.mainFields = ['browser', 'module', 'main'];

Expand Down

0 comments on commit 7907bdd

Please sign in to comment.