Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(angular-rspack,angular-rsbuild): add chunking options #31

Merged
merged 2 commits into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions e2e/fixtures/rspack-csr-css/rspack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ module.exports = () => {
options: {
extractLicenses: false,
optimization: false,
namedChunks: true,
vendorChunk: true,
},
},
}
Expand Down
16 changes: 16 additions & 0 deletions packages/angular-rsbuild/src/lib/models/normalize-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ export function validateOptimization(
);
}

function validateChunkOptions(options: Partial<PluginAngularOptions>) {
if (options.namedChunks !== undefined) {
console.warn(`The "namedChunks" option is not supported with Rsbuild.`);
}
if (options.commonChunk !== undefined) {
console.warn(`The "commonChunk" option is not supported with Rsbuild.`);
}
if (options.vendorChunk !== undefined) {
console.warn(`The "vendorChunk" option is not supported with Rsbuild.`);
}
}

export const DEFAULT_PLUGIN_ANGULAR_OPTIONS: PluginAngularOptions = {
index: './src/index.html',
browser: './src/main.ts',
Expand All @@ -90,6 +102,7 @@ export const DEFAULT_PLUGIN_ANGULAR_OPTIONS: PluginAngularOptions = {
outputPath: normalizeOutputPath(process.cwd(), undefined),
outputHashing: 'all',
useTsProjectReferences: false,
namedChunks: false,
skipTypeChecking: false,
devServer: {
port: 4200,
Expand Down Expand Up @@ -125,6 +138,8 @@ export function normalizeOptions(
const aot = options.aot ?? true;
const advancedOptimizations = aot && normalizedOptimization;

validateChunkOptions(options);

return {
...DEFAULT_PLUGIN_ANGULAR_OPTIONS,
...restOptions,
Expand All @@ -135,6 +150,7 @@ export function normalizeOptions(
advancedOptimizations,
aot,
outputHashing: options.outputHashing ?? 'all',
namedChunks: options.namedChunks ?? false,
fileReplacements: resolveFileReplacements(fileReplacements, root),
hasServer: getHasServer({ server, ssr: normalizedSsr }),
devServer: normalizeDevServer(devServer),
Expand Down
3 changes: 3 additions & 0 deletions packages/angular-rsbuild/src/lib/models/plugin-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ export interface PluginAngularOptions {
hasServer: boolean;
skipTypeChecking: boolean;
useTsProjectReferences?: boolean;
namedChunks?: boolean;
commonChunk?: boolean;
vendorChunk?: boolean;
stylePreprocessorOptions?: StylePreprocessorOptions;
devServer?: DevServerOptions;
}
Expand Down
184 changes: 106 additions & 78 deletions packages/angular-rspack/src/lib/config/create-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { getStyleLoaders } from './style-config-utils';
import { getOutputHashFormat } from './helpers';
import { getProxyConfig } from './dev-server-config-utils';

const VENDORS_TEST = /[\\/]node_modules[\\/]/;

export async function _createConfig(
options: AngularRspackPluginOptions,
rspackConfigOverrides?: Partial<Configuration>
Expand Down Expand Up @@ -157,48 +159,61 @@ export async function _createConfig(
normalizedOptions.devServer?.proxyConfig
),
},
optimization: normalizedOptions.optimization
? {
minimize: true,
runtimeChunk: false,
splitChunks: {
chunks: 'async',
minChunks: 1,
minSize: 20000,
maxAsyncRequests: 30,
maxInitialRequests: 30,
cacheGroups: {
defaultVendors: {
test: /[\\/]node_modules[\\/]/,
priority: -10,
reuseExistingChunk: true,
},
default: {
minChunks: 2,
priority: -20,
reuseExistingChunk: true,
},
},
},
minimizer: [
new SwcJsMinimizerRspackPlugin({
minimizerOptions: {
minify: true,
mangle: true,
compress: {
passes: 2,
optimization: {
chunkIds: normalizedOptions.namedChunks ? 'named' : 'deterministic',
moduleIds: 'deterministic',
...(normalizedOptions.optimization
? {
minimize: true,
runtimeChunk: 'single',
splitChunks: {
chunks: 'async',
minChunks: 1,
minSize: 20000,
maxAsyncRequests: 30,
maxInitialRequests: 30,
cacheGroups: {
default: normalizedOptions.commonChunk && {
chunks: 'async',
minChunks: 2,
priority: 10,
},
format: {
comments: false,
common: normalizedOptions.commonChunk && {
name: 'common',
chunks: 'async',
minChunks: 2,
enforce: true,
priority: 5,
},
vendors: false,
defaultVendors: normalizedOptions.vendorChunk && {
name: 'vendor',
chunks: (chunk) => chunk.name === 'main',
enforce: true,
test: VENDORS_TEST,
},
},
}),
],
}
: {
minimize: false,
minimizer: [],
},
},
minimizer: [
new SwcJsMinimizerRspackPlugin({
minimizerOptions: {
minify: true,
mangle: true,
compress: {
passes: 2,
},
format: {
comments: false,
},
},
}),
],
}
: {
minimize: false,
minimizer: [],
}),
},
plugins: [
...(defaultConfig.plugins ?? []),
new NgRspackPlugin({
Expand Down Expand Up @@ -288,48 +303,61 @@ export async function _createConfig(
scriptType: 'module',
module: true,
},
optimization: normalizedOptions.optimization
? {
minimize: true,
runtimeChunk: 'single',
splitChunks: {
chunks: 'all',
minChunks: 1,
minSize: 20000,
maxAsyncRequests: 30,
maxInitialRequests: 30,
cacheGroups: {
defaultVendors: {
test: /[\\/]node_modules[\\/]/,
priority: -10,
reuseExistingChunk: true,
},
default: {
minChunks: 2,
priority: -20,
reuseExistingChunk: true,
},
},
},
minimizer: [
new SwcJsMinimizerRspackPlugin({
minimizerOptions: {
minify: true,
mangle: true,
compress: {
passes: 2,
optimization: {
chunkIds: normalizedOptions.namedChunks ? 'named' : 'deterministic',
moduleIds: 'deterministic',
...(normalizedOptions.optimization
? {
minimize: true,
runtimeChunk: false,
splitChunks: {
chunks: 'all',
minChunks: 1,
minSize: 20000,
maxAsyncRequests: 30,
maxInitialRequests: 30,
cacheGroups: {
default: normalizedOptions.commonChunk && {
chunks: 'async',
minChunks: 2,
priority: 10,
},
format: {
comments: false,
common: normalizedOptions.commonChunk && {
name: 'common',
chunks: 'async',
minChunks: 2,
enforce: true,
priority: 5,
},
vendors: false,
defaultVendors: normalizedOptions.vendorChunk && {
name: 'vendor',
chunks: (chunk) => chunk.name === 'main',
enforce: true,
test: VENDORS_TEST,
},
},
}),
],
}
: {
minimize: false,
minimizer: [],
},
},
minimizer: [
new SwcJsMinimizerRspackPlugin({
minimizerOptions: {
minify: true,
mangle: true,
compress: {
passes: 2,
},
format: {
comments: false,
},
},
}),
],
}
: {
minimize: false,
minimizer: [],
}),
},
plugins: [
...(defaultConfig.plugins ?? []),
new NgRspackPlugin({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ export interface AngularRspackPluginOptions {
optimization?: boolean | OptimizationOptions;
outputHashing?: OutputHashing;
stylePreprocessorOptions?: StylePreprocessorOptions;
namedChunks?: boolean;
vendorChunk?: boolean;
commonChunk?: boolean;
devServer?: DevServerOptions;
extractLicenses?: boolean;
}
Expand All @@ -107,6 +110,9 @@ export interface NormalizedAngularRspackPluginOptions
index: NormalizedIndexElement | undefined;
devServer: DevServerOptions & { port: number };
extractLicenses: boolean;
namedChunks: boolean;
vendorChunk: boolean;
commonChunk: boolean;
globalScripts: GlobalEntry[];
globalStyles: GlobalEntry[];
optimization: boolean | OptimizationOptions;
Expand Down
3 changes: 3 additions & 0 deletions packages/angular-rspack/src/lib/models/normalize-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@ export function normalizeOptions(
hasServer: getHasServer({ server, ssr: normalizedSsr }),
skipTypeChecking: options.skipTypeChecking ?? false,
useTsProjectReferences: options.useTsProjectReferences ?? false,
namedChunks: options.namedChunks ?? false,
vendorChunk: options.vendorChunk ?? false,
commonChunk: options.commonChunk ?? true,
devServer: normalizeDevServer(options.devServer),
extractLicenses: options.extractLicenses ?? true,
};
Expand Down