@modern-js/uni-builder 2.55.0 → 2.56.1

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/README.md CHANGED
@@ -1,19 +1,26 @@
1
1
  <p align="center">
2
- <a href="https://rsbuild.dev" target="blank"><img src="https://github.com/web-infra-dev/rsbuild/assets/7237365/84abc13e-b620-468f-a90b-dbf28e7e9427" alt="Rsbuild Logo" /></a>
2
+ <a href="https://modernjs.dev" target="blank"><img src="https://lf3-static.bytednsdoc.com/obj/eden-cn/ylaelkeh7nuhfnuhf/modernjs-cover.png" width="300" alt="Modern.js Logo" /></a>
3
3
  </p>
4
4
 
5
- # Rsbuild
5
+ <h1 align="center">Modern.js</h1>
6
6
 
7
- Unleash the power of Rspack with the out-of-the-box build tool.
7
+ <p align="center">
8
+ A Progressive React Framework for modern web development.
9
+ </p>
10
+
11
+ ## Getting Started
12
+
13
+ Please follow [Quick Start](https://modernjs.dev/en/guides/get-started/quick-start) to get started with Modern.js.
8
14
 
9
15
  ## Documentation
10
16
 
11
- https://rsbuild.dev/
17
+ - [English Documentation](https://modernjs.dev/en/)
18
+ - [中文文档](https://modernjs.dev)
12
19
 
13
20
  ## Contributing
14
21
 
15
- Please read the [Contributing Guide](https://github.com/web-infra-dev/rsbuild/blob/main/CONTRIBUTING.md).
22
+ Please read the [Contributing Guide](https://github.com/web-infra-dev/modern.js/blob/main/CONTRIBUTING.md).
16
23
 
17
24
  ## License
18
25
 
19
- Rsbuild is [MIT licensed](https://github.com/web-infra-dev/rsbuild/blob/main/LICENSE).
26
+ Modern.js is [MIT licensed](https://github.com/web-infra-dev/modern.js/blob/main/LICENSE).
package/dist/index.d.ts CHANGED
@@ -4,10 +4,9 @@ import type { CreateUniBuilderOptions } from './types';
4
4
  import HtmlWebpackPlugin from 'html-webpack-plugin';
5
5
  export { HtmlWebpackPlugin };
6
6
  export type { CreateUniBuilderOptions, UniBuilderInstance, UniBuilderWebpackInstance, };
7
- export type { CacheGroup } from '@rsbuild/shared';
8
7
  export type { UniBuilderConfig, UniBuilderPlugin, BundlerType, MetaOptions, Stats, MultiStats, RspackConfig, } from './types';
9
8
  export type { StartDevServerOptions } from './shared/devServer';
10
9
  export declare function createUniBuilder(options: CreateUniBuilderOptions): Promise<UniBuilderInstance>;
11
- export { logger, type ConfigChain, type RsbuildPlugin, type ChainIdentifier, type RspackChain, type Rspack, type RsbuildContext, type RsbuildConfig, type RsbuildTarget, type NormalizedConfig, } from '@rsbuild/core';
10
+ export { logger, type ConfigChain, type RsbuildPlugin, type ChainIdentifier, type RspackChain, type Rspack, type RsbuildContext, type RsbuildConfig, type RsbuildTarget, type NormalizedConfig, type CacheGroup, } from '@rsbuild/core';
12
11
  export type { webpack, WebpackConfig } from '@rsbuild/webpack';
13
- export { RUNTIME_CHUNK_NAME, isHtmlDisabled, castArray } from './shared/utils';
12
+ export { RUNTIME_CHUNK_NAME, SERVICE_WORKER_ENVIRONMENT_NAME, isHtmlDisabled, castArray, } from './shared/utils';
package/dist/index.js CHANGED
@@ -30,6 +30,7 @@ var src_exports = {};
30
30
  __export(src_exports, {
31
31
  HtmlWebpackPlugin: () => import_html_webpack_plugin.default,
32
32
  RUNTIME_CHUNK_NAME: () => import_utils.RUNTIME_CHUNK_NAME,
33
+ SERVICE_WORKER_ENVIRONMENT_NAME: () => import_utils.SERVICE_WORKER_ENVIRONMENT_NAME,
33
34
  castArray: () => import_utils.castArray,
34
35
  createUniBuilder: () => createUniBuilder,
35
36
  isHtmlDisabled: () => import_utils.isHtmlDisabled,
@@ -48,6 +49,7 @@ async function createUniBuilder(options) {
48
49
  0 && (module.exports = {
49
50
  HtmlWebpackPlugin,
50
51
  RUNTIME_CHUNK_NAME,
52
+ SERVICE_WORKER_ENVIRONMENT_NAME,
51
53
  castArray,
52
54
  createUniBuilder,
53
55
  isHtmlDisabled,
@@ -35,6 +35,7 @@ module.exports = __toCommonJS(rspack_exports);
35
35
  var import_core = require("@rsbuild/core");
36
36
  var import_parseCommonConfig = require("../shared/parseCommonConfig");
37
37
  var import_compatLegacyPlugin = require("../shared/compatLegacyPlugin");
38
+ var import_utils = require("../shared/utils");
38
39
  async function parseConfig(uniBuilderConfig, options) {
39
40
  var _uniBuilderConfig_experiments, _uniBuilderConfig_tools, _uniBuilderConfig_tools1;
40
41
  const { rsbuildConfig, rsbuildPlugins } = await (0, import_parseCommonConfig.parseCommonConfig)(uniBuilderConfig, options);
@@ -50,9 +51,13 @@ async function parseConfig(uniBuilderConfig, options) {
50
51
  }), pluginBabelPost());
51
52
  }
52
53
  if (((_uniBuilderConfig_tools1 = uniBuilderConfig.tools) === null || _uniBuilderConfig_tools1 === void 0 ? void 0 : _uniBuilderConfig_tools1.styledComponents) !== false) {
53
- var _uniBuilderConfig_tools3;
54
+ var _uniBuilderConfig_tools3, _uniBuilderConfig_environments;
54
55
  const { pluginStyledComponents } = await Promise.resolve().then(() => __toESM(require("@rsbuild/plugin-styled-components")));
55
- rsbuildPlugins.push(pluginStyledComponents((_uniBuilderConfig_tools3 = uniBuilderConfig.tools) === null || _uniBuilderConfig_tools3 === void 0 ? void 0 : _uniBuilderConfig_tools3.styledComponents));
56
+ const options2 = ((_uniBuilderConfig_tools3 = uniBuilderConfig.tools) === null || _uniBuilderConfig_tools3 === void 0 ? void 0 : _uniBuilderConfig_tools3.styledComponents) || {};
57
+ if ((_uniBuilderConfig_environments = uniBuilderConfig.environments) === null || _uniBuilderConfig_environments === void 0 ? void 0 : _uniBuilderConfig_environments[import_utils.SERVICE_WORKER_ENVIRONMENT_NAME]) {
58
+ options2.ssr = true;
59
+ }
60
+ rsbuildPlugins.push(pluginStyledComponents(options2));
56
61
  }
57
62
  return {
58
63
  rsbuildConfig,
@@ -37,10 +37,10 @@ const pluginBabelPost = () => ({
37
37
  name: "uni-builder:babel-post",
38
38
  setup(api) {
39
39
  api.modifyBundlerChain({
40
- handler: async (chain, { CHAIN_ID }) => {
40
+ handler: async (chain, { CHAIN_ID, environment }) => {
41
41
  if (chain.module.rules.get(CHAIN_ID.RULE.JS)) {
42
42
  const { cacheIdentifier, ...babelLoaderOptions } = chain.module.rule(CHAIN_ID.RULE.JS).use(CHAIN_ID.USE.BABEL).get("options");
43
- const config = api.getNormalizedConfig();
43
+ const { config } = environment;
44
44
  if (babelLoaderOptions && (0, import_isEqual.default)((0, import_plugin_babel.getDefaultBabelOptions)(config, api.context), babelLoaderOptions)) {
45
45
  chain.module.rule(CHAIN_ID.RULE.JS).uses.delete(CHAIN_ID.USE.BABEL);
46
46
  }
@@ -34,14 +34,15 @@ function compatLegacyPlugin(plugin, extraInfo) {
34
34
  get(target, prop) {
35
35
  switch (prop) {
36
36
  case "target":
37
- addDeprecatedWarning(plugin.name, "context.target", "context.targets");
38
- return target.targets;
37
+ throw new Error(`Plugin(${plugin.name})'s api 'target' is deprecated, please use 'environment.config.output.target' instead.`);
39
38
  case "srcPath":
40
39
  addDeprecatedWarning(plugin.name, "context.srcPath");
41
40
  return (0, import_path.join)(extraInfo.cwd, "src");
42
41
  case "framework":
43
42
  addDeprecatedWarning(plugin.name, "context.framework");
44
43
  return "";
44
+ case "entry":
45
+ throw new Error(`Plugin(${plugin.name})'s api 'entry' is deprecated, please use 'environment.entry' instead.`);
45
46
  default: {
46
47
  if (prop in target) {
47
48
  return target[prop];
@@ -60,7 +60,7 @@ const getServerOptions = (builderConfig) => {
60
60
  };
61
61
  const transformToRsbuildServerOptions = (dev, devServer) => {
62
62
  var _newDevServerConfig_devMiddleware, _newDevServerConfig_before, _newDevServerConfig_after;
63
- const { port = 8080, host, https, ...devConfig } = dev;
63
+ const { port = 8080, host, https, startUrl, beforeStartUrl, ...devConfig } = dev;
64
64
  var _dev_hmr;
65
65
  const newDevServerConfig = (0, import_utils.applyOptionsChain)({
66
66
  devMiddleware: {
@@ -71,15 +71,19 @@ const transformToRsbuildServerOptions = (dev, devServer) => {
71
71
  client: {
72
72
  path: "/webpack-hmr",
73
73
  overlay: false,
74
+ port: "<port>",
74
75
  ...devConfig.client || {}
75
76
  }
76
77
  }, devServer, {}, import_ts_deepmerge.merge);
77
78
  const rsbuildDev = {
78
- ...devConfig,
79
79
  writeToDisk: (_newDevServerConfig_devMiddleware = newDevServerConfig.devMiddleware) === null || _newDevServerConfig_devMiddleware === void 0 ? void 0 : _newDevServerConfig_devMiddleware.writeToDisk,
80
80
  hmr: newDevServerConfig.hot,
81
- client: newDevServerConfig.client,
82
- liveReload: newDevServerConfig.liveReload
81
+ liveReload: newDevServerConfig.liveReload,
82
+ ...devConfig,
83
+ client: {
84
+ ...newDevServerConfig.client,
85
+ ...devConfig.client || {}
86
+ }
83
87
  };
84
88
  if (dev.progressBar === void 0) {
85
89
  rsbuildDev.progressBar = true;
@@ -111,6 +115,12 @@ const transformToRsbuildServerOptions = (dev, devServer) => {
111
115
  host,
112
116
  https: https ? https : void 0
113
117
  };
118
+ if (!(0, import_utils.isProd)() && startUrl) {
119
+ server.open = beforeStartUrl ? {
120
+ target: startUrl === true ? "//localhost:<port>" : startUrl,
121
+ before: beforeStartUrl
122
+ } : startUrl;
123
+ }
114
124
  return {
115
125
  dev: rsbuildDev,
116
126
  server
@@ -47,8 +47,10 @@ var import_devtools = require("./plugins/devtools");
47
47
  var import_emitRouteFile = require("./plugins/emitRouteFile");
48
48
  var import_antd = require("./plugins/antd");
49
49
  var import_arco = require("./plugins/arco");
50
+ var import_environmentDefaults = require("./plugins/environmentDefaults");
50
51
  var import_plugin_sass = require("@rsbuild/plugin-sass");
51
52
  var import_plugin_less = require("@rsbuild/plugin-less");
53
+ var import_htmlMinify = require("./plugins/htmlMinify");
52
54
  var import_devServer = require("./devServer");
53
55
  var import_utils2 = require("./utils");
54
56
  const CSS_MODULES_REGEX = /\.modules?\.\w+$/i;
@@ -78,9 +80,9 @@ const isUseCssSourceMap = (disableSourceMap = {}) => {
78
80
  };
79
81
  async function parseCommonConfig(uniBuilderConfig, options) {
80
82
  var _uniBuilderConfig_output, _uniBuilderConfig_tools;
81
- var _output, _output_distPath, _output_distPath1, _output1, _extraConfig, _html, _extraConfig1;
82
- const { cwd, frameworkConfigPath, entry, target } = options;
83
- const { plugins: [...plugins] = [], performance: { ...performanceConfig } = {}, output: { disableFilenameHash, enableLatestDecorators, cssModuleLocalIdentName, enableInlineScripts, disableCssExtract, enableInlineStyles, enableCssModuleTSDeclaration, disableCssModuleExtension, disableTsChecker, disableSvgr, svgDefaultExport, assetsRetry, enableAssetFallback, enableAssetManifest, disableSourceMap, convertToRem, disableMinimize, polyfill, dataUriLimit = 1e4, ...outputConfig } = {}, html: { disableHtmlFolder, metaByEntries, titleByEntries, faviconByEntries, injectByEntries, templateByEntries, templateParametersByEntries, tagsByEntries, tags, ...htmlConfig } = {}, source: { alias, globalVars, resolveMainFields, resolveExtensionPrefix, ...sourceConfig } = {}, dev, security: { checkSyntax, sri, ...securityConfig } = {}, tools: { devServer, tsChecker, minifyCss, less, sass, ...toolsConfig } = {} } = uniBuilderConfig;
83
+ var _output_distPath, _output, _extraConfig, _html, _extraConfig1;
84
+ const { frameworkConfigPath } = options;
85
+ const { plugins: [...plugins] = [], performance: { ...performanceConfig } = {}, output: { disableFilenameHash, enableLatestDecorators, cssModuleLocalIdentName, enableInlineScripts, disableCssExtract, enableInlineStyles, enableCssModuleTSDeclaration, disableCssModuleExtension, disableTsChecker, disableSvgr, svgDefaultExport, assetsRetry, enableAssetFallback, enableAssetManifest, disableSourceMap, convertToRem, disableMinimize, polyfill, dataUriLimit = 1e4, distPath = {}, ...outputConfig } = {}, html: { disableHtmlFolder, metaByEntries, titleByEntries, faviconByEntries, injectByEntries, templateByEntries, templateParametersByEntries, tagsByEntries, tags, ...htmlConfig } = {}, source: { alias, globalVars, resolveMainFields, resolveExtensionPrefix, ...sourceConfig } = {}, dev, security: { checkSyntax, sri, ...securityConfig } = {}, tools: { devServer, tsChecker, minifyCss, less, sass, htmlPlugin, ...toolsConfig } = {}, environments = {} } = uniBuilderConfig;
84
86
  const rsbuildConfig = {
85
87
  plugins,
86
88
  output: {
@@ -95,39 +97,45 @@ async function parseCommonConfig(uniBuilderConfig, options) {
95
97
  performance: performanceConfig,
96
98
  html: htmlConfig,
97
99
  tools: toolsConfig,
98
- security: securityConfig
100
+ security: securityConfig,
101
+ environments
99
102
  };
103
+ rsbuildConfig.tools.htmlPlugin = htmlPlugin;
100
104
  const { html = {}, output = {}, source = {} } = rsbuildConfig;
101
105
  if (enableLatestDecorators) {
102
106
  source.decorators = {
103
107
  version: "2022-03"
104
108
  };
109
+ } else {
110
+ var _source;
111
+ var _decorators;
112
+ (_decorators = (_source = source).decorators) !== null && _decorators !== void 0 ? _decorators : _source.decorators = {
113
+ version: "legacy"
114
+ };
105
115
  }
106
116
  if (disableMinimize) {
107
- var _output2;
108
- (_output2 = output).minify || (_output2.minify = false);
117
+ var _output1;
118
+ (_output1 = output).minify || (_output1.minify = false);
109
119
  }
110
120
  if (cssModuleLocalIdentName) {
111
- var _output3;
112
- (_output3 = output).cssModules || (_output3.cssModules = {});
121
+ var _output2;
122
+ (_output2 = output).cssModules || (_output2.cssModules = {});
113
123
  output.cssModules.localIdentName = cssModuleLocalIdentName;
114
124
  }
115
125
  if (isUseCssSourceMap(disableSourceMap)) {
116
- var _output4;
117
- (_output4 = output).sourceMap || (_output4.sourceMap = {});
126
+ var _output3;
127
+ (_output3 = output).sourceMap || (_output3.sourceMap = {});
118
128
  output.sourceMap.css = true;
119
129
  }
120
- var _distPath;
121
- (_distPath = (_output = output).distPath) !== null && _distPath !== void 0 ? _distPath : _output.distPath = {};
130
+ const { server: _server, worker, ...rsbuildDistPath } = distPath;
131
+ output.distPath = rsbuildDistPath;
122
132
  var _html1;
123
133
  (_html1 = (_output_distPath = output.distPath).html) !== null && _html1 !== void 0 ? _html1 : _output_distPath.html = "html";
124
- var _server;
125
- (_server = (_output_distPath1 = output.distPath).server) !== null && _server !== void 0 ? _server : _output_distPath1.server = "bundles";
126
134
  var _polyfill;
127
- (_polyfill = (_output1 = output).polyfill) !== null && _polyfill !== void 0 ? _polyfill : _output1.polyfill = "entry";
135
+ (_polyfill = (_output = output).polyfill) !== null && _polyfill !== void 0 ? _polyfill : _output.polyfill = "entry";
128
136
  if (disableCssModuleExtension) {
129
- var _output5, _output_cssModules;
130
- (_output5 = output).cssModules || (_output5.cssModules = {});
137
+ var _output4, _output_cssModules;
138
+ (_output4 = output).cssModules || (_output4.cssModules = {});
131
139
  var _auto;
132
140
  (_auto = (_output_cssModules = output.cssModules).auto) !== null && _auto !== void 0 ? _auto : _output_cssModules.auto = isLooseCssModules;
133
141
  }
@@ -137,15 +145,6 @@ async function parseCommonConfig(uniBuilderConfig, options) {
137
145
  if (disableCssExtract) {
138
146
  output.injectStyles = disableCssExtract;
139
147
  }
140
- const targets = Array.isArray(target) ? target : [
141
- target || "web"
142
- ];
143
- output.targets = targets;
144
- const overrideBrowserslist = {};
145
- for (const target2 of targets) {
146
- overrideBrowserslist[target2] = await (0, import_utils2.getBrowserslistWithDefault)(cwd, uniBuilderConfig, target2);
147
- }
148
- output.overrideBrowserslist = overrideBrowserslist;
149
148
  if (enableInlineStyles) {
150
149
  output.inlineStyles = enableInlineStyles;
151
150
  }
@@ -217,12 +216,6 @@ async function parseCommonConfig(uniBuilderConfig, options) {
217
216
  rsbuildConfig.dev = removeUndefinedKey(RsbuildDev);
218
217
  rsbuildConfig.html = html;
219
218
  rsbuildConfig.output = output;
220
- if (entry) {
221
- var _rsbuildConfig;
222
- var _source;
223
- (_source = (_rsbuildConfig = rsbuildConfig).source) !== null && _source !== void 0 ? _source : _rsbuildConfig.source = {};
224
- rsbuildConfig.source.entry = entry;
225
- }
226
219
  const rsbuildPlugins = [
227
220
  (0, import_splitChunk.pluginSplitChunks)(),
228
221
  (0, import_globalVars.pluginGlobalVars)(globalVars),
@@ -239,7 +232,9 @@ async function parseCommonConfig(uniBuilderConfig, options) {
239
232
  }),
240
233
  (0, import_plugin_less.pluginLess)({
241
234
  lessLoaderOptions: less
242
- })
235
+ }),
236
+ (0, import_environmentDefaults.pluginEnvironmentDefaults)(distPath),
237
+ (0, import_htmlMinify.pluginHtmlMinifierTerser)()
243
238
  ];
244
239
  if (checkSyntax) {
245
240
  const { pluginCheckSyntax } = await Promise.resolve().then(() => __toESM(require("@rsbuild/plugin-check-syntax")));
@@ -304,7 +299,7 @@ async function parseCommonConfig(uniBuilderConfig, options) {
304
299
  rsbuildPlugins.push((0, import_plugin_css_minimizer.pluginCssMinimizer)({
305
300
  pluginOptions: minifyCss
306
301
  }));
307
- targets.includes("web") && rsbuildPlugins.push((0, import_postcssLegacy.pluginPostcssLegacy)(overrideBrowserslist.web));
302
+ rsbuildPlugins.push((0, import_postcssLegacy.pluginPostcssLegacy)());
308
303
  if (enableAssetManifest) {
309
304
  const { pluginManifest } = await Promise.resolve().then(() => __toESM(require("./plugins/manifest")));
310
305
  rsbuildPlugins.push(pluginManifest());
@@ -38,24 +38,19 @@ const getAntdMajorVersion = (appDirectory) => {
38
38
  const pluginAntd = () => ({
39
39
  name: "uni-builder:antd",
40
40
  setup(api) {
41
- api.modifyRsbuildConfig((rsbuildConfig) => {
41
+ api.modifyEnvironmentConfig((rsbuildConfig, { name }) => {
42
42
  var _rsbuildConfig_source_transformImport;
43
- var _rsbuildConfig;
44
- var _source;
45
- (_source = (_rsbuildConfig = rsbuildConfig).source) !== null && _source !== void 0 ? _source : _rsbuildConfig.source = {};
46
43
  if (rsbuildConfig.source.transformImport === false || ((_rsbuildConfig_source_transformImport = rsbuildConfig.source.transformImport) === null || _rsbuildConfig_source_transformImport === void 0 ? void 0 : _rsbuildConfig_source_transformImport.some((item) => item.libraryName === "antd"))) {
47
44
  return;
48
45
  }
46
+ const useServerEnvironment = (0, import_utils.isServerEnvironment)(rsbuildConfig.output.target, name);
49
47
  const antdMajorVersion = getAntdMajorVersion(api.context.rootPath);
50
48
  if (antdMajorVersion && antdMajorVersion < 5) {
51
- var _rsbuildConfig1;
52
- var _source1;
53
- (_source1 = (_rsbuildConfig1 = rsbuildConfig).source) !== null && _source1 !== void 0 ? _source1 : _rsbuildConfig1.source = {};
54
49
  rsbuildConfig.source.transformImport = [
55
50
  ...rsbuildConfig.source.transformImport || [],
56
51
  {
57
52
  libraryName: "antd",
58
- libraryDirectory: (0, import_utils.isServerTarget)(api.context.targets) ? "lib" : "es",
53
+ libraryDirectory: useServerEnvironment ? "lib" : "es",
59
54
  style: true
60
55
  }
61
56
  ];
@@ -22,23 +22,22 @@ __export(arco_exports, {
22
22
  });
23
23
  module.exports = __toCommonJS(arco_exports);
24
24
  var import_utils = require("@modern-js/utils");
25
- var import_utils2 = require("../../shared/utils");
25
+ var import_utils2 = require("../utils");
26
26
  const pluginArco = () => ({
27
27
  name: "uni-builder:arco",
28
28
  setup(api) {
29
29
  const ARCO_NAME = "@arco-design/web-react";
30
30
  const ARCO_ICON = `${ARCO_NAME}/icon`;
31
- api.modifyRsbuildConfig((rsbuildConfig) => {
32
- var _rsbuildConfig;
33
- const { transformImport = [] } = rsbuildConfig.source || {};
31
+ api.modifyEnvironmentConfig((rsbuildConfig, { name }) => {
32
+ const { transformImport = [] } = rsbuildConfig.source;
34
33
  if (transformImport === false || !(0, import_utils.isPackageInstalled)(ARCO_NAME, api.context.rootPath)) {
35
34
  return;
36
35
  }
37
- const isUseSSR = (0, import_utils2.isServerTarget)(api.context.targets);
36
+ const useServerEnvironment = (0, import_utils2.isServerEnvironment)(rsbuildConfig.output.target, name);
38
37
  if (!(transformImport === null || transformImport === void 0 ? void 0 : transformImport.some((item) => item.libraryName === ARCO_NAME))) {
39
38
  transformImport.push({
40
39
  libraryName: ARCO_NAME,
41
- libraryDirectory: isUseSSR ? "lib" : "es",
40
+ libraryDirectory: useServerEnvironment ? "lib" : "es",
42
41
  camelToDashComponentName: false,
43
42
  style: true
44
43
  });
@@ -46,11 +45,10 @@ const pluginArco = () => ({
46
45
  if (!(transformImport === null || transformImport === void 0 ? void 0 : transformImport.some((item) => item.libraryName === ARCO_ICON))) {
47
46
  transformImport.push({
48
47
  libraryName: ARCO_ICON,
49
- libraryDirectory: isUseSSR ? "react-icon-cjs" : "react-icon",
48
+ libraryDirectory: useServerEnvironment ? "react-icon-cjs" : "react-icon",
50
49
  camelToDashComponentName: false
51
50
  });
52
51
  }
53
- (_rsbuildConfig = rsbuildConfig).source || (_rsbuildConfig.source = {});
54
52
  rsbuildConfig.source.transformImport = transformImport;
55
53
  });
56
54
  }
@@ -40,10 +40,15 @@ async function isFileExists(file) {
40
40
  const pluginEmitRouteFile = () => ({
41
41
  name: "uni-builder:emit-route-file",
42
42
  setup(api) {
43
- api.onBeforeStartDevServer(async () => {
43
+ api.onBeforeStartDevServer(async ({ environments }) => {
44
44
  const { fs: fs2, ROUTE_SPEC_FILE } = await Promise.resolve().then(() => __toESM(require("@modern-js/utils")));
45
45
  const routeFilePath = (0, import_node_path.join)(api.context.distPath, ROUTE_SPEC_FILE);
46
- const htmlPaths = api.getHTMLPaths();
46
+ const htmlPaths = Object.values(environments).reduce((prev, curr) => {
47
+ return {
48
+ ...prev,
49
+ ...curr.htmlPaths
50
+ };
51
+ }, {});
47
52
  const routesInfo = Object.entries(htmlPaths).map(([entryName, filename], index) => ({
48
53
  urlPath: index === 0 ? "/" : `/${entryName}`,
49
54
  entryName,
@@ -0,0 +1,3 @@
1
+ import type { RsbuildPlugin } from '@rsbuild/core';
2
+ import type { DistPath } from '../../types';
3
+ export declare const pluginEnvironmentDefaults: (distPath?: DistPath) => RsbuildPlugin;
@@ -0,0 +1,92 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var environmentDefaults_exports = {};
20
+ __export(environmentDefaults_exports, {
21
+ pluginEnvironmentDefaults: () => pluginEnvironmentDefaults
22
+ });
23
+ module.exports = __toCommonJS(environmentDefaults_exports);
24
+ var import_node_path = require("node:path");
25
+ var import_utils = require("../utils");
26
+ const pluginEnvironmentDefaults = (distPath = {}) => ({
27
+ name: "uni-builder:environment-defaults-plugin",
28
+ setup(api) {
29
+ api.modifyRsbuildConfig((config, { mergeRsbuildConfig }) => {
30
+ var _config_environments, _config_environments1;
31
+ const compatConfig = {};
32
+ if ((_config_environments = config.environments) === null || _config_environments === void 0 ? void 0 : _config_environments[import_utils.SERVICE_WORKER_ENVIRONMENT_NAME]) {
33
+ var _compatConfig;
34
+ var _environments;
35
+ (_environments = (_compatConfig = compatConfig).environments) !== null && _environments !== void 0 ? _environments : _compatConfig.environments = {};
36
+ compatConfig.environments[import_utils.SERVICE_WORKER_ENVIRONMENT_NAME] = {
37
+ output: {
38
+ distPath: {
39
+ root: (0, import_node_path.join)(distPath.root || "dist", distPath.worker || "worker"),
40
+ js: "",
41
+ css: "",
42
+ jsAsync: "",
43
+ cssAsync: ""
44
+ },
45
+ filename: {
46
+ js: "[name].js"
47
+ }
48
+ }
49
+ };
50
+ }
51
+ if ((_config_environments1 = config.environments) === null || _config_environments1 === void 0 ? void 0 : _config_environments1.node) {
52
+ var _compatConfig1;
53
+ var _environments1;
54
+ (_environments1 = (_compatConfig1 = compatConfig).environments) !== null && _environments1 !== void 0 ? _environments1 : _compatConfig1.environments = {};
55
+ compatConfig.environments.node = {
56
+ output: {
57
+ // no need to emit assets for SSR bundles
58
+ emitAssets: false,
59
+ distPath: {
60
+ root: (0, import_node_path.join)(distPath.root || "dist", distPath.server || "bundles"),
61
+ js: "",
62
+ css: "",
63
+ jsAsync: "",
64
+ cssAsync: ""
65
+ }
66
+ }
67
+ };
68
+ }
69
+ return compatConfig.environments ? mergeRsbuildConfig(compatConfig, config) : config;
70
+ });
71
+ api.modifyRsbuildConfig({
72
+ handler: (config) => {
73
+ const environmentNameOrder = [
74
+ "web",
75
+ "node",
76
+ "serviceWorker"
77
+ ];
78
+ config.environments = Object.fromEntries(Object.entries(config.environments).sort((a1, a2) => environmentNameOrder.includes(a1[0]) ? environmentNameOrder.indexOf(a1[0]) - environmentNameOrder.indexOf(a2[0]) : 1));
79
+ },
80
+ order: "post"
81
+ });
82
+ api.modifyEnvironmentConfig(async (config, { name }) => {
83
+ var _config_output;
84
+ var _overrideBrowserslist;
85
+ (_overrideBrowserslist = (_config_output = config.output).overrideBrowserslist) !== null && _overrideBrowserslist !== void 0 ? _overrideBrowserslist : _config_output.overrideBrowserslist = await (0, import_utils.getBrowserslistWithDefault)(api.context.rootPath, config, name === import_utils.SERVICE_WORKER_ENVIRONMENT_NAME ? "node" : config.output.target);
86
+ });
87
+ }
88
+ });
89
+ // Annotate the CommonJS export names for ESM import in node:
90
+ 0 && (module.exports = {
91
+ pluginEnvironmentDefaults
92
+ });
@@ -70,8 +70,8 @@ const resourceRuleFallback = (rules = []) => {
70
70
  const pluginFallback = () => ({
71
71
  name: "uni-builder:fallback",
72
72
  setup(api) {
73
- api.modifyBundlerChain((chain) => {
74
- const rsbuildConfig = api.getNormalizedConfig();
73
+ api.modifyBundlerChain((chain, { environment }) => {
74
+ const rsbuildConfig = environment.config;
75
75
  const { distPath, filename } = rsbuildConfig.output;
76
76
  const distDir = distPath.media;
77
77
  var _filename_media;
@@ -0,0 +1,2 @@
1
+ import type { RsbuildPlugin } from '@rsbuild/core';
2
+ export declare const pluginHtmlMinifierTerser: () => RsbuildPlugin;
@@ -0,0 +1,140 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var htmlMinify_exports = {};
30
+ __export(htmlMinify_exports, {
31
+ pluginHtmlMinifierTerser: () => pluginHtmlMinifierTerser
32
+ });
33
+ module.exports = __toCommonJS(htmlMinify_exports);
34
+ var import_ts_deepmerge = require("ts-deepmerge");
35
+ function applyRemoveConsole(options, config) {
36
+ const { removeConsole } = config.performance;
37
+ const compressOptions = typeof options.compress === "boolean" ? {} : options.compress || {};
38
+ if (removeConsole === true) {
39
+ options.compress = {
40
+ ...compressOptions,
41
+ drop_console: true
42
+ };
43
+ } else if (Array.isArray(removeConsole)) {
44
+ const pureFuncs = removeConsole.map((method) => `console.${method}`);
45
+ options.compress = {
46
+ ...compressOptions,
47
+ pure_funcs: pureFuncs
48
+ };
49
+ }
50
+ return options;
51
+ }
52
+ function getTerserMinifyOptions(config) {
53
+ const options = {
54
+ mangle: {
55
+ safari10: true
56
+ },
57
+ format: {
58
+ ascii_only: config.output.charset === "ascii"
59
+ }
60
+ };
61
+ if (config.output.legalComments === "none") {
62
+ var _options;
63
+ (_options = options).format || (_options.format = {});
64
+ options.format.comments = false;
65
+ }
66
+ const finalOptions = applyRemoveConsole(options, config);
67
+ return finalOptions;
68
+ }
69
+ function getMinifyOptions(config) {
70
+ const minifyJS = getTerserMinifyOptions(config);
71
+ return {
72
+ removeComments: false,
73
+ useShortDoctype: true,
74
+ keepClosingSlash: true,
75
+ collapseWhitespace: true,
76
+ removeRedundantAttributes: true,
77
+ removeScriptTypeAttributes: true,
78
+ removeStyleLinkTypeAttributes: true,
79
+ removeEmptyAttributes: true,
80
+ minifyJS,
81
+ minifyCSS: true,
82
+ minifyURLs: true
83
+ };
84
+ }
85
+ const pluginHtmlMinifierTerser = () => ({
86
+ name: "uni-builder:plugin-html-minifier-terser",
87
+ setup(api) {
88
+ api.modifyBundlerChain(async (chain, { isProd, environment }) => {
89
+ const { output, tools: { htmlPlugin } } = environment.config;
90
+ if (!isProd || output.minify === false || htmlPlugin === false) {
91
+ return;
92
+ }
93
+ const { minify } = await Promise.resolve().then(() => __toESM(require("html-minifier-terser")));
94
+ const pluginRecord = chain.plugins.entries();
95
+ const minifyOptions = getMinifyOptions(environment.config);
96
+ for (const id of Object.keys(pluginRecord)) {
97
+ if (!id.startsWith("html-")) {
98
+ continue;
99
+ }
100
+ const values = pluginRecord[id].values();
101
+ const isHtmlRspackPlugin = values.some((item) => {
102
+ var _item_constructor;
103
+ const name = (item === null || item === void 0 ? void 0 : item.name) || ((_item_constructor = item.constructor) === null || _item_constructor === void 0 ? void 0 : _item_constructor.name);
104
+ return name === "HtmlRspackPlugin";
105
+ });
106
+ if (isHtmlRspackPlugin) {
107
+ chain.plugin(id).tap((options) => {
108
+ if (!options.length) {
109
+ return options;
110
+ }
111
+ const userMinifyOption = options[0].minify;
112
+ const minifyFn = (html) => minify(html, userMinifyOption ? (0, import_ts_deepmerge.merge)(minifyOptions, userMinifyOption) : minifyOptions);
113
+ options[0].minify = minifyFn;
114
+ return options;
115
+ });
116
+ continue;
117
+ }
118
+ const isHtmlWebpackPlugin = values.some((item) => {
119
+ var _item_constructor;
120
+ const name = (item === null || item === void 0 ? void 0 : item.name) || ((_item_constructor = item.constructor) === null || _item_constructor === void 0 ? void 0 : _item_constructor.name);
121
+ return name === "HtmlWebpackPlugin";
122
+ });
123
+ if (isHtmlWebpackPlugin) {
124
+ chain.plugin(id).tap((options) => {
125
+ if (!options.length || options[0].minify) {
126
+ return options;
127
+ }
128
+ const userMinifyOption = options[0].minify;
129
+ options[0].minify = userMinifyOption ? (0, import_ts_deepmerge.merge)(minifyOptions, userMinifyOption) : minifyOptions;
130
+ return options;
131
+ });
132
+ }
133
+ }
134
+ });
135
+ }
136
+ });
137
+ // Annotate the CommonJS export names for ESM import in node:
138
+ 0 && (module.exports = {
139
+ pluginHtmlMinifierTerser
140
+ });
@@ -1,2 +1,2 @@
1
1
  import type { RsbuildPlugin } from '@rsbuild/core';
2
- export declare const pluginPostcssLegacy: (webBrowserslist: string[]) => RsbuildPlugin;
2
+ export declare const pluginPostcssLegacy: () => RsbuildPlugin;
@@ -23,12 +23,15 @@ __export(postcssLegacy_exports, {
23
23
  module.exports = __toCommonJS(postcssLegacy_exports);
24
24
  var import_utils = require("@modern-js/utils");
25
25
  var import_getCssSupport = require("../getCssSupport");
26
- const pluginPostcssLegacy = (webBrowserslist) => ({
26
+ const pluginPostcssLegacy = () => ({
27
27
  name: "uni-builder:postcss-plugins",
28
28
  setup(api) {
29
- api.modifyRsbuildConfig((config, { mergeRsbuildConfig }) => {
29
+ api.modifyEnvironmentConfig((config, { mergeEnvironmentConfig }) => {
30
30
  var _config_output;
31
- const cssSupport = (0, import_getCssSupport.getCssSupport)(webBrowserslist);
31
+ if (config.output.target !== "web") {
32
+ return;
33
+ }
34
+ const cssSupport = (0, import_getCssSupport.getCssSupport)(config.output.overrideBrowserslist);
32
35
  const enableExtractCSS = !((_config_output = config.output) === null || _config_output === void 0 ? void 0 : _config_output.injectStyles);
33
36
  const enableCssMinify = !enableExtractCSS && import_utils.isProd;
34
37
  const plugins = [
@@ -51,7 +54,7 @@ const pluginPostcssLegacy = (webBrowserslist) => ({
51
54
  ]
52
55
  }) : false
53
56
  ].filter(Boolean);
54
- return mergeRsbuildConfig(
57
+ return mergeEnvironmentConfig(
55
58
  {
56
59
  tools: {
57
60
  postcss: (opts) => {
@@ -25,11 +25,11 @@ var import_utils = require("../utils");
25
25
  const pluginRuntimeChunk = (disableInlineRuntimeChunk) => ({
26
26
  name: "uni-builder:runtime-chunk",
27
27
  setup(api) {
28
- api.modifyBundlerChain(async (chain, { target }) => {
28
+ api.modifyBundlerChain(async (chain, { target, environment }) => {
29
29
  if (target !== "web") {
30
30
  return;
31
31
  }
32
- const config = api.getNormalizedConfig();
32
+ const { config } = environment;
33
33
  const { chunkSplit } = config.performance;
34
34
  if (chunkSplit.strategy !== "all-in-one") {
35
35
  chain.optimization.runtimeChunk({
@@ -21,13 +21,31 @@ __export(splitChunk_exports, {
21
21
  pluginSplitChunks: () => pluginSplitChunks
22
22
  });
23
23
  module.exports = __toCommonJS(splitChunk_exports);
24
- var import_shared = require("@rsbuild/shared");
25
24
  var import_utils = require("@modern-js/utils");
25
+ const DEP_MATCH_TEMPLATE = /[\\/]node_modules[\\/](<SOURCES>)[\\/]/.source;
26
+ const createDependenciesRegExp = (...dependencies) => {
27
+ const sources = dependencies.map((d) => typeof d === "string" ? d : d.source);
28
+ const expr = DEP_MATCH_TEMPLATE.replace("<SOURCES>", sources.join("|"));
29
+ return new RegExp(expr);
30
+ };
31
+ function createCacheGroups(group) {
32
+ const experienceCacheGroup = {};
33
+ for (const [name, pkgs] of Object.entries(group)) {
34
+ const key = `lib-${name}`;
35
+ experienceCacheGroup[key] = {
36
+ test: createDependenciesRegExp(...pkgs),
37
+ priority: 0,
38
+ name: key,
39
+ reuseExistingChunk: true
40
+ };
41
+ }
42
+ return experienceCacheGroup;
43
+ }
26
44
  const pluginSplitChunks = () => ({
27
45
  name: "uni-builder:split-chunks",
28
46
  setup(api) {
29
- api.modifyBundlerChain((chain) => {
30
- const config = api.getNormalizedConfig();
47
+ api.modifyBundlerChain((chain, { environment }) => {
48
+ const { config } = environment;
31
49
  const { chunkSplit } = config.performance || {};
32
50
  if ((chunkSplit === null || chunkSplit === void 0 ? void 0 : chunkSplit.strategy) !== "split-by-experience") {
33
51
  return;
@@ -60,7 +78,7 @@ const pluginSplitChunks = () => ({
60
78
  ...currentConfig,
61
79
  // rspack chunks type mismatch with webpack
62
80
  cacheGroups: {
63
- ...(0, import_shared.createCacheGroups)(groups),
81
+ ...createCacheGroups(groups),
64
82
  ...currentConfig.cacheGroups
65
83
  }
66
84
  });
@@ -1,24 +1,25 @@
1
- import type { RsbuildTarget, RspackChain, RsbuildContext, NormalizedConfig } from '@rsbuild/core';
2
- import { OverrideBrowserslist } from '@rsbuild/shared';
1
+ import type { RsbuildTarget, RspackChain, RsbuildContext, NormalizedEnvironmentConfig } from '@rsbuild/core';
3
2
  export declare const RUNTIME_CHUNK_NAME = "builder-runtime";
3
+ export declare const SERVICE_WORKER_ENVIRONMENT_NAME = "serviceWorker";
4
4
  export declare const JS_REGEX: RegExp;
5
5
  export declare const TS_REGEX: RegExp;
6
6
  export declare const SCRIPT_REGEX: RegExp;
7
7
  export declare const NODE_MODULES_REGEX: RegExp;
8
- export declare function isServerTarget(target: RsbuildTarget[]): boolean;
8
+ export declare function isServerEnvironment(target: RsbuildTarget, environment: string): boolean;
9
9
  export declare const castArray: <T>(arr?: T | T[] | undefined) => T[];
10
+ export declare const getUseBuiltIns: (config: NormalizedEnvironmentConfig) => false | 'usage' | 'entry';
10
11
  export declare function applyScriptCondition({ rule, chain, config, context, includes, excludes, }: {
11
12
  rule: RspackChain.Rule;
12
13
  chain: RspackChain;
13
- config: NormalizedConfig;
14
+ config: NormalizedEnvironmentConfig;
14
15
  context: RsbuildContext;
15
16
  includes: (string | RegExp)[];
16
17
  excludes: (string | RegExp)[];
17
18
  }): void;
18
- export declare const isHtmlDisabled: (config: NormalizedConfig, target: RsbuildTarget) => boolean;
19
- export declare const getHash: (config: NormalizedConfig) => string;
19
+ export declare const isHtmlDisabled: (config: NormalizedEnvironmentConfig, target: RsbuildTarget) => boolean;
20
+ export declare const getHash: (config: NormalizedEnvironmentConfig) => string;
20
21
  export declare function getBrowserslistWithDefault(path: string, config: {
21
22
  output?: {
22
- overrideBrowserslist?: OverrideBrowserslist;
23
+ overrideBrowserslist?: string[];
23
24
  };
24
25
  }, target: RsbuildTarget): Promise<string[]>;
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
+ var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
5
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
8
  var __export = (target, all) => {
7
9
  for (var name in all)
@@ -15,6 +17,14 @@ var __copyProps = (to, from, except, desc) => {
15
17
  }
16
18
  return to;
17
19
  };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
18
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
29
  var utils_exports = {};
20
30
  __export(utils_exports, {
@@ -22,28 +32,26 @@ __export(utils_exports, {
22
32
  NODE_MODULES_REGEX: () => NODE_MODULES_REGEX,
23
33
  RUNTIME_CHUNK_NAME: () => RUNTIME_CHUNK_NAME,
24
34
  SCRIPT_REGEX: () => SCRIPT_REGEX,
35
+ SERVICE_WORKER_ENVIRONMENT_NAME: () => SERVICE_WORKER_ENVIRONMENT_NAME,
25
36
  TS_REGEX: () => TS_REGEX,
26
37
  applyScriptCondition: () => applyScriptCondition,
27
38
  castArray: () => castArray,
28
39
  getBrowserslistWithDefault: () => getBrowserslistWithDefault,
29
40
  getHash: () => getHash,
41
+ getUseBuiltIns: () => getUseBuiltIns,
30
42
  isHtmlDisabled: () => isHtmlDisabled,
31
- isServerTarget: () => isServerTarget
43
+ isServerEnvironment: () => isServerEnvironment
32
44
  });
33
45
  module.exports = __toCommonJS(utils_exports);
34
- var import_shared = require("@rsbuild/shared");
46
+ var import_browserslist = __toESM(require("browserslist"));
35
47
  const RUNTIME_CHUNK_NAME = "builder-runtime";
48
+ const SERVICE_WORKER_ENVIRONMENT_NAME = "serviceWorker";
36
49
  const JS_REGEX = /\.(?:js|mjs|cjs|jsx)$/;
37
50
  const TS_REGEX = /\.(?:ts|mts|cts|tsx)$/;
38
51
  const SCRIPT_REGEX = /\.(?:js|jsx|mjs|cjs|ts|tsx|mts|cts)$/;
39
52
  const NODE_MODULES_REGEX = /[\\/]node_modules[\\/]/;
40
- function isServerTarget(target) {
41
- return (Array.isArray(target) ? target : [
42
- target
43
- ]).some((item) => [
44
- "node",
45
- "service-worker"
46
- ].includes(item));
53
+ function isServerEnvironment(target, environment) {
54
+ return target === "node" || environment === SERVICE_WORKER_ENVIRONMENT_NAME;
47
55
  }
48
56
  const castArray = (arr) => {
49
57
  if (arr === void 0) {
@@ -53,6 +61,30 @@ const castArray = (arr) => {
53
61
  arr
54
62
  ];
55
63
  };
64
+ const browsersListCache = /* @__PURE__ */ new Map();
65
+ async function getBrowserslist(path) {
66
+ const env = process.env.NODE_ENV;
67
+ const cacheKey = `${path}${env}`;
68
+ if (browsersListCache.has(cacheKey)) {
69
+ return browsersListCache.get(cacheKey);
70
+ }
71
+ const result = import_browserslist.default.loadConfig({
72
+ path,
73
+ env
74
+ });
75
+ if (result) {
76
+ browsersListCache.set(cacheKey, result);
77
+ return result;
78
+ }
79
+ return null;
80
+ }
81
+ const getUseBuiltIns = (config) => {
82
+ const { polyfill } = config.output;
83
+ if (polyfill === "off") {
84
+ return false;
85
+ }
86
+ return polyfill;
87
+ };
56
88
  function applyScriptCondition({ rule, chain, config, context, includes, excludes }) {
57
89
  rule.include.add({
58
90
  and: [
@@ -107,26 +139,19 @@ const DEFAULT_BROWSERSLIST = {
107
139
  node: [
108
140
  "node >= 14"
109
141
  ],
110
- "web-worker": DEFAULT_WEB_BROWSERSLIST,
111
- "service-worker": DEFAULT_WEB_BROWSERSLIST
142
+ "web-worker": DEFAULT_WEB_BROWSERSLIST
112
143
  };
113
144
  async function getBrowserslistWithDefault(path, config, target) {
114
- const { overrideBrowserslist: overrides = {} } = (config === null || config === void 0 ? void 0 : config.output) || {};
145
+ const { overrideBrowserslist: overrides } = (config === null || config === void 0 ? void 0 : config.output) || {};
115
146
  if (target === "web" || target === "web-worker") {
116
- if (Array.isArray(overrides)) {
147
+ if (overrides) {
117
148
  return overrides;
118
149
  }
119
- if (overrides[target]) {
120
- return overrides[target];
121
- }
122
- const browserslistrc = await (0, import_shared.getBrowserslist)(path);
150
+ const browserslistrc = await getBrowserslist(path);
123
151
  if (browserslistrc) {
124
152
  return browserslistrc;
125
153
  }
126
154
  }
127
- if (!Array.isArray(overrides) && overrides[target]) {
128
- return overrides[target];
129
- }
130
155
  return DEFAULT_BROWSERSLIST[target];
131
156
  }
132
157
  // Annotate the CommonJS export names for ESM import in node:
@@ -135,11 +160,13 @@ async function getBrowserslistWithDefault(path, config, target) {
135
160
  NODE_MODULES_REGEX,
136
161
  RUNTIME_CHUNK_NAME,
137
162
  SCRIPT_REGEX,
163
+ SERVICE_WORKER_ENVIRONMENT_NAME,
138
164
  TS_REGEX,
139
165
  applyScriptCondition,
140
166
  castArray,
141
167
  getBrowserslistWithDefault,
142
168
  getHash,
169
+ getUseBuiltIns,
143
170
  isHtmlDisabled,
144
- isServerTarget
171
+ isServerEnvironment
145
172
  });
package/dist/types.d.ts CHANGED
@@ -1,5 +1,4 @@
1
- import type { NodeEnv, RequestHandler, HtmlTagDescriptor } from '@rsbuild/shared';
2
- import type { ConfigChainWithContext, ConfigChain, DevConfig, RsbuildConfig, RsbuildTarget, Polyfill, ScriptInject, ServerConfig, RsbuildPluginAPI, SourceConfig, OutputConfig, Rspack } from '@rsbuild/core';
1
+ import type { ConfigChainWithContext, ConfigChain, DevConfig, RsbuildConfig, RsbuildTarget, Polyfill, ScriptInject, ServerConfig, RsbuildPluginAPI, OutputConfig, DistPathConfig, Rspack, RequestHandler, NodeEnv, HtmlTagDescriptor } from '@rsbuild/core';
3
2
  import type { PluginAssetsRetryOptions } from '@rsbuild/plugin-assets-retry';
4
3
  import type { PluginStyledComponentsOptions } from '@rsbuild/plugin-styled-components';
5
4
  import type { PluginRemOptions } from '@rsbuild/plugin-rem';
@@ -16,8 +15,9 @@ import type { AliasOption } from '@modern-js/utils';
16
15
  import type { StartDevServerOptions, UniBuilderStartServerResult } from './shared/devServer';
17
16
  import type { PluginSourceBuildOptions } from '@rsbuild/plugin-source-build';
18
17
  import type TerserPlugin from 'terser-webpack-plugin';
18
+ import type { Options as HTMLPluginOptions } from 'html-webpack-plugin';
19
19
  type ArrayOrNot<T> = T | T[];
20
- export type Stats = Omit<Rspack.Stats, '#private' | 'hash'>;
20
+ export type Stats = Omit<Rspack.Stats, '#private' | 'hash' | 'startTime' | 'endTime'>;
21
21
  export type RspackConfig = Rspack.Configuration;
22
22
  export type MultiStats = Omit<Rspack.MultiStats, '#private' | 'hash' | 'stats'> & {
23
23
  stats: Stats[];
@@ -37,9 +37,7 @@ export type MetaOptions = {
37
37
  [name: string]: string | false | MetaAttrs;
38
38
  };
39
39
  export type CreateBuilderCommonOptions = {
40
- entry?: SourceConfig['entry'];
41
40
  frameworkConfigPath?: string;
42
- target?: RsbuildTarget | RsbuildTarget[];
43
41
  /** The root path of current project. */
44
42
  cwd: string;
45
43
  };
@@ -83,6 +81,10 @@ export type TerserPluginOptions = TerserPlugin.BasePluginOptions & TerserPlugin.
83
81
  export type ToolsTerserConfig = ConfigChain<TerserPluginOptions>;
84
82
  export type UniBuilderExtraConfig = {
85
83
  tools?: {
84
+ htmlPlugin?: boolean | ConfigChainWithContext<HTMLPluginOptions, {
85
+ entryName: string;
86
+ entryValue: (string | string[] | Rspack.EntryDescription)[];
87
+ }>;
86
88
  styledComponents?: false | PluginStyledComponentsOptions;
87
89
  devServer?: ToolsDevServerConfig;
88
90
  /**
@@ -126,6 +128,10 @@ export type UniBuilderExtraConfig = {
126
128
  sass?: PluginSassOptions['sassLoaderOptions'];
127
129
  };
128
130
  dev?: {
131
+ /** Set the page URL to open when the server starts. */
132
+ startUrl?: boolean | string | string[];
133
+ /** Execute a callback function before opening the `startUrl`. */
134
+ beforeStartUrl?: () => Promise<void> | void;
129
135
  /**
130
136
  * Used to set the host of Dev Server.
131
137
  */
@@ -318,7 +324,7 @@ export type OverridesUniBuilderInstance = {
318
324
  startDevServer: (options: StartDevServerOptions) => Promise<UniBuilderStartServerResult>;
319
325
  };
320
326
  export type UniBuilderContext = RsbuildPluginAPI['context'] & {
321
- target: RsbuildPluginAPI['context']['targets'];
327
+ target: RsbuildTarget[];
322
328
  framework: string;
323
329
  srcPath: string;
324
330
  entry: Record<string, string | string[]>;
@@ -352,16 +358,22 @@ export type UniBuilderPlugin = {
352
358
  post?: string[];
353
359
  remove?: string[];
354
360
  };
361
+ export type DistPath = DistPathConfig & {
362
+ server?: string;
363
+ worker?: string;
364
+ };
355
365
  export type UniBuilderConfig = {
356
366
  dev?: RsbuildConfig['dev'];
357
367
  html?: RsbuildConfig['html'];
358
- output?: Omit<NonNullable<RsbuildConfig['output']>, 'polyfill'> & {
368
+ output?: Omit<NonNullable<RsbuildConfig['output']>, 'polyfill' | 'distPath'> & {
359
369
  polyfill?: Polyfill | 'ua';
370
+ distPath?: DistPath;
360
371
  };
361
372
  performance?: RsbuildConfig['performance'];
362
373
  security?: RsbuildConfig['security'];
363
- tools?: RsbuildConfig['tools'];
374
+ tools?: Omit<NonNullable<RsbuildConfig['tools']>, 'htmlPlugin'>;
364
375
  source?: Omit<NonNullable<RsbuildConfig['source']>, 'alias'>;
365
376
  plugins?: RsbuildConfig['plugins'];
377
+ environments?: RsbuildConfig['environments'];
366
378
  } & UniBuilderExtraConfig;
367
379
  export {};
@@ -38,6 +38,7 @@ var import_compatLegacyPlugin = require("../shared/compatLegacyPlugin");
38
38
  var import_moduleScopes = require("./plugins/moduleScopes");
39
39
  var import_babel = require("./plugins/babel");
40
40
  var import_react = require("./plugins/react");
41
+ var import_utils = require("../shared/utils");
41
42
  async function parseConfig(uniBuilderConfig, options) {
42
43
  var _uniBuilderConfig_tools, _uniBuilderConfig_performance, _uniBuilderConfig_tools1, _uniBuilderConfig_output, _uniBuilderConfig_security, _uniBuilderConfig_experiments, _uniBuilderConfig_tools2;
43
44
  const { rsbuildConfig, rsbuildPlugins } = await (0, import_parseCommonConfig.parseCommonConfig)(uniBuilderConfig, options);
@@ -68,9 +69,13 @@ async function parseConfig(uniBuilderConfig, options) {
68
69
  rsbuildPlugins.push(pluginLazyCompilation((_uniBuilderConfig_experiments1 = uniBuilderConfig.experiments) === null || _uniBuilderConfig_experiments1 === void 0 ? void 0 : _uniBuilderConfig_experiments1.lazyCompilation));
69
70
  }
70
71
  if (((_uniBuilderConfig_tools2 = uniBuilderConfig.tools) === null || _uniBuilderConfig_tools2 === void 0 ? void 0 : _uniBuilderConfig_tools2.styledComponents) !== false) {
71
- var _uniBuilderConfig_tools4;
72
+ var _uniBuilderConfig_tools4, _uniBuilderConfig_environments;
72
73
  const { pluginStyledComponents } = await Promise.resolve().then(() => __toESM(require("./plugins/styledComponents")));
73
- rsbuildPlugins.push(pluginStyledComponents((_uniBuilderConfig_tools4 = uniBuilderConfig.tools) === null || _uniBuilderConfig_tools4 === void 0 ? void 0 : _uniBuilderConfig_tools4.styledComponents));
74
+ const options2 = ((_uniBuilderConfig_tools4 = uniBuilderConfig.tools) === null || _uniBuilderConfig_tools4 === void 0 ? void 0 : _uniBuilderConfig_tools4.styledComponents) || {};
75
+ if ((_uniBuilderConfig_environments = uniBuilderConfig.environments) === null || _uniBuilderConfig_environments === void 0 ? void 0 : _uniBuilderConfig_environments[import_utils.SERVICE_WORKER_ENVIRONMENT_NAME]) {
76
+ options2.ssr = true;
77
+ }
78
+ rsbuildPlugins.push(pluginStyledComponents(options2));
74
79
  }
75
80
  return {
76
81
  rsbuildConfig,
@@ -33,8 +33,8 @@ __export(babel_exports, {
33
33
  });
34
34
  module.exports = __toCommonJS(babel_exports);
35
35
  var import_cloneDeep = __toESM(require("lodash/cloneDeep"));
36
- var import_web = require("@rsbuild/babel-preset/web");
37
- var import_node = require("@rsbuild/babel-preset/node");
36
+ var import_web = require("@modern-js/babel-preset/web");
37
+ var import_node = require("@modern-js/babel-preset/node");
38
38
  var import_utils = require("@modern-js/utils");
39
39
  var import_plugin_babel = require("@rsbuild/plugin-babel");
40
40
  var import_utils2 = require("../../shared/utils");
@@ -63,8 +63,8 @@ const pluginBabel = (options, extraOptions) => ({
63
63
  setup(api) {
64
64
  api.modifyBundlerChain({
65
65
  order: "pre",
66
- handler: async (chain, { CHAIN_ID, target, isProd, isServer, isServiceWorker }) => {
67
- const config = api.getNormalizedConfig();
66
+ handler: async (chain, { CHAIN_ID, target, isProd, isServer, environment }) => {
67
+ const { config, name } = environment;
68
68
  const browserslist = await (0, import_utils2.getBrowserslistWithDefault)(api.context.rootPath, config, target);
69
69
  const getBabelOptions = (config2) => {
70
70
  var _baseBabelConfig_presets;
@@ -87,7 +87,7 @@ const pluginBabel = (options, extraOptions) => ({
87
87
  }
88
88
  };
89
89
  const decoratorConfig = config2.source.decorators;
90
- const baseBabelConfig = isServer || isServiceWorker ? (0, import_node.getBabelConfigForNode)({
90
+ const baseBabelConfig = isServer || name === import_utils2.SERVICE_WORKER_ENVIRONMENT_NAME ? (0, import_node.getBabelConfigForNode)({
91
91
  presetEnv: {
92
92
  targets: [
93
93
  "node >= 14"
@@ -97,7 +97,7 @@ const pluginBabel = (options, extraOptions) => ({
97
97
  }) : (0, import_web.getBabelConfigForWeb)({
98
98
  presetEnv: {
99
99
  targets: browserslist,
100
- useBuiltIns: (0, import_plugin_babel.getUseBuiltIns)(config2)
100
+ useBuiltIns: (0, import_utils2.getUseBuiltIns)(config2)
101
101
  },
102
102
  pluginDecorators: decoratorConfig
103
103
  });
@@ -138,14 +138,12 @@ const pluginBabel = (options, extraOptions) => ({
138
138
  };
139
139
  const { babelOptions, includes, excludes } = getBabelOptions(config);
140
140
  const rule = chain.module.rule(CHAIN_ID.RULE.JS);
141
- (0, import_utils2.applyScriptCondition)({
142
- chain,
143
- rule,
144
- config,
145
- context: api.context,
146
- includes,
147
- excludes
148
- });
141
+ for (const condition of includes) {
142
+ rule.include.add(condition);
143
+ }
144
+ for (const condition of excludes) {
145
+ rule.exclude.add(condition);
146
+ }
149
147
  rule.test(import_utils2.SCRIPT_REGEX).use(CHAIN_ID.USE.BABEL).loader(require.resolve("babel-loader")).options(babelOptions);
150
148
  chain.module.rule(CHAIN_ID.RULE.JS_DATA_URI).mimetype({
151
149
  or: [
@@ -86,8 +86,8 @@ async function applyJSMinimizer(chain, config, CHAIN_ID, userTerserConfig) {
86
86
  const pluginMinimize = (userTerserConfig) => ({
87
87
  name: "uni-builder:minimize",
88
88
  setup(api) {
89
- api.modifyBundlerChain(async (chain, { isProd, CHAIN_ID }) => {
90
- const config = api.getNormalizedConfig();
89
+ api.modifyBundlerChain(async (chain, { isProd, CHAIN_ID, environment }) => {
90
+ const { config } = environment;
91
91
  const { minify } = config.output;
92
92
  if (minify === false || !isProd) {
93
93
  return;
@@ -38,7 +38,7 @@ const pluginReact = () => ({
38
38
  ],
39
39
  setup(api) {
40
40
  api.modifyBundlerChain(async (chain, utils) => {
41
- const config = api.getNormalizedConfig();
41
+ const { config } = utils.environment;
42
42
  const usingHMR = !utils.isProd && config.dev.hmr && utils.target === "web";
43
43
  if (!usingHMR) {
44
44
  return;
@@ -52,8 +52,8 @@ const pluginStyledComponents = (userConfig = {}) => ({
52
52
  ],
53
53
  setup(api) {
54
54
  api.modifyBundlerChain(async (chain, { CHAIN_ID, isProd }) => {
55
- const isSSR = (0, import_utils2.isServerTarget)(api.context.targets);
56
- const styledComponentsOptions = (0, import_utils.applyOptionsChain)(getDefaultStyledComponentsConfig(isProd, isSSR), userConfig);
55
+ const hasSSR = Object.entries(api.getNormalizedConfig().environments).some(([name, config]) => (0, import_utils2.isServerEnvironment)(config.output.target, name));
56
+ const styledComponentsOptions = (0, import_utils.applyOptionsChain)(getDefaultStyledComponentsConfig(isProd, hasSSR), userConfig);
57
57
  if (!styledComponentsOptions) {
58
58
  return;
59
59
  }
@@ -1,5 +1,5 @@
1
1
  import { type ConfigChainWithContext } from '@rsbuild/core';
2
- import { PluginBabelOptions } from '@rsbuild/plugin-babel';
2
+ import { type PluginBabelOptions } from '@rsbuild/plugin-babel';
3
3
  import type { RsbuildPlugin } from '@rsbuild/core';
4
4
  import type { Options as RawTSLoaderOptions } from 'ts-loader';
5
5
  type OneOrMany<T> = T | T[];
@@ -33,7 +33,7 @@ __export(tsLoader_exports, {
33
33
  module.exports = __toCommonJS(tsLoader_exports);
34
34
  var import_utils = require("@modern-js/utils");
35
35
  var import_plugin_babel = require("@rsbuild/plugin-babel");
36
- var import_web = require("@rsbuild/babel-preset/web");
36
+ var import_web = require("@modern-js/babel-preset/web");
37
37
  var import_babel = require("./babel");
38
38
  var import_utils2 = require("../../shared/utils");
39
39
  const pluginTsLoader = (options, babelOptions) => {
@@ -45,15 +45,15 @@ const pluginTsLoader = (options, babelOptions) => {
45
45
  setup(api) {
46
46
  api.modifyBundlerChain({
47
47
  order: "pre",
48
- handler: async (chain, { isProd, target, CHAIN_ID }) => {
48
+ handler: async (chain, { isProd, target, CHAIN_ID, environment }) => {
49
49
  var _baseBabelConfig_presets;
50
- const config = api.getNormalizedConfig();
50
+ const { config } = environment;
51
51
  const { rootPath } = api.context;
52
52
  const browserslist = await (0, import_utils2.getBrowserslistWithDefault)(rootPath, config, target);
53
53
  const baseBabelConfig = (0, import_web.getBabelConfigForWeb)({
54
54
  presetEnv: {
55
55
  targets: browserslist,
56
- useBuiltIns: (0, import_plugin_babel.getUseBuiltIns)(config)
56
+ useBuiltIns: (0, import_utils2.getUseBuiltIns)(config)
57
57
  }
58
58
  });
59
59
  (_baseBabelConfig_presets = baseBabelConfig.presets) === null || _baseBabelConfig_presets === void 0 ? void 0 : _baseBabelConfig_presets.push((0, import_babel.getPresetReact)(api.context.rootPath, isProd));
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@modern-js/uni-builder",
3
- "version": "2.55.0",
3
+ "version": "2.56.1",
4
4
  "description": "Unified builder for Modern.js",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "https://github.com/web-infra-dev/modern.js",
8
- "directory": "packages/builder/uni-builder"
8
+ "directory": "packages/cli/uni-builder"
9
9
  },
10
10
  "license": "MIT",
11
11
  "type": "commonjs",
@@ -22,30 +22,28 @@
22
22
  "dist"
23
23
  ],
24
24
  "dependencies": {
25
- "@babel/core": "^7.23.2",
25
+ "@babel/core": "^7.24.7",
26
26
  "@babel/preset-react": "^7.22.15",
27
- "@babel/types": "^7.23.0",
27
+ "@babel/types": "^7.24.7",
28
28
  "@pmmmwh/react-refresh-webpack-plugin": "0.5.10",
29
- "@rsbuild/babel-preset": "0.7.10",
30
- "@rsbuild/core": "0.7.10",
31
- "@rsbuild/plugin-sass": "0.7.10",
32
- "@rsbuild/plugin-less": "0.7.10",
33
- "@rsbuild/plugin-assets-retry": "0.7.10",
34
- "@rsbuild/plugin-babel": "0.7.10",
35
- "@rsbuild/plugin-check-syntax": "0.7.10",
36
- "@rsbuild/plugin-css-minimizer": "0.7.10",
37
- "@rsbuild/plugin-pug": "0.7.10",
38
- "@rsbuild/plugin-react": "0.7.10",
39
- "@rsbuild/plugin-rem": "0.7.10",
40
- "@rsbuild/plugin-source-build": "0.7.10",
41
- "@rsbuild/plugin-styled-components": "0.7.10",
42
- "@rsbuild/plugin-svgr": "0.7.10",
43
- "@rsbuild/plugin-type-check": "0.7.10",
44
- "@rsbuild/plugin-typed-css-modules": "0.7.10",
45
- "@rsbuild/plugin-toml": "0.7.10",
46
- "@rsbuild/plugin-yaml": "0.7.10",
47
- "@rsbuild/shared": "0.7.10",
48
- "@rsbuild/webpack": "0.7.10",
29
+ "@rsbuild/core": "1.0.1-beta.3",
30
+ "@rsbuild/plugin-sass": "1.0.1-beta.3",
31
+ "@rsbuild/plugin-less": "1.0.1-beta.3",
32
+ "@rsbuild/plugin-assets-retry": "1.0.1-beta.3",
33
+ "@rsbuild/plugin-babel": "1.0.1-beta.3",
34
+ "@rsbuild/plugin-check-syntax": "1.0.1-beta.3",
35
+ "@rsbuild/plugin-css-minimizer": "1.0.1",
36
+ "@rsbuild/plugin-pug": "1.0.1",
37
+ "@rsbuild/plugin-react": "1.0.1-beta.3",
38
+ "@rsbuild/plugin-rem": "1.0.1-beta.3",
39
+ "@rsbuild/plugin-source-build": "1.0.1-beta.3",
40
+ "@rsbuild/plugin-styled-components": "1.0.1-beta.3",
41
+ "@rsbuild/plugin-svgr": "1.0.1-beta.3",
42
+ "@rsbuild/plugin-type-check": "1.0.1-beta.3",
43
+ "@rsbuild/plugin-typed-css-modules": "1.0.1",
44
+ "@rsbuild/plugin-toml": "1.0.0",
45
+ "@rsbuild/plugin-yaml": "1.0.1",
46
+ "@rsbuild/webpack": "1.0.1-beta.3",
49
47
  "@swc/helpers": "0.5.3",
50
48
  "babel-loader": "9.1.3",
51
49
  "babel-plugin-import": "1.13.5",
@@ -55,6 +53,7 @@
55
53
  "cssnano": "6.0.1",
56
54
  "glob": "^9.3.5",
57
55
  "html-webpack-plugin": "5.5.3",
56
+ "html-minifier-terser": "^7.2.0",
58
57
  "lodash": "^4.17.21",
59
58
  "postcss": "^8.4.35",
60
59
  "postcss-custom-properties": "13.1.5",
@@ -70,20 +69,23 @@
70
69
  "terser-webpack-plugin": "5.3.10",
71
70
  "ts-deepmerge": "7.0.0",
72
71
  "ts-loader": "9.4.4",
73
- "webpack": "^5.92.0",
72
+ "webpack": "^5.93.0",
74
73
  "webpack-subresource-integrity": "5.1.0",
75
- "@modern-js/utils": "2.55.0",
76
- "@modern-js/server": "2.55.0",
77
- "@modern-js/prod-server": "2.55.0"
74
+ "@modern-js/server": "2.56.1",
75
+ "@modern-js/prod-server": "2.56.1",
76
+ "@modern-js/utils": "2.56.1",
77
+ "@modern-js/babel-preset": "2.56.1"
78
78
  },
79
79
  "devDependencies": {
80
- "@rsbuild/plugin-swc": "0.7.10",
80
+ "@rsbuild/plugin-swc": "1.0.1-beta.3",
81
+ "@types/html-minifier-terser": "^7.0.2",
81
82
  "@types/lodash": "^4.14.202",
82
83
  "react": "^18.2.0",
83
84
  "react-dom": "^18.2.0",
85
+ "terser": "^5.31.1",
84
86
  "typescript": "^5.3.0",
85
- "@scripts/vitest-config": "2.55.0",
86
- "@scripts/build": "2.55.0"
87
+ "@scripts/build": "2.56.1",
88
+ "@scripts/vitest-config": "2.56.1"
87
89
  },
88
90
  "publishConfig": {
89
91
  "access": "public",