@rsbuild/core 0.7.1 → 0.7.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -35,23 +35,6 @@ var __publicField = (obj, key, value) => {
35
35
  return value;
36
36
  };
37
37
 
38
- // src/constants.ts
39
- var import_node_path, PLUGIN_SWC_NAME, PLUGIN_CSS_NAME, LOADER_PATH, STATIC_PATH, COMPILED_PATH, TS_CONFIG_FILE, HTML_REGEX, CSS_REGEX;
40
- var init_constants = __esm({
41
- "src/constants.ts"() {
42
- "use strict";
43
- import_node_path = require("path");
44
- PLUGIN_SWC_NAME = "rsbuild:swc";
45
- PLUGIN_CSS_NAME = "rsbuild:css";
46
- LOADER_PATH = (0, import_node_path.join)(__dirname);
47
- STATIC_PATH = (0, import_node_path.join)(__dirname, "../static");
48
- COMPILED_PATH = (0, import_node_path.join)(__dirname, "../compiled");
49
- TS_CONFIG_FILE = "tsconfig.json";
50
- HTML_REGEX = /\.html$/;
51
- CSS_REGEX = /\.css$/;
52
- }
53
- });
54
-
55
38
  // src/client/format.ts
56
39
  function resolveFileName(stats) {
57
40
  if (stats.moduleIdentifier) {
@@ -65,8 +48,8 @@ function resolveFileName(stats) {
65
48
  }
66
49
  }
67
50
  }
68
- return `File: ${stats.moduleName}
69
- `;
51
+ return stats.moduleName ? `File: ${stats.moduleName}
52
+ ` : "";
70
53
  }
71
54
  function hintUnknownFiles(message) {
72
55
  const hint = "You may need an appropriate loader to handle this file type.";
@@ -130,6 +113,23 @@ var init_format = __esm({
130
113
  }
131
114
  });
132
115
 
116
+ // src/constants.ts
117
+ var import_node_path, PLUGIN_SWC_NAME, PLUGIN_CSS_NAME, LOADER_PATH, STATIC_PATH, COMPILED_PATH, TS_CONFIG_FILE, HTML_REGEX, CSS_REGEX;
118
+ var init_constants = __esm({
119
+ "src/constants.ts"() {
120
+ "use strict";
121
+ import_node_path = require("path");
122
+ PLUGIN_SWC_NAME = "rsbuild:swc";
123
+ PLUGIN_CSS_NAME = "rsbuild:css";
124
+ LOADER_PATH = (0, import_node_path.join)(__dirname);
125
+ STATIC_PATH = (0, import_node_path.join)(__dirname, "../static");
126
+ COMPILED_PATH = (0, import_node_path.join)(__dirname, "../compiled");
127
+ TS_CONFIG_FILE = "tsconfig.json";
128
+ HTML_REGEX = /\.html$/;
129
+ CSS_REGEX = /\.css$/;
130
+ }
131
+ });
132
+
133
133
  // src/helpers.ts
134
134
  function formatErrorMessage(errors) {
135
135
  const messages = errors.map((error) => hintNodePolyfill(error));
@@ -188,6 +188,10 @@ function formatStats(stats, options = {}) {
188
188
  }
189
189
  return {};
190
190
  }
191
+ function isEmptyDir(path13) {
192
+ const files = import_shared2.fse.readdirSync(path13);
193
+ return files.length === 0 || files.length === 1 && files[0] === ".git";
194
+ }
191
195
  async function isFileExists(file) {
192
196
  return import_shared2.fse.promises.access(file, import_shared2.fse.constants.F_OK).then(() => true).catch(() => false);
193
197
  }
@@ -592,7 +596,10 @@ var init_config = __esm({
592
596
  scriptLoading: "defer"
593
597
  });
594
598
  getDefaultSecurityConfig = () => ({
595
- nonce: ""
599
+ nonce: "",
600
+ sri: {
601
+ enable: false
602
+ }
596
603
  });
597
604
  getDefaultToolsConfig = () => ({
598
605
  cssExtract: {
@@ -650,6 +657,7 @@ var init_config = __esm({
650
657
  inlineStyles: false,
651
658
  cssModules: {
652
659
  auto: true,
660
+ namedExport: false,
653
661
  exportGlobals: false,
654
662
  exportLocalsConvention: "camelCase"
655
663
  },
@@ -667,7 +675,7 @@ var init_config = __esm({
667
675
  });
668
676
  withDefaultConfig = async (rootPath, config) => {
669
677
  const merged = mergeRsbuildConfig(createDefaultConfig(), config);
670
- merged.source || (merged.source = {});
678
+ merged.source ||= {};
671
679
  if (!merged.source.entry) {
672
680
  merged.source.entry = getDefaultEntry(rootPath);
673
681
  }
@@ -760,15 +768,15 @@ function loadEnv({
760
768
  publicVars
761
769
  };
762
770
  }
763
- var import_node_fs2, import_node_path5, import_shared6, import_dotenv_expand, import_dotenv;
771
+ var import_node_fs2, import_node_path5, import_shared6, import_dotenv, import_dotenv_expand;
764
772
  var init_loadEnv = __esm({
765
773
  "src/loadEnv.ts"() {
766
774
  "use strict";
767
775
  import_node_fs2 = __toESM(require("fs"));
768
776
  import_node_path5 = require("path");
769
777
  import_shared6 = require("@rsbuild/shared");
770
- import_dotenv_expand = require("../compiled/dotenv-expand/index.js");
771
778
  import_dotenv = require("../compiled/dotenv/index.js");
779
+ import_dotenv_expand = require("../compiled/dotenv-expand/index.js");
772
780
  init_helpers();
773
781
  }
774
782
  });
@@ -844,11 +852,10 @@ function getEntryObject(config, target) {
844
852
  if (!config.source?.entry) {
845
853
  return {};
846
854
  }
847
- return (0, import_shared8.mergeChainedOptions)({
848
- defaults: {},
849
- options: config.source?.entry,
850
- utils: { target },
851
- useObjectParam: true
855
+ return (0, import_shared8.reduceConfigsMergeContext)({
856
+ initial: {},
857
+ config: config.source?.entry,
858
+ ctx: { target }
852
859
  });
853
860
  }
854
861
  var import_shared8, pluginEntry;
@@ -911,7 +918,7 @@ async function createContextByConfig(options, bundlerType, config = {}) {
911
918
  return {
912
919
  entry: getEntryObject(config, "web"),
913
920
  targets: config.output?.targets || [],
914
- version: "0.7.1",
921
+ version: "0.7.3",
915
922
  rootPath,
916
923
  distPath,
917
924
  cachePath,
@@ -1380,10 +1387,10 @@ async function modifyRspackConfig(context, rspackConfig, utils) {
1380
1387
  utils
1381
1388
  );
1382
1389
  if (context.config.tools?.rspack) {
1383
- modifiedConfig = (0, import_shared13.mergeChainedOptions)({
1384
- defaults: modifiedConfig,
1385
- options: context.config.tools.rspack,
1386
- utils,
1390
+ modifiedConfig = await (0, import_shared13.reduceConfigsAsyncWithContext)({
1391
+ initial: modifiedConfig,
1392
+ config: context.config.tools.rspack,
1393
+ ctx: utils,
1387
1394
  mergeFn: utils.mergeConfig
1388
1395
  });
1389
1396
  }
@@ -1888,7 +1895,7 @@ function formatProxyOptions(proxyOptions) {
1888
1895
  }
1889
1896
  const handleError = (err) => import_shared18.logger.error(err);
1890
1897
  for (const opts of ret) {
1891
- opts.onError ?? (opts.onError = handleError);
1898
+ opts.onError ??= handleError;
1892
1899
  }
1893
1900
  return ret;
1894
1901
  }
@@ -3083,7 +3090,10 @@ var init_build = __esm({
3083
3090
  const buildError = err || new Error("Rspack build failed!");
3084
3091
  reject(buildError);
3085
3092
  } else {
3086
- compiler.close(() => {
3093
+ compiler.close((closeErr) => {
3094
+ if (closeErr) {
3095
+ import_shared24.logger.error(closeErr);
3096
+ }
3087
3097
  resolve({ stats });
3088
3098
  });
3089
3099
  }
@@ -3192,13 +3202,12 @@ var init_basic = __esm({
3192
3202
  name: "rsbuild:basic",
3193
3203
  setup(api) {
3194
3204
  api.modifyBundlerChain(
3195
- (chain, { env, isProd: isProd4, target, bundler, CHAIN_ID: CHAIN_ID3 }) => {
3196
- var _a;
3205
+ (chain, { env, isProd: isProd5, target, bundler, CHAIN_ID: CHAIN_ID3 }) => {
3197
3206
  const config = api.getNormalizedConfig();
3198
3207
  chain.name(import_shared25.TARGET_ID_MAP[target]);
3199
3208
  chain.devtool((0, import_shared25.getJsSourceMap)(config));
3200
3209
  chain.context(api.context.rootPath);
3201
- chain.mode(isProd4 ? "production" : "development");
3210
+ chain.mode(isProd5 ? "production" : "development");
3202
3211
  chain.merge({
3203
3212
  infrastructureLogging: {
3204
3213
  // Using `error` level to avoid `cache.PackFileCacheStrategy` logs
@@ -3211,9 +3220,9 @@ var init_basic = __esm({
3211
3220
  exportsPresence: "error"
3212
3221
  }
3213
3222
  });
3214
- const isMinimize = isProd4 && config.output.minify !== false;
3223
+ const isMinimize = isProd5 && config.output.minify !== false;
3215
3224
  chain.optimization.minimize(isMinimize);
3216
- const usingHMR = (0, import_shared25.isUsingHMR)(config, { target, isProd: isProd4 });
3225
+ const usingHMR = (0, import_shared25.isUsingHMR)(config, { target, isProd: isProd5 });
3217
3226
  if (usingHMR) {
3218
3227
  chain.plugin(CHAIN_ID3.PLUGIN.HMR).use(bundler.HotModuleReplacementPlugin);
3219
3228
  }
@@ -3223,7 +3232,7 @@ var init_basic = __esm({
3223
3232
  );
3224
3233
  }
3225
3234
  process.env.RSPACK_CONFIG_VALIDATE = "loose-silent";
3226
- (_a = process.env).WATCHPACK_WATCHER_LIMIT || (_a.WATCHPACK_WATCHER_LIMIT = "20");
3235
+ process.env.WATCHPACK_WATCHER_LIMIT ||= "20";
3227
3236
  }
3228
3237
  );
3229
3238
  }
@@ -3395,7 +3404,7 @@ async function applyCSSRule({
3395
3404
  rule,
3396
3405
  config,
3397
3406
  context,
3398
- utils: { target, isProd: isProd4, CHAIN_ID: CHAIN_ID3 },
3407
+ utils: { target, isProd: isProd5, CHAIN_ID: CHAIN_ID3 },
3399
3408
  importLoaders = 1
3400
3409
  }) {
3401
3410
  const browserslist = await (0, import_shared28.getBrowserslistWithDefault)(
@@ -3404,7 +3413,7 @@ async function applyCSSRule({
3404
3413
  target
3405
3414
  );
3406
3415
  const enableExtractCSS = isUseCssExtract(config, target);
3407
- const localIdentName = getCSSModulesLocalIdentName(config, isProd4);
3416
+ const localIdentName = getCSSModulesLocalIdentName(config, isProd5);
3408
3417
  const cssLoaderOptions = getCSSLoaderOptions({
3409
3418
  config,
3410
3419
  importLoaders,
@@ -3415,9 +3424,9 @@ async function applyCSSRule({
3415
3424
  if (enableExtractCSS) {
3416
3425
  rule.use(CHAIN_ID3.USE.MINI_CSS_EXTRACT).loader(getCssExtractPlugin().loader).options(config.tools.cssExtract.loaderOptions).end();
3417
3426
  } else {
3418
- const styleLoaderOptions = (0, import_shared28.mergeChainedOptions)({
3419
- defaults: {},
3420
- options: config.tools.styleLoader
3427
+ const styleLoaderOptions = (0, import_shared28.reduceConfigs)({
3428
+ initial: {},
3429
+ config: config.tools.styleLoader
3421
3430
  });
3422
3431
  rule.use(CHAIN_ID3.USE.STYLE).loader(getCompiledPath("style-loader")).options(styleLoaderOptions).end();
3423
3432
  }
@@ -3447,8 +3456,8 @@ var init_css = __esm({
3447
3456
  init_pluginHelper();
3448
3457
  enableNativeCss = (config) => !config.output.injectStyles;
3449
3458
  isUseCssExtract = (config, target) => !config.output.injectStyles && target !== "node" && target !== "web-worker";
3450
- getCSSModulesLocalIdentName = (config, isProd4) => config.output.cssModules.localIdentName || // Using shorter classname in production to reduce bundle size
3451
- (isProd4 ? "[local]-[hash:base64:6]" : "[path][name]__[local]-[hash:base64:6]");
3459
+ getCSSModulesLocalIdentName = (config, isProd5) => config.output.cssModules.localIdentName || // Using shorter classname in production to reduce bundle size
3460
+ (isProd5 ? "[local]-[hash:base64:6]" : "[path][name]__[local]-[hash:base64:6]");
3452
3461
  normalizeCssLoaderOptions = (options, exportOnlyLocals) => {
3453
3462
  if (options.modules && exportOnlyLocals) {
3454
3463
  let { modules } = options;
@@ -3485,12 +3494,12 @@ var init_css = __esm({
3485
3494
  });
3486
3495
  if (!hasAutoprefixer) {
3487
3496
  const { default: autoprefixer } = await import("@rsbuild/shared/autoprefixer");
3488
- const autoprefixerOptions = (0, import_shared28.mergeChainedOptions)({
3489
- defaults: {
3497
+ const autoprefixerOptions = (0, import_shared28.reduceConfigs)({
3498
+ initial: {
3490
3499
  flexbox: "no-2009",
3491
3500
  overrideBrowserslist: browserslist
3492
3501
  },
3493
- options: config.tools.autoprefixer
3502
+ config: config.tools.autoprefixer
3494
3503
  });
3495
3504
  pluginObjects.push(autoprefixer(autoprefixerOptions));
3496
3505
  }
@@ -3525,15 +3534,15 @@ var init_css = __esm({
3525
3534
  },
3526
3535
  sourceMap: config.output.sourceMap.css
3527
3536
  };
3528
- const mergedConfig = (0, import_shared28.mergeChainedOptions)({
3529
- defaults: defaultPostcssConfig,
3530
- options: config.tools.postcss,
3531
- utils
3537
+ const mergedConfig = (0, import_shared28.reduceConfigsWithContext)({
3538
+ initial: defaultPostcssConfig,
3539
+ config: config.tools.postcss,
3540
+ ctx: utils
3532
3541
  });
3533
3542
  if (extraPlugins.length) {
3534
3543
  mergedConfig?.postcssOptions?.plugins.push(...extraPlugins);
3535
3544
  }
3536
- mergedConfig.postcssOptions || (mergedConfig.postcssOptions = {});
3545
+ mergedConfig.postcssOptions ||= {};
3537
3546
  mergedConfig.postcssOptions.config = false;
3538
3547
  return mergedConfig;
3539
3548
  };
@@ -3547,17 +3556,14 @@ var init_css = __esm({
3547
3556
  const defaultOptions2 = {
3548
3557
  importLoaders,
3549
3558
  modules: {
3550
- auto: cssModules.auto,
3551
- namedExport: false,
3552
- exportGlobals: cssModules.exportGlobals,
3553
- exportLocalsConvention: cssModules.exportLocalsConvention,
3559
+ ...cssModules,
3554
3560
  localIdentName
3555
3561
  },
3556
3562
  sourceMap: config.output.sourceMap.css
3557
3563
  };
3558
- const mergedCssLoaderOptions = (0, import_shared28.mergeChainedOptions)({
3559
- defaults: defaultOptions2,
3560
- options: config.tools.cssLoader,
3564
+ const mergedCssLoaderOptions = (0, import_shared28.reduceConfigs)({
3565
+ initial: defaultOptions2,
3566
+ config: config.tools.cssLoader,
3561
3567
  mergeFn: import_shared28.deepmerge
3562
3568
  });
3563
3569
  const cssLoaderOptions = normalizeCssLoaderOptions(
@@ -3584,7 +3590,7 @@ var init_css = __esm({
3584
3590
  }
3585
3591
  });
3586
3592
  api.modifyRspackConfig(async (rspackConfig) => {
3587
- rspackConfig.experiments || (rspackConfig.experiments = {});
3593
+ rspackConfig.experiments ||= {};
3588
3594
  rspackConfig.experiments.css = false;
3589
3595
  });
3590
3596
  }
@@ -3598,13 +3604,13 @@ __export(output_exports, {
3598
3604
  pluginOutput: () => pluginOutput
3599
3605
  });
3600
3606
  function getPublicPath({
3601
- isProd: isProd4,
3607
+ isProd: isProd5,
3602
3608
  config,
3603
3609
  context
3604
3610
  }) {
3605
3611
  const { dev, output } = config;
3606
3612
  let publicPath = import_shared29.DEFAULT_ASSET_PREFIX;
3607
- if (isProd4) {
3613
+ if (isProd5) {
3608
3614
  if (typeof output.assetPrefix === "string") {
3609
3615
  publicPath = output.assetPrefix;
3610
3616
  }
@@ -3637,20 +3643,23 @@ var init_output = __esm({
3637
3643
  name: "rsbuild:output",
3638
3644
  setup(api) {
3639
3645
  api.modifyBundlerChain(
3640
- async (chain, { CHAIN_ID: CHAIN_ID3, target, isProd: isProd4, isServer, isServiceWorker }) => {
3646
+ async (chain, { CHAIN_ID: CHAIN_ID3, target, isProd: isProd5, isServer, isServiceWorker }) => {
3641
3647
  const config = api.getNormalizedConfig();
3642
3648
  const publicPath = getPublicPath({
3643
3649
  config,
3644
- isProd: isProd4,
3650
+ isProd: isProd5,
3645
3651
  context: api.context
3646
3652
  });
3647
3653
  const jsPath = (0, import_shared29.getDistPath)(config, "js");
3648
3654
  const jsAsyncPath = (0, import_shared29.getDistPath)(config, "jsAsync");
3649
- const jsFilename = (0, import_shared29.getFilename)(config, "js", isProd4);
3655
+ const jsFilename = (0, import_shared29.getFilename)(config, "js", isProd5);
3650
3656
  chain.output.path(api.context.distPath).filename(import_node_path15.posix.join(jsPath, jsFilename)).chunkFilename(import_node_path15.posix.join(jsAsyncPath, jsFilename)).publicPath(publicPath).pathinfo(false).hashFunction("xxhash64");
3651
3657
  if (isServer) {
3652
3658
  const serverPath = (0, import_shared29.getDistPath)(config, "server");
3653
- chain.output.path(import_node_path15.posix.join(api.context.distPath, serverPath)).filename("[name].js").chunkFilename("[name].js").libraryTarget("commonjs2");
3659
+ chain.output.path(import_node_path15.posix.join(api.context.distPath, serverPath)).filename("[name].js").chunkFilename("[name].js").library({
3660
+ ...chain.output.get("library") || {},
3661
+ type: "commonjs2"
3662
+ });
3654
3663
  }
3655
3664
  if (isServiceWorker) {
3656
3665
  const workerPath = (0, import_shared29.getDistPath)(config, "worker");
@@ -3665,7 +3674,7 @@ var init_output = __esm({
3665
3674
  if (isUseCssExtract(config, target)) {
3666
3675
  const extractPluginOptions = config.tools.cssExtract.pluginOptions;
3667
3676
  const cssPath = (0, import_shared29.getDistPath)(config, "css");
3668
- const cssFilename = (0, import_shared29.getFilename)(config, "css", isProd4);
3677
+ const cssFilename = (0, import_shared29.getFilename)(config, "css", isProd5);
3669
3678
  const cssAsyncPath = (0, import_shared29.getDistPath)(config, "cssAsync");
3670
3679
  chain.plugin(CHAIN_ID3.PLUGIN.MINI_CSS_EXTRACT).use(getCssExtractPlugin(), [
3671
3680
  {
@@ -3715,10 +3724,10 @@ function applyAlias({
3715
3724
  if (!alias) {
3716
3725
  return;
3717
3726
  }
3718
- const mergedAlias = (0, import_shared30.mergeChainedOptions)({
3719
- defaults: {},
3720
- options: alias,
3721
- utils: { target }
3727
+ const mergedAlias = (0, import_shared30.reduceConfigsWithContext)({
3728
+ initial: {},
3729
+ config: alias,
3730
+ ctx: { target }
3722
3731
  });
3723
3732
  for (const name of Object.keys(mergedAlias)) {
3724
3733
  const values = (0, import_shared30.castArray)(mergedAlias[name]);
@@ -3760,7 +3769,7 @@ var init_resolve = __esm({
3760
3769
  api.modifyRspackConfig(async (rspackConfig) => {
3761
3770
  const isTsProject = Boolean(api.context.tsconfigPath);
3762
3771
  const config = api.getNormalizedConfig();
3763
- rspackConfig.resolve || (rspackConfig.resolve = {});
3772
+ rspackConfig.resolve ||= {};
3764
3773
  if (isTsProject && config.source.aliasStrategy === "prefer-tsconfig") {
3765
3774
  rspackConfig.resolve.tsConfigPath = api.context.tsconfigPath;
3766
3775
  }
@@ -4040,12 +4049,12 @@ var init_asset = __esm({
4040
4049
  pluginAsset = () => ({
4041
4050
  name: "rsbuild:asset",
4042
4051
  setup(api) {
4043
- api.modifyBundlerChain((chain, { isProd: isProd4, target }) => {
4052
+ api.modifyBundlerChain((chain, { isProd: isProd5, target }) => {
4044
4053
  const config = api.getNormalizedConfig();
4045
4054
  const createAssetRule = (assetType, exts, emit2) => {
4046
4055
  const regExp = getRegExpForExts(exts);
4047
4056
  const distDir = (0, import_shared34.getDistPath)(config, assetType);
4048
- const filename = (0, import_shared34.getFilename)(config, assetType, isProd4);
4057
+ const filename = (0, import_shared34.getFilename)(config, assetType, isProd5);
4049
4058
  const { dataUriLimit } = config.output;
4050
4059
  const maxSize = typeof dataUriLimit === "number" ? dataUriLimit : dataUriLimit[assetType];
4051
4060
  const rule = chain.module.rule(assetType).test(regExp);
@@ -4100,7 +4109,7 @@ var init_minimize = __esm({
4100
4109
  pure_funcs: pureFuncs
4101
4110
  };
4102
4111
  }
4103
- options.format || (options.format = {});
4112
+ options.format ||= {};
4104
4113
  switch (config.output.legalComments) {
4105
4114
  case "inline":
4106
4115
  options.format.comments = "some";
@@ -4123,9 +4132,9 @@ var init_minimize = __esm({
4123
4132
  }
4124
4133
  return options;
4125
4134
  };
4126
- parseMinifyOptions = (config, isProd4 = true) => {
4135
+ parseMinifyOptions = (config, isProd5 = true) => {
4127
4136
  const minify = config.output.minify;
4128
- if (minify === false || !isProd4) {
4137
+ if (minify === false || !isProd5) {
4129
4138
  return {
4130
4139
  minifyJs: false,
4131
4140
  minifyCss: false,
@@ -4157,9 +4166,9 @@ var init_minimize = __esm({
4157
4166
  if (api.context.bundlerType === "webpack") {
4158
4167
  return;
4159
4168
  }
4160
- api.modifyBundlerChain(async (chain, { isProd: isProd4 }) => {
4169
+ api.modifyBundlerChain(async (chain, { isProd: isProd5 }) => {
4161
4170
  const config = api.getNormalizedConfig();
4162
- const isMinimize = isProd4 && config.output.minify !== false;
4171
+ const isMinimize = isProd5 && config.output.minify !== false;
4163
4172
  if (!isMinimize) {
4164
4173
  return;
4165
4174
  }
@@ -4362,10 +4371,17 @@ var init_HtmlBasicPlugin = __esm({
4362
4371
  addTitleTag(headTags, data.plugin.options?.title);
4363
4372
  }
4364
4373
  addFavicon(headTags, favicon);
4365
- const result = await this.modifyTagsFn({
4366
- headTags: headTags.map(formatBasicTag),
4367
- bodyTags: bodyTags.map(formatBasicTag)
4368
- });
4374
+ const result = await this.modifyTagsFn(
4375
+ {
4376
+ headTags: headTags.map(formatBasicTag),
4377
+ bodyTags: bodyTags.map(formatBasicTag)
4378
+ },
4379
+ {
4380
+ compilation,
4381
+ assetPrefix: data.publicPath,
4382
+ filename: data.outputName
4383
+ }
4384
+ );
4369
4385
  Object.assign(data, {
4370
4386
  headTags: result.headTags.map(fromBasicTag),
4371
4387
  bodyTags: result.bodyTags.map(fromBasicTag)
@@ -4493,8 +4509,8 @@ function getTerserMinifyOptions(config) {
4493
4509
  const finalOptions = applyRemoveConsole(options, config);
4494
4510
  return finalOptions;
4495
4511
  }
4496
- async function getHtmlMinifyOptions(isProd4, config) {
4497
- if (!isProd4 || !config.output.minify || !parseMinifyOptions(config).minifyHtml) {
4512
+ async function getHtmlMinifyOptions(isProd5, config) {
4513
+ if (!isProd5 || !config.output.minify || !parseMinifyOptions(config).minifyHtml) {
4498
4514
  return false;
4499
4515
  }
4500
4516
  const minifyJS = getTerserMinifyOptions(config);
@@ -4515,29 +4531,25 @@ async function getHtmlMinifyOptions(isProd4, config) {
4515
4531
  return typeof htmlMinifyOptions === "object" ? (0, import_shared38.deepmerge)(htmlMinifyDefaultOptions, htmlMinifyOptions) : htmlMinifyDefaultOptions;
4516
4532
  }
4517
4533
  function getTitle(entryName, config) {
4518
- return (0, import_shared38.mergeChainedOptions)({
4519
- defaults: "",
4520
- options: config.html.title,
4521
- utils: { entryName },
4522
- useObjectParam: true
4534
+ return (0, import_shared38.reduceConfigsMergeContext)({
4535
+ initial: "",
4536
+ config: config.html.title,
4537
+ ctx: { entryName }
4523
4538
  });
4524
4539
  }
4525
4540
  function getInject(entryName, config) {
4526
- return (0, import_shared38.mergeChainedOptions)({
4527
- defaults: "head",
4528
- options: config.html.inject,
4529
- utils: { entryName },
4530
- useObjectParam: true,
4531
- isFalsy: import_shared38.isNil
4541
+ return (0, import_shared38.reduceConfigsMergeContext)({
4542
+ initial: "head",
4543
+ config: config.html.inject,
4544
+ ctx: { entryName }
4532
4545
  });
4533
4546
  }
4534
4547
  async function getTemplate(entryName, config, rootPath) {
4535
4548
  const DEFAULT_TEMPLATE = import_node_path20.default.resolve(STATIC_PATH, "template.html");
4536
- const templatePath = (0, import_shared38.mergeChainedOptions)({
4537
- defaults: DEFAULT_TEMPLATE,
4538
- options: config.html.template,
4539
- utils: { entryName },
4540
- useObjectParam: true
4549
+ const templatePath = (0, import_shared38.reduceConfigsMergeContext)({
4550
+ initial: DEFAULT_TEMPLATE,
4551
+ config: config.html.template,
4552
+ ctx: { entryName }
4541
4553
  });
4542
4554
  if (templatePath === DEFAULT_TEMPLATE) {
4543
4555
  return {
@@ -4562,19 +4574,17 @@ async function getTemplate(entryName, config, rootPath) {
4562
4574
  };
4563
4575
  }
4564
4576
  function getFavicon(entryName, config) {
4565
- return (0, import_shared38.mergeChainedOptions)({
4566
- defaults: "",
4567
- options: config.html.favicon,
4568
- utils: { entryName },
4569
- useObjectParam: true
4577
+ return (0, import_shared38.reduceConfigsMergeContext)({
4578
+ initial: "",
4579
+ config: config.html.favicon,
4580
+ ctx: { entryName }
4570
4581
  });
4571
4582
  }
4572
4583
  function getMetaTags(entryName, config, templateContent) {
4573
- const metaTags = (0, import_shared38.mergeChainedOptions)({
4574
- defaults: {},
4575
- options: config.html.meta,
4576
- utils: { entryName },
4577
- useObjectParam: true
4584
+ const metaTags = (0, import_shared38.reduceConfigsMergeContext)({
4585
+ initial: {},
4586
+ config: config.html.meta,
4587
+ ctx: { entryName }
4578
4588
  });
4579
4589
  if (templateContent && metaTags.charset) {
4580
4590
  const charsetRegExp = /<meta[^>]+charset=["'][^>]*>/i;
@@ -4599,10 +4609,10 @@ function getTemplateParameters(entryName, config, assetPrefix) {
4599
4609
  options: pluginOptions
4600
4610
  }
4601
4611
  };
4602
- return (0, import_shared38.mergeChainedOptions)({
4603
- defaults: defaultOptions2,
4604
- options: templateParameters,
4605
- utils: { entryName }
4612
+ return (0, import_shared38.reduceConfigsWithContext)({
4613
+ initial: defaultOptions2,
4614
+ config: templateParameters,
4615
+ ctx: { entryName }
4606
4616
  });
4607
4617
  };
4608
4618
  }
@@ -4651,12 +4661,12 @@ var init_html = __esm({
4651
4661
  name: "rsbuild:html",
4652
4662
  setup(api) {
4653
4663
  api.modifyBundlerChain(
4654
- async (chain, { HtmlPlugin, isProd: isProd4, CHAIN_ID: CHAIN_ID3, target }) => {
4664
+ async (chain, { HtmlPlugin, isProd: isProd5, CHAIN_ID: CHAIN_ID3, target }) => {
4655
4665
  const config = api.getNormalizedConfig();
4656
4666
  if ((0, import_shared38.isHtmlDisabled)(config, target)) {
4657
4667
  return;
4658
4668
  }
4659
- const minify = await getHtmlMinifyOptions(isProd4, config);
4669
+ const minify = await getHtmlMinifyOptions(isProd5, config);
4660
4670
  const assetPrefix = getPublicPathFromChain(chain, false);
4661
4671
  const entries = chain.entryPoints.entries() || {};
4662
4672
  const entryNames = Object.keys(entries);
@@ -4665,11 +4675,7 @@ var init_html = __esm({
4665
4675
  const finalOptions = await Promise.all(
4666
4676
  entryNames.map(async (entryName) => {
4667
4677
  const entryValue = entries[entryName].values();
4668
- const chunks = getChunks(
4669
- entryName,
4670
- // EntryDescription type is different between webpack and Rspack
4671
- entryValue
4672
- );
4678
+ const chunks = getChunks(entryName, entryValue);
4673
4679
  const inject = getInject(entryName, config);
4674
4680
  const filename = htmlPaths[entryName];
4675
4681
  const { templatePath, templateContent } = await getTemplate(
@@ -4715,13 +4721,10 @@ var init_html = __esm({
4715
4721
  pluginOptions.favicon = favicon;
4716
4722
  }
4717
4723
  }
4718
- const finalOptions2 = (0, import_shared38.mergeChainedOptions)({
4719
- defaults: pluginOptions,
4720
- options: typeof config.tools.htmlPlugin === "boolean" ? {} : config.tools.htmlPlugin,
4721
- utils: {
4722
- entryName,
4723
- entryValue
4724
- }
4724
+ const finalOptions2 = (0, import_shared38.reduceConfigsWithContext)({
4725
+ initial: pluginOptions,
4726
+ config: typeof config.tools.htmlPlugin === "boolean" ? {} : config.tools.htmlPlugin,
4727
+ ctx: { entryName, entryValue }
4725
4728
  });
4726
4729
  return finalOptions2;
4727
4730
  })
@@ -4746,50 +4749,18 @@ var init_html = __esm({
4746
4749
  }
4747
4750
  }
4748
4751
  );
4749
- api.onAfterCreateCompiler(({ compiler }) => {
4750
- const { nonce } = api.getNormalizedConfig().security;
4751
- if (!nonce) {
4752
- return;
4753
- }
4754
- (0, import_shared38.applyToCompiler)(compiler, (compiler2) => {
4755
- const { plugins } = compiler2.options;
4756
- const hasHTML = plugins.some(
4757
- (plugin) => plugin && plugin.constructor.name === "HtmlBasicPlugin"
4758
- );
4759
- if (!hasHTML) {
4760
- return;
4761
- }
4762
- const injectCode = (0, import_shared38.createVirtualModule)(
4763
- `__webpack_nonce__ = "${nonce}";`
4764
- );
4765
- new compiler2.webpack.EntryPlugin(compiler2.context, injectCode, {
4766
- name: void 0
4767
- }).apply(compiler2);
4768
- });
4769
- });
4770
4752
  api.modifyHTMLTags({
4771
4753
  // ensure `crossorigin` and `nonce` can be applied to all tags
4772
4754
  order: "post",
4773
4755
  handler: ({ headTags, bodyTags }) => {
4774
- var _a;
4775
4756
  const config = api.getNormalizedConfig();
4776
4757
  const { crossorigin } = config.html;
4777
- const { nonce } = config.security;
4778
4758
  const allTags = [...headTags, ...bodyTags];
4779
4759
  if (crossorigin) {
4780
4760
  const formattedCrossorigin = crossorigin === true ? "anonymous" : crossorigin;
4781
4761
  for (const tag of allTags) {
4782
4762
  if (tag.tag === "script" && tag.attrs?.src || tag.tag === "link" && tag.attrs?.rel === "stylesheet") {
4783
- tag.attrs || (tag.attrs = {});
4784
- (_a = tag.attrs).crossorigin ?? (_a.crossorigin = formattedCrossorigin);
4785
- }
4786
- }
4787
- }
4788
- if (nonce) {
4789
- for (const tag of allTags) {
4790
- if (tag.tag === "script" || tag.tag === "style") {
4791
- tag.attrs ?? (tag.attrs = {});
4792
- tag.attrs.nonce = nonce;
4763
+ tag.attrs.crossorigin ??= formattedCrossorigin;
4793
4764
  }
4794
4765
  }
4795
4766
  }
@@ -5020,17 +4991,15 @@ async function applyCoreJs(swcConfig, chain, polyfillMode) {
5020
4991
  });
5021
4992
  }
5022
4993
  function applyTransformImport(swcConfig, pluginImport) {
5023
- var _a;
5024
4994
  if (pluginImport !== false && pluginImport) {
5025
- swcConfig.rspackExperiments ?? (swcConfig.rspackExperiments = {});
5026
- (_a = swcConfig.rspackExperiments).import ?? (_a.import = []);
4995
+ swcConfig.rspackExperiments ??= {};
4996
+ swcConfig.rspackExperiments.import ??= [];
5027
4997
  swcConfig.rspackExperiments.import.push(...pluginImport);
5028
4998
  }
5029
4999
  }
5030
5000
  function applySwcDecoratorConfig(swcConfig, config) {
5031
- var _a;
5032
- swcConfig.jsc || (swcConfig.jsc = {});
5033
- (_a = swcConfig.jsc).transform || (_a.transform = {});
5001
+ swcConfig.jsc ||= {};
5002
+ swcConfig.jsc.transform ||= {};
5034
5003
  const { version: version2 } = config.source.decorators;
5035
5004
  switch (version2) {
5036
5005
  case "legacy":
@@ -5095,9 +5064,9 @@ var init_swc = __esm({
5095
5064
  await applyCoreJs(swcConfig, chain, polyfillMode);
5096
5065
  }
5097
5066
  }
5098
- const mergedSwcConfig = (0, import_shared42.mergeChainedOptions)({
5099
- defaults: swcConfig,
5100
- options: config.tools.swc,
5067
+ const mergedSwcConfig = (0, import_shared42.reduceConfigs)({
5068
+ initial: swcConfig,
5069
+ config: config.tools.swc,
5101
5070
  mergeFn: import_shared42.deepmerge
5102
5071
  });
5103
5072
  rule.use(CHAIN_ID3.USE.SWC).loader(builtinSwcLoaderName).options(mergedSwcConfig);
@@ -5705,7 +5674,7 @@ function pluginBundleAnalyzer() {
5705
5674
  if ((0, import_shared47.isProd)() || !isUseAnalyzer(config)) {
5706
5675
  return;
5707
5676
  }
5708
- config.dev || (config.dev = {});
5677
+ config.dev ||= {};
5709
5678
  config.dev.writeToDisk = true;
5710
5679
  return config;
5711
5680
  });
@@ -5779,7 +5748,7 @@ var init_rsdoctor = __esm({
5779
5748
  if (registered) {
5780
5749
  return;
5781
5750
  }
5782
- config.plugins || (config.plugins = []);
5751
+ config.plugins ||= [];
5783
5752
  config.plugins.push(new module2[pluginName]());
5784
5753
  isAutoRegister = true;
5785
5754
  }
@@ -6146,7 +6115,7 @@ var init_performance = __esm({
6146
6115
  api.modifyRsbuildConfig((rsbuildConfig) => {
6147
6116
  if (rsbuildConfig.performance?.profile) {
6148
6117
  if (!rsbuildConfig.performance?.bundleAnalyze) {
6149
- rsbuildConfig.performance ?? (rsbuildConfig.performance = {});
6118
+ rsbuildConfig.performance ??= {};
6150
6119
  rsbuildConfig.performance.bundleAnalyze = {
6151
6120
  analyzerMode: "disabled",
6152
6121
  generateStatsFile: true
@@ -6540,8 +6509,8 @@ var init_lazyCompilation = __esm({
6540
6509
  pluginLazyCompilation = () => ({
6541
6510
  name: "rsbuild:lazy-compilation",
6542
6511
  setup(api) {
6543
- api.modifyBundlerChain((chain, { isProd: isProd4, target }) => {
6544
- if (isProd4 || target !== "web") {
6512
+ api.modifyBundlerChain((chain, { isProd: isProd5, target }) => {
6513
+ if (isProd5 || target !== "web") {
6545
6514
  return;
6546
6515
  }
6547
6516
  const config = api.getNormalizedConfig();
@@ -6601,6 +6570,222 @@ var init_lazyCompilation = __esm({
6601
6570
  }
6602
6571
  });
6603
6572
 
6573
+ // src/plugins/sri.ts
6574
+ var sri_exports = {};
6575
+ __export(sri_exports, {
6576
+ pluginSri: () => pluginSri
6577
+ });
6578
+ var import_node_crypto2, import_shared57, getAssetName, pluginSri;
6579
+ var init_sri = __esm({
6580
+ "src/plugins/sri.ts"() {
6581
+ "use strict";
6582
+ import_node_crypto2 = __toESM(require("crypto"));
6583
+ import_shared57 = require("@rsbuild/shared");
6584
+ init_constants();
6585
+ getAssetName = (url2, assetPrefix) => {
6586
+ if (url2.startsWith(assetPrefix)) {
6587
+ return (0, import_shared57.removeLeadingSlash)(url2.replace(assetPrefix, ""));
6588
+ }
6589
+ return (0, import_shared57.removeLeadingSlash)(url2);
6590
+ };
6591
+ pluginSri = () => ({
6592
+ name: "rsbuild:sri",
6593
+ setup(api) {
6594
+ const placeholder = "RSBUILD_INTEGRITY_PLACEHOLDER:";
6595
+ const getAlgorithm = () => {
6596
+ const config = api.getNormalizedConfig();
6597
+ const { sri } = config.security;
6598
+ const enable = sri.enable === "auto" ? (0, import_shared57.isProd)() : sri.enable;
6599
+ if (!enable) {
6600
+ return null;
6601
+ }
6602
+ const { algorithm = "sha384" } = sri;
6603
+ return algorithm;
6604
+ };
6605
+ api.modifyHTMLTags({
6606
+ // ensure `sri` can be applied to all tags
6607
+ order: "post",
6608
+ handler(tags, { assetPrefix }) {
6609
+ const algorithm = getAlgorithm();
6610
+ if (!algorithm) {
6611
+ return tags;
6612
+ }
6613
+ const allTags = [...tags.headTags, ...tags.bodyTags];
6614
+ for (const tag of allTags) {
6615
+ let url2 = "";
6616
+ if (!tag.attrs) {
6617
+ continue;
6618
+ }
6619
+ if (tag.tag === "script" && typeof tag.attrs.src === "string") {
6620
+ url2 = tag.attrs.src;
6621
+ } else if (tag.tag === "link" && tag.attrs.rel === "stylesheet" && typeof tag.attrs.href === "string") {
6622
+ url2 = tag.attrs.href;
6623
+ }
6624
+ if (!url2) {
6625
+ continue;
6626
+ }
6627
+ const assetName = getAssetName(url2, assetPrefix);
6628
+ if (!assetName) {
6629
+ continue;
6630
+ }
6631
+ tag.attrs.integrity ??= `${placeholder}${assetName}`;
6632
+ }
6633
+ return tags;
6634
+ }
6635
+ });
6636
+ const replaceIntegrity = (htmlContent, assets, algorithm, integrityCache) => {
6637
+ const regex = /integrity="RSBUILD_INTEGRITY_PLACEHOLDER:([^"]+)"/g;
6638
+ const matches = htmlContent.matchAll(regex);
6639
+ let replacedHtml = htmlContent;
6640
+ const calcIntegrity = (algorithm2, assetName, data) => {
6641
+ if (integrityCache.has(assetName)) {
6642
+ return integrityCache.get(assetName);
6643
+ }
6644
+ const hash = import_node_crypto2.default.createHash(algorithm2).update(data).digest().toString("base64");
6645
+ const integrity = `${algorithm2}-${hash}`;
6646
+ integrityCache.set(assetName, integrity);
6647
+ return integrity;
6648
+ };
6649
+ for (const match of matches) {
6650
+ const assetName = match[1];
6651
+ if (!assetName) {
6652
+ continue;
6653
+ }
6654
+ if (assets[assetName]) {
6655
+ const integrity = calcIntegrity(
6656
+ algorithm,
6657
+ assetName,
6658
+ assets[assetName].buffer()
6659
+ );
6660
+ replacedHtml = replacedHtml.replaceAll(
6661
+ `integrity="${placeholder}${assetName}"`,
6662
+ `integrity="${integrity}"`
6663
+ );
6664
+ } else {
6665
+ import_shared57.logger.debug(
6666
+ `[rsbuild:sri] failed to generate integrity for ${assetName}.`
6667
+ );
6668
+ replacedHtml = replacedHtml.replace(
6669
+ `integrity="${placeholder}${assetName}"`,
6670
+ ""
6671
+ );
6672
+ }
6673
+ }
6674
+ return replacedHtml;
6675
+ };
6676
+ class SriReplaceIntegrityPlugin {
6677
+ constructor(algorithm) {
6678
+ __publicField(this, "algorithm");
6679
+ this.algorithm = algorithm;
6680
+ }
6681
+ apply(compiler) {
6682
+ compiler.hooks.compilation.tap(
6683
+ "SriReplaceIntegrityPlugin",
6684
+ (compilation) => {
6685
+ compilation.hooks.processAssets.tapPromise(
6686
+ {
6687
+ name: "SriReplaceIntegrityPlugin",
6688
+ // use to final stage to get the final asset content
6689
+ stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_REPORT
6690
+ },
6691
+ async (assets) => {
6692
+ const integrityCache = /* @__PURE__ */ new Map();
6693
+ for (const asset of Object.keys(assets)) {
6694
+ if (!HTML_REGEX.test(asset)) {
6695
+ continue;
6696
+ }
6697
+ const htmlContent = assets[asset].source();
6698
+ if (!htmlContent.includes(placeholder)) {
6699
+ continue;
6700
+ }
6701
+ assets[asset] = new compiler.webpack.sources.RawSource(
6702
+ replaceIntegrity(
6703
+ htmlContent,
6704
+ assets,
6705
+ this.algorithm,
6706
+ integrityCache
6707
+ )
6708
+ );
6709
+ }
6710
+ }
6711
+ );
6712
+ }
6713
+ );
6714
+ }
6715
+ }
6716
+ api.modifyBundlerChain((chain, { target }) => {
6717
+ const config = api.getNormalizedConfig();
6718
+ if ((0, import_shared57.isHtmlDisabled)(config, target)) {
6719
+ return;
6720
+ }
6721
+ const algorithm = getAlgorithm();
6722
+ if (!algorithm) {
6723
+ return;
6724
+ }
6725
+ chain.plugin("rsbuild-sri-replace").use(SriReplaceIntegrityPlugin, [algorithm]);
6726
+ });
6727
+ }
6728
+ });
6729
+ }
6730
+ });
6731
+
6732
+ // src/plugins/nonce.ts
6733
+ var nonce_exports = {};
6734
+ __export(nonce_exports, {
6735
+ pluginNonce: () => pluginNonce
6736
+ });
6737
+ var import_shared58, pluginNonce;
6738
+ var init_nonce = __esm({
6739
+ "src/plugins/nonce.ts"() {
6740
+ "use strict";
6741
+ import_shared58 = require("@rsbuild/shared");
6742
+ pluginNonce = () => ({
6743
+ name: "rsbuild:nonce",
6744
+ setup(api) {
6745
+ api.onAfterCreateCompiler(({ compiler }) => {
6746
+ const { nonce } = api.getNormalizedConfig().security;
6747
+ if (!nonce) {
6748
+ return;
6749
+ }
6750
+ (0, import_shared58.applyToCompiler)(compiler, (compiler2) => {
6751
+ const { plugins } = compiler2.options;
6752
+ const hasHTML = plugins.some(
6753
+ (plugin) => plugin && plugin.constructor.name === "HtmlBasicPlugin"
6754
+ );
6755
+ if (!hasHTML) {
6756
+ return;
6757
+ }
6758
+ const injectCode = (0, import_shared58.createVirtualModule)(
6759
+ `__webpack_nonce__ = "${nonce}";`
6760
+ );
6761
+ new compiler2.webpack.EntryPlugin(compiler2.context, injectCode, {
6762
+ name: void 0
6763
+ }).apply(compiler2);
6764
+ });
6765
+ });
6766
+ api.modifyHTMLTags({
6767
+ // ensure `nonce` can be applied to all tags
6768
+ order: "post",
6769
+ handler: ({ headTags, bodyTags }) => {
6770
+ const config = api.getNormalizedConfig();
6771
+ const { nonce } = config.security;
6772
+ const allTags = [...headTags, ...bodyTags];
6773
+ if (nonce) {
6774
+ for (const tag of allTags) {
6775
+ if (tag.tag === "script" || tag.tag === "style") {
6776
+ tag.attrs ??= {};
6777
+ tag.attrs.nonce = nonce;
6778
+ }
6779
+ }
6780
+ }
6781
+ return { headTags, bodyTags };
6782
+ }
6783
+ });
6784
+ }
6785
+ });
6786
+ }
6787
+ });
6788
+
6604
6789
  // src/createRsbuild.ts
6605
6790
  var createRsbuild_exports = {};
6606
6791
  __export(createRsbuild_exports, {
@@ -6639,6 +6824,8 @@ async function applyDefaultPlugins(pluginManager, context) {
6639
6824
  const { pluginModuleFederation: pluginModuleFederation2 } = await Promise.resolve().then(() => (init_moduleFederation(), moduleFederation_exports));
6640
6825
  const { pluginRspackProfile: pluginRspackProfile2 } = await Promise.resolve().then(() => (init_rspackProfile(), rspackProfile_exports));
6641
6826
  const { pluginLazyCompilation: pluginLazyCompilation2 } = await Promise.resolve().then(() => (init_lazyCompilation(), lazyCompilation_exports));
6827
+ const { pluginSri: pluginSri2 } = await Promise.resolve().then(() => (init_sri(), sri_exports));
6828
+ const { pluginNonce: pluginNonce2 } = await Promise.resolve().then(() => (init_nonce(), nonce_exports));
6642
6829
  pluginManager.addPlugins([
6643
6830
  pluginBasic2(),
6644
6831
  pluginEntry2(),
@@ -6650,8 +6837,8 @@ async function applyDefaultPlugins(pluginManager, context) {
6650
6837
  // cleanOutput plugin should before the html plugin
6651
6838
  pluginCleanOutput2(),
6652
6839
  pluginAsset2(),
6653
- pluginHtml2(async (tags) => {
6654
- const result = await context.hooks.modifyHTMLTags.call(tags);
6840
+ pluginHtml2(async (...args) => {
6841
+ const result = await context.hooks.modifyHTMLTags.call(...args);
6655
6842
  return result[0];
6656
6843
  }),
6657
6844
  pluginWasm2(),
@@ -6674,7 +6861,9 @@ async function applyDefaultPlugins(pluginManager, context) {
6674
6861
  pluginManifest2(),
6675
6862
  pluginModuleFederation2(),
6676
6863
  pluginRspackProfile2(),
6677
- pluginLazyCompilation2()
6864
+ pluginLazyCompilation2(),
6865
+ pluginSri2(),
6866
+ pluginNonce2()
6678
6867
  ]);
6679
6868
  }
6680
6869
  async function createRsbuild(options = {}) {
@@ -6692,9 +6881,9 @@ async function createRsbuild(options = {}) {
6692
6881
  );
6693
6882
  const pluginAPI = getPluginAPI({ context, pluginManager });
6694
6883
  context.pluginAPI = pluginAPI;
6695
- (0, import_shared57.debug)("add default plugins");
6884
+ (0, import_shared59.debug)("add default plugins");
6696
6885
  await applyDefaultPlugins(pluginManager, context);
6697
- (0, import_shared57.debug)("add default plugins done");
6886
+ (0, import_shared59.debug)("add default plugins done");
6698
6887
  const provider = rsbuildConfig.provider || await getRspackProvider();
6699
6888
  const providerInstance = await provider({
6700
6889
  context,
@@ -6703,13 +6892,13 @@ async function createRsbuild(options = {}) {
6703
6892
  setCssExtractPlugin
6704
6893
  });
6705
6894
  const rsbuild = {
6706
- ...(0, import_shared57.pick)(pluginManager, [
6895
+ ...(0, import_shared59.pick)(pluginManager, [
6707
6896
  "addPlugins",
6708
6897
  "getPlugins",
6709
6898
  "removePlugins",
6710
6899
  "isPluginExists"
6711
6900
  ]),
6712
- ...(0, import_shared57.pick)(pluginAPI, [
6901
+ ...(0, import_shared59.pick)(pluginAPI, [
6713
6902
  "onBeforeBuild",
6714
6903
  "onBeforeCreateCompiler",
6715
6904
  "onBeforeStartDevServer",
@@ -6725,7 +6914,7 @@ async function createRsbuild(options = {}) {
6725
6914
  "getRsbuildConfig",
6726
6915
  "getNormalizedConfig"
6727
6916
  ]),
6728
- ...(0, import_shared57.pick)(providerInstance, [
6917
+ ...(0, import_shared59.pick)(providerInstance, [
6729
6918
  "build",
6730
6919
  "preview",
6731
6920
  "initConfigs",
@@ -6742,11 +6931,11 @@ async function createRsbuild(options = {}) {
6742
6931
  }
6743
6932
  return rsbuild;
6744
6933
  }
6745
- var import_shared57, getRspackProvider, pickRsbuildConfig;
6934
+ var import_shared59, getRspackProvider, pickRsbuildConfig;
6746
6935
  var init_createRsbuild = __esm({
6747
6936
  "src/createRsbuild.ts"() {
6748
6937
  "use strict";
6749
- import_shared57 = require("@rsbuild/shared");
6938
+ import_shared59 = require("@rsbuild/shared");
6750
6939
  init_createContext();
6751
6940
  init_initPlugins();
6752
6941
  init_pluginHelper();
@@ -6768,7 +6957,7 @@ var init_createRsbuild = __esm({
6768
6957
  "moduleFederation",
6769
6958
  "_privateMeta"
6770
6959
  ];
6771
- return (0, import_shared57.pick)(rsbuildConfig, keys);
6960
+ return (0, import_shared59.pick)(rsbuildConfig, keys);
6772
6961
  };
6773
6962
  }
6774
6963
  });
@@ -6787,7 +6976,7 @@ async function init({
6787
6976
  cwd: root,
6788
6977
  mode: cliOptions?.envMode
6789
6978
  });
6790
- if ((0, import_shared58.isDev)()) {
6979
+ if ((0, import_shared60.isDev)()) {
6791
6980
  onBeforeRestartServer(envs.cleanup);
6792
6981
  }
6793
6982
  const { content: config, filePath: configFilePath } = await loadConfig({
@@ -6804,21 +6993,21 @@ async function init({
6804
6993
  watchFiles(files);
6805
6994
  }
6806
6995
  const { createRsbuild: createRsbuild2 } = await Promise.resolve().then(() => (init_createRsbuild(), createRsbuild_exports));
6807
- config.source || (config.source = {});
6996
+ config.source ||= {};
6808
6997
  config.source.define = {
6809
6998
  ...envs.publicVars,
6810
6999
  ...config.source.define
6811
7000
  };
6812
7001
  if (commonOpts.open && !config.dev?.startUrl) {
6813
- config.dev || (config.dev = {});
7002
+ config.dev ||= {};
6814
7003
  config.dev.startUrl = commonOpts.open;
6815
7004
  }
6816
7005
  if (commonOpts.host) {
6817
- config.server || (config.server = {});
7006
+ config.server ||= {};
6818
7007
  config.server.host = commonOpts.host;
6819
7008
  }
6820
7009
  if (commonOpts.port) {
6821
- config.server || (config.server = {});
7010
+ config.server ||= {};
6822
7011
  config.server.port = commonOpts.port;
6823
7012
  }
6824
7013
  return createRsbuild2({
@@ -6827,17 +7016,17 @@ async function init({
6827
7016
  });
6828
7017
  } catch (err) {
6829
7018
  if (isRestart) {
6830
- import_shared58.logger.error(err);
7019
+ import_shared60.logger.error(err);
6831
7020
  } else {
6832
7021
  throw err;
6833
7022
  }
6834
7023
  }
6835
7024
  }
6836
- var import_shared58, commonOpts;
7025
+ var import_shared60, commonOpts;
6837
7026
  var init_init = __esm({
6838
7027
  "src/cli/init.ts"() {
6839
7028
  "use strict";
6840
- import_shared58 = require("@rsbuild/shared");
7029
+ import_shared60 = require("@rsbuild/shared");
6841
7030
  init_config();
6842
7031
  init_loadEnv();
6843
7032
  init_restart();
@@ -6855,7 +7044,7 @@ __export(src_exports, {
6855
7044
  defineConfig: () => defineConfig,
6856
7045
  loadConfig: () => loadConfig,
6857
7046
  loadEnv: () => loadEnv,
6858
- logger: () => import_shared61.logger,
7047
+ logger: () => import_shared63.logger,
6859
7048
  mergeRsbuildConfig: () => mergeRsbuildConfig,
6860
7049
  rspack: () => import_core10.rspack,
6861
7050
  version: () => version
@@ -6887,8 +7076,9 @@ __export(internal_exports, {
6887
7076
  // src/cli/commands.ts
6888
7077
  var import_node_fs5 = require("fs");
6889
7078
  var import_node_path28 = require("path");
6890
- var import_shared59 = require("@rsbuild/shared");
7079
+ var import_shared61 = require("@rsbuild/shared");
6891
7080
  var import_commander = require("../compiled/commander/index.js");
7081
+ init_helpers();
6892
7082
  init_init();
6893
7083
  var applyCommonOptions = (command) => {
6894
7084
  command.option(
@@ -6903,7 +7093,7 @@ var applyServerOptions = (command) => {
6903
7093
  command.option("-o --open [url]", "open the page in browser on startup").option("--port <port>", "specify a port number for server to listen").option("--host <host>", "specify the host that the server listens to");
6904
7094
  };
6905
7095
  function runCli() {
6906
- import_commander.program.name("rsbuild").usage("<command> [options]").version("0.7.1");
7096
+ import_commander.program.name("rsbuild").usage("<command> [options]").version("0.7.3");
6907
7097
  const devCommand = import_commander.program.command("dev");
6908
7098
  const buildCommand = import_commander.program.command("build");
6909
7099
  const previewCommand = import_commander.program.command("preview");
@@ -6917,8 +7107,8 @@ function runCli() {
6917
7107
  const rsbuild = await init({ cliOptions: options });
6918
7108
  await rsbuild?.startDevServer();
6919
7109
  } catch (err) {
6920
- import_shared59.logger.error("Failed to start dev server.");
6921
- import_shared59.logger.error(err);
7110
+ import_shared61.logger.error("Failed to start dev server.");
7111
+ import_shared61.logger.error(err);
6922
7112
  process.exit(1);
6923
7113
  }
6924
7114
  });
@@ -6929,25 +7119,35 @@ function runCli() {
6929
7119
  watch: options.watch
6930
7120
  });
6931
7121
  } catch (err) {
6932
- import_shared59.logger.error("Failed to build.");
6933
- import_shared59.logger.error(err);
7122
+ import_shared61.logger.error("Failed to build.");
7123
+ import_shared61.logger.error(err);
6934
7124
  process.exit(1);
6935
7125
  }
6936
7126
  });
6937
7127
  previewCommand.description("preview the production build locally").action(async (options) => {
6938
7128
  try {
6939
7129
  const rsbuild = await init({ cliOptions: options });
6940
- if (rsbuild && !(0, import_node_fs5.existsSync)(rsbuild.context.distPath)) {
6941
- throw new Error(
6942
- `The output directory ${import_shared59.color.yellow(
6943
- rsbuild.context.distPath
6944
- )} does not exist, please build the project before previewing.`
6945
- );
7130
+ if (rsbuild) {
7131
+ const { distPath } = rsbuild.context;
7132
+ if (!(0, import_node_fs5.existsSync)(distPath)) {
7133
+ throw new Error(
7134
+ `The output directory ${import_shared61.color.yellow(
7135
+ distPath
7136
+ )} does not exist, please build the project before previewing.`
7137
+ );
7138
+ }
7139
+ if (isEmptyDir(distPath)) {
7140
+ throw new Error(
7141
+ `The output directory ${import_shared61.color.yellow(
7142
+ distPath
7143
+ )} is empty, please build the project before previewing.`
7144
+ );
7145
+ }
6946
7146
  }
6947
7147
  await rsbuild?.preview();
6948
7148
  } catch (err) {
6949
- import_shared59.logger.error("Failed to start preview server.");
6950
- import_shared59.logger.error(err);
7149
+ import_shared61.logger.error("Failed to start preview server.");
7150
+ import_shared61.logger.error(err);
6951
7151
  process.exit(1);
6952
7152
  }
6953
7153
  });
@@ -6961,8 +7161,8 @@ function runCli() {
6961
7161
  writeToDisk: true
6962
7162
  });
6963
7163
  } catch (err) {
6964
- import_shared59.logger.error("Failed to inspect config.");
6965
- import_shared59.logger.error(err);
7164
+ import_shared61.logger.error("Failed to inspect config.");
7165
+ import_shared61.logger.error(err);
6966
7166
  process.exit(1);
6967
7167
  }
6968
7168
  });
@@ -6970,7 +7170,7 @@ function runCli() {
6970
7170
  }
6971
7171
 
6972
7172
  // src/cli/prepare.ts
6973
- var import_shared60 = require("@rsbuild/shared");
7173
+ var import_shared62 = require("@rsbuild/shared");
6974
7174
  function initNodeEnv() {
6975
7175
  if (!process.env.NODE_ENV) {
6976
7176
  const command = process.argv[2];
@@ -6983,7 +7183,7 @@ function prepareCli() {
6983
7183
  if (!npm_execpath || npm_execpath.includes("npx-cli.js") || npm_execpath.includes(".bun")) {
6984
7184
  console.log();
6985
7185
  }
6986
- import_shared60.logger.greet(` ${`Rsbuild v${"0.7.1"}`}
7186
+ import_shared62.logger.greet(` ${`Rsbuild v${"0.7.3"}`}
6987
7187
  `);
6988
7188
  }
6989
7189
 
@@ -7006,10 +7206,10 @@ init_prodServer();
7006
7206
  init_loadEnv();
7007
7207
  init_createRsbuild();
7008
7208
  init_config();
7009
- var import_shared61 = require("@rsbuild/shared");
7209
+ var import_shared63 = require("@rsbuild/shared");
7010
7210
  init_mergeConfig();
7011
7211
  init_constants();
7012
- var version = "0.7.1";
7212
+ var version = "0.7.3";
7013
7213
  // Annotate the CommonJS export names for ESM import in node:
7014
7214
  0 && (module.exports = {
7015
7215
  PLUGIN_CSS_NAME,