@rsbuild/core 0.0.16 → 0.0.18

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.
Files changed (49) hide show
  1. package/dist/cli/commands.d.ts +3 -0
  2. package/dist/cli/commands.js +4 -2
  3. package/dist/cli/config.d.ts +4 -0
  4. package/dist/cli/index.js +2 -1
  5. package/dist/plugins/define.js +15 -27
  6. package/dist/plugins/html.d.ts +7 -1
  7. package/dist/plugins/html.js +72 -48
  8. package/dist/plugins/index.js +0 -1
  9. package/dist/plugins/nodeAddons.js +10 -17
  10. package/dist/plugins/startUrl.js +12 -16
  11. package/dist/rspack-plugins/HtmlBasicPlugin.d.ts +19 -0
  12. package/dist/rspack-plugins/HtmlBasicPlugin.js +87 -0
  13. package/dist/rspack-provider/core/createCompiler.js +7 -5
  14. package/dist/rspack-provider/core/devMiddleware.d.ts +2 -4
  15. package/dist/rspack-provider/core/startDevServer.d.ts +1 -2
  16. package/dist/rspack-provider/core/startDevServer.js +6 -31
  17. package/dist/rspack-provider/plugins/css.js +2 -2
  18. package/dist/rspack-provider/plugins/less.js +1 -1
  19. package/dist/rspack-provider/plugins/progress.js +5 -1
  20. package/dist/rspack-provider/plugins/resolve.js +1 -2
  21. package/dist/rspack-provider/plugins/sass.js +2 -2
  22. package/dist/rspack-provider/plugins/swc.js +5 -8
  23. package/dist/rspack-provider/provider.js +5 -3
  24. package/dist/rspack-provider/shared/plugin.js +0 -1
  25. package/dist/server/constants.d.ts +6 -0
  26. package/dist/server/constants.js +49 -0
  27. package/dist/server/dev-middleware/hmr-client/createSocketUrl.d.ts +16 -0
  28. package/dist/server/dev-middleware/hmr-client/createSocketUrl.js +68 -0
  29. package/dist/server/dev-middleware/hmr-client/index.d.ts +1 -0
  30. package/dist/server/dev-middleware/hmr-client/index.js +164 -0
  31. package/dist/server/dev-middleware/index.d.ts +22 -0
  32. package/dist/server/dev-middleware/index.js +90 -0
  33. package/dist/server/dev-middleware/socketServer.d.ts +22 -0
  34. package/dist/server/dev-middleware/socketServer.js +167 -0
  35. package/dist/server/devServer.d.ts +23 -0
  36. package/dist/server/devServer.js +172 -0
  37. package/dist/server/https.d.ts +6 -0
  38. package/dist/server/https.js +50 -0
  39. package/dist/server/index.d.ts +2 -0
  40. package/dist/server/index.js +31 -0
  41. package/dist/server/prodServer.d.ts +26 -0
  42. package/dist/server/prodServer.js +133 -0
  43. package/package.json +32 -6
  44. package/static/template.html +7 -0
  45. package/types.d.ts +130 -0
  46. package/dist/plugins/assetsRetry.d.ts +0 -2
  47. package/dist/plugins/assetsRetry.js +0 -66
  48. package/dist/utils/generateMetaTags.d.ts +0 -0
  49. package/dist/utils/generateMetaTags.js +0 -1
@@ -4,4 +4,7 @@ export type InspectOptions = {
4
4
  output: string;
5
5
  verbose?: boolean;
6
6
  };
7
+ export type DevOptions = {
8
+ open?: boolean;
9
+ };
7
10
  export declare function setupProgram(rsbuild: RsbuildInstance): void;
@@ -28,8 +28,10 @@ function setupProgram(rsbuild) {
28
28
  const pkgJson = (0, import_path.join)(__dirname, "../../package.json");
29
29
  const { version } = import_fs_extra.fs.readJSONSync(pkgJson);
30
30
  import_commander.program.name("rsbuild").usage("<command> [options]").version(version);
31
- import_commander.program.command("dev").description("starting the dev server").action(async () => {
32
- await rsbuild.startDevServer();
31
+ import_commander.program.command("dev").option(`--open`, "open the page in browser on startup").description("starting the dev server").action(async (options) => {
32
+ await rsbuild.startDevServer({
33
+ open: options.open
34
+ });
33
35
  });
34
36
  import_commander.program.command("build").description("build the app for production").action(async () => {
35
37
  await rsbuild.build();
@@ -4,6 +4,10 @@ export type RsbuildConfig = BaseRsbuildConfig & {
4
4
  entries?: RsbuildEntry;
5
5
  };
6
6
  plugins?: RsbuildPlugin[];
7
+ /**
8
+ * @private only for testing
9
+ */
10
+ provider?: any;
7
11
  };
8
12
  export declare const defineConfig: (config: RsbuildConfig) => RsbuildConfig;
9
13
  export declare function loadConfig(): Promise<ReturnType<typeof defineConfig>>;
package/dist/cli/index.js CHANGED
@@ -31,7 +31,8 @@ async function runCli(options = {}) {
31
31
  const config = await (0, import_config.loadConfig)();
32
32
  const rsbuild = await (0, import__.createRsbuild)({
33
33
  rsbuildConfig: config,
34
- entry: ((_a = config.source) == null ? void 0 : _a.entries) || (0, import_config.getDefaultEntries)()
34
+ entry: ((_a = config.source) == null ? void 0 : _a.entries) || (0, import_config.getDefaultEntries)(),
35
+ provider: config.provider
35
36
  });
36
37
  if (options.defaultPlugins) {
37
38
  rsbuild.addPlugins(options.defaultPlugins);
@@ -21,36 +21,24 @@ __export(define_exports, {
21
21
  pluginDefine: () => pluginDefine
22
22
  });
23
23
  module.exports = __toCommonJS(define_exports);
24
- var import_lodash = require("lodash");
25
24
  var import_shared = require("@rsbuild/shared");
26
25
  const pluginDefine = () => ({
27
26
  name: "plugin-define",
28
- async setup(api) {
29
- api.modifyBundlerChain(
30
- async (chain, { env, target, CHAIN_ID, bundler }) => {
31
- const config = api.getNormalizedConfig();
32
- const publicPath = chain.output.get("publicPath");
33
- const assetPrefix = publicPath && typeof publicPath === "string" ? publicPath : config.output.assetPrefix;
34
- const builtinVars = {
35
- "process.env.NODE_ENV": process.env.NODE_ENV,
36
- "process.env.ASSET_PREFIX": (0, import_shared.removeTailSlash)(assetPrefix)
37
- };
38
- const globalVars = (0, import_shared.mergeChainedOptions)({
39
- defaults: builtinVars,
40
- options: config.source.globalVars,
41
- utils: { env, target }
42
- });
43
- const serializedVars = (0, import_lodash.mapValues)(
44
- globalVars,
45
- (value) => {
46
- var _a;
47
- return (_a = JSON.stringify(value)) != null ? _a : "undefined";
48
- }
49
- );
50
- const defineExprs = config.source.define;
51
- chain.plugin(CHAIN_ID.PLUGIN.DEFINE).use(bundler.DefinePlugin, [{ ...serializedVars, ...defineExprs }]);
52
- }
53
- );
27
+ setup(api) {
28
+ api.modifyBundlerChain((chain, { CHAIN_ID, bundler }) => {
29
+ const config = api.getNormalizedConfig();
30
+ const publicPath = chain.output.get("publicPath");
31
+ const assetPrefix = publicPath && typeof publicPath === "string" ? publicPath : config.output.assetPrefix;
32
+ const builtinVars = {
33
+ "process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV),
34
+ "process.env.ASSET_PREFIX": JSON.stringify(
35
+ (0, import_shared.removeTailSlash)(assetPrefix)
36
+ )
37
+ };
38
+ chain.plugin(CHAIN_ID.PLUGIN.DEFINE).use(bundler.DefinePlugin, [
39
+ { ...builtinVars, ...config.source.define }
40
+ ]);
41
+ });
54
42
  }
55
43
  });
56
44
  // Annotate the CommonJS export names for ESM import in node:
@@ -1,4 +1,10 @@
1
- import type { MetaAttrs, HtmlConfig, MetaOptions, DefaultRsbuildPlugin, SharedRsbuildPluginAPI, NormalizedOutputConfig } from '@rsbuild/shared';
1
+ import type { MetaAttrs, HtmlConfig, MetaOptions, NormalizedConfig, DefaultRsbuildPlugin, SharedRsbuildPluginAPI, NormalizedOutputConfig } from '@rsbuild/shared';
2
+ export declare function getTitle(entryName: string, config: NormalizedConfig): string;
3
+ export declare function getInject(entryName: string, config: NormalizedConfig): import("@rsbuild/shared").ScriptInject;
4
+ export declare function getTemplatePath(entryName: string, config: NormalizedConfig): string;
5
+ export declare function getFavicon(entryName: string, config: {
6
+ html: HtmlConfig;
7
+ }): string;
2
8
  export declare const generateMetaTags: (metaOptions?: MetaOptions) => MetaAttrs[];
3
9
  export declare function getMetaTags(entryName: string, config: {
4
10
  html: HtmlConfig;
@@ -30,7 +30,11 @@ var html_exports = {};
30
30
  __export(html_exports, {
31
31
  applyInjectTags: () => applyInjectTags,
32
32
  generateMetaTags: () => generateMetaTags,
33
+ getFavicon: () => getFavicon,
34
+ getInject: () => getInject,
33
35
  getMetaTags: () => getMetaTags,
36
+ getTemplatePath: () => getTemplatePath,
37
+ getTitle: () => getTitle,
34
38
  pluginHtml: () => pluginHtml
35
39
  });
36
40
  module.exports = __toCommonJS(html_exports);
@@ -38,6 +42,43 @@ var import_path = __toESM(require("path"));
38
42
  var import_shared = require("@rsbuild/shared");
39
43
  var import_fs_extra = require("@rsbuild/shared/fs-extra");
40
44
  var import_lodash = __toESM(require("lodash"));
45
+ var import_HtmlBasicPlugin = require("../rspack-plugins/HtmlBasicPlugin");
46
+ function getTitle(entryName, config) {
47
+ return (0, import_shared.mergeChainedOptions)({
48
+ defaults: "",
49
+ options: config.html.title,
50
+ utils: { entryName },
51
+ useObjectParam: true
52
+ });
53
+ }
54
+ function getInject(entryName, config) {
55
+ return (0, import_shared.mergeChainedOptions)({
56
+ defaults: "head",
57
+ options: config.html.inject,
58
+ utils: { entryName },
59
+ useObjectParam: true
60
+ });
61
+ }
62
+ function getTemplatePath(entryName, config) {
63
+ const DEFAULT_TEMPLATE = import_path.default.resolve(
64
+ __dirname,
65
+ "../../static/template.html"
66
+ );
67
+ return (0, import_shared.mergeChainedOptions)({
68
+ defaults: DEFAULT_TEMPLATE,
69
+ options: config.html.template,
70
+ utils: { entryName },
71
+ useObjectParam: true
72
+ });
73
+ }
74
+ function getFavicon(entryName, config) {
75
+ return (0, import_shared.mergeChainedOptions)({
76
+ defaults: "",
77
+ options: config.html.favicon,
78
+ utils: { entryName },
79
+ useObjectParam: true
80
+ });
81
+ }
41
82
  const generateMetaTags = (metaOptions) => {
42
83
  if (!metaOptions) {
43
84
  return [];
@@ -59,32 +100,29 @@ async function getMetaTags(entryName, config) {
59
100
  });
60
101
  return generateMetaTags(merged);
61
102
  }
62
- async function getTemplateParameters(entryName, config, assetPrefix) {
63
- const { mountId, templateParameters, templateParametersByEntries } = config.html;
64
- const templateParams = (templateParametersByEntries == null ? void 0 : templateParametersByEntries[entryName]) || templateParameters;
65
- const baseParameters = {
66
- mountId,
67
- entryName,
68
- assetPrefix
69
- };
103
+ function getTemplateParameters(entryName, config, assetPrefix) {
70
104
  return (compilation, assets, assetTags, pluginOptions) => {
105
+ const { mountId, templateParameters } = config.html;
71
106
  const defaultOptions = {
107
+ mountId,
108
+ entryName,
109
+ assetPrefix,
72
110
  compilation,
73
111
  webpackConfig: compilation.options,
74
112
  htmlWebpackPlugin: {
75
113
  tags: assetTags,
76
114
  files: assets,
77
115
  options: pluginOptions
78
- },
79
- ...baseParameters
116
+ }
80
117
  };
81
118
  return (0, import_shared.mergeChainedOptions)({
82
119
  defaults: defaultOptions,
83
- options: templateParams
120
+ options: templateParameters,
121
+ utils: { entryName }
84
122
  });
85
123
  };
86
124
  }
87
- async function getChunks(entryName, entryValue) {
125
+ function getChunks(entryName, entryValue) {
88
126
  const dependOn = [];
89
127
  if ((0, import_shared.isPlainObject)(entryValue)) {
90
128
  dependOn.push(...entryValue.dependOn);
@@ -139,36 +177,19 @@ const pluginHtml = () => ({
139
177
  const entries = chain.entryPoints.entries() || {};
140
178
  const entryNames = Object.keys(entries);
141
179
  const htmlPaths = api.getHTMLPaths();
142
- const faviconUrls = [];
143
- const metaPluginOptions = {
144
- meta: {},
145
- HtmlPlugin
146
- };
147
- const titlePluginOptions = {
148
- titles: {},
149
- HtmlPlugin
150
- };
180
+ const htmlInfoMap = {};
151
181
  await Promise.all(
152
182
  entryNames.map(async (entryName, index) => {
153
183
  const entryValue = entries[entryName].values();
154
- const chunks = await getChunks(entryName, entryValue);
155
- const inject = (0, import_shared.getInject)(entryName, config);
156
- const favicon = (0, import_shared.getFavicon)(entryName, config);
184
+ const chunks = getChunks(entryName, entryValue);
185
+ const inject = getInject(entryName, config);
157
186
  const filename = htmlPaths[entryName];
158
- const template = (0, import_shared.getTemplatePath)(entryName, config);
159
- const metaTags = await getMetaTags(entryName, config);
160
- const title = await (0, import_shared.getTitle)(entryName, config);
161
- const templateParameters = await getTemplateParameters(
187
+ const template = getTemplatePath(entryName, config);
188
+ const templateParameters = getTemplateParameters(
162
189
  entryName,
163
190
  config,
164
191
  assetPrefix
165
192
  );
166
- if (metaTags.length) {
167
- metaPluginOptions.meta[filename] = metaTags;
168
- }
169
- if (title) {
170
- titlePluginOptions.titles[filename] = title;
171
- }
172
193
  const pluginOptions = {
173
194
  chunks,
174
195
  inject,
@@ -178,12 +199,20 @@ const pluginHtml = () => ({
178
199
  templateParameters,
179
200
  scriptLoading: config.html.scriptLoading
180
201
  };
202
+ const htmlInfo = {};
203
+ htmlInfoMap[filename] = htmlInfo;
204
+ const title = getTitle(entryName, config);
205
+ if (title) {
206
+ htmlInfo.title = title;
207
+ }
208
+ const metaTags = await getMetaTags(entryName, config);
209
+ if (metaTags.length) {
210
+ htmlInfo.meta = metaTags;
211
+ }
212
+ const favicon = getFavicon(entryName, config);
181
213
  if (favicon) {
182
214
  if ((0, import_shared.isURL)(favicon)) {
183
- faviconUrls.push({
184
- filename,
185
- url: favicon
186
- });
215
+ htmlInfo.favicon = favicon;
187
216
  } else {
188
217
  pluginOptions.favicon = favicon;
189
218
  }
@@ -205,12 +234,7 @@ const pluginHtml = () => ({
205
234
  chain.plugin(`${CHAIN_ID.PLUGIN.HTML}-${entryName}`).use(HtmlPlugin, [finalOptions]);
206
235
  })
207
236
  );
208
- if (Object.keys(metaPluginOptions.meta).length) {
209
- chain.plugin(CHAIN_ID.PLUGIN.HTML_META).use(import_shared.HtmlMetaPlugin, [metaPluginOptions]);
210
- }
211
- if (Object.keys(titlePluginOptions.titles).length) {
212
- chain.plugin(CHAIN_ID.PLUGIN.HTML_TITLE).use(import_shared.HtmlTitlePlugin, [titlePluginOptions]);
213
- }
237
+ chain.plugin(CHAIN_ID.PLUGIN.HTML_BASIC).use(import_HtmlBasicPlugin.HtmlBasicPlugin, [{ HtmlPlugin, info: htmlInfoMap }]);
214
238
  if (config.security) {
215
239
  const { nonce } = config.security;
216
240
  if (nonce) {
@@ -228,10 +252,6 @@ const pluginHtml = () => ({
228
252
  ]);
229
253
  chain.output.crossOriginLoading(formattedCrossorigin);
230
254
  }
231
- if (faviconUrls.length) {
232
- const { HtmlFaviconUrlPlugin } = await Promise.resolve().then(() => __toESM(require("@rsbuild/shared")));
233
- chain.plugin(CHAIN_ID.PLUGIN.FAVICON_URL).use(HtmlFaviconUrlPlugin, [{ faviconUrls, HtmlPlugin }]);
234
- }
235
255
  if (appIcon) {
236
256
  const { HtmlAppIconPlugin } = await Promise.resolve().then(() => __toESM(require("@rsbuild/shared")));
237
257
  const distDir = (0, import_shared.getDistPath)(config.output, "image");
@@ -259,6 +279,10 @@ const pluginHtml = () => ({
259
279
  0 && (module.exports = {
260
280
  applyInjectTags,
261
281
  generateMetaTags,
282
+ getFavicon,
283
+ getInject,
262
284
  getMetaTags,
285
+ getTemplatePath,
286
+ getTitle,
263
287
  pluginHtml
264
288
  });
@@ -50,7 +50,6 @@ const plugins = {
50
50
  font: () => Promise.resolve().then(() => __toESM(require("./asset"))).then((m) => m.pluginAsset("font", import_shared.FONT_EXTENSIONS)),
51
51
  image: () => Promise.resolve().then(() => __toESM(require("./asset"))).then((m) => m.pluginAsset("image", import_shared.IMAGE_EXTENSIONS)),
52
52
  media: () => Promise.resolve().then(() => __toESM(require("./asset"))).then((m) => m.pluginAsset("media", import_shared.MEDIA_EXTENSIONS)),
53
- assetsRetry: () => Promise.resolve().then(() => __toESM(require("./assetsRetry"))).then((m) => m.pluginAssetsRetry()),
54
53
  rem: () => Promise.resolve().then(() => __toESM(require("./rem"))).then((m) => m.pluginRem()),
55
54
  wasm: () => Promise.resolve().then(() => __toESM(require("./wasm"))).then((m) => m.pluginWasm()),
56
55
  moment: () => Promise.resolve().then(() => __toESM(require("./moment"))).then((m) => m.pluginMoment()),
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
2
  var __defProp = Object.defineProperty;
4
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
6
  var __export = (target, all) => {
9
7
  for (var name in all)
@@ -17,20 +15,13 @@ var __copyProps = (to, from, except, desc) => {
17
15
  }
18
16
  return to;
19
17
  };
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
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
19
  var nodeAddons_exports = {};
30
20
  __export(nodeAddons_exports, {
31
21
  pluginNodeAddons: () => pluginNodeAddons
32
22
  });
33
23
  module.exports = __toCommonJS(nodeAddons_exports);
24
+ var import_path = require("path");
34
25
  var import_shared = require("@rsbuild/shared");
35
26
  const pluginNodeAddons = () => ({
36
27
  name: "plugin-node-addons",
@@ -40,19 +31,21 @@ const pluginNodeAddons = () => ({
40
31
  if (!isServer && !isServiceWorker) {
41
32
  return;
42
33
  }
43
- const pkgUp = await Promise.resolve().then(() => __toESM(require("pkg-up")));
44
- const getDistName = (resource) => {
45
- const pkgJSON = pkgUp.sync({ cwd: resource });
34
+ const getDistName = (resourcePath) => {
35
+ const pkgJSON = (0, import_shared.findUpSync)({
36
+ filename: "package.json",
37
+ cwd: (0, import_path.dirname)(resourcePath)
38
+ });
46
39
  if (!pkgJSON) {
47
40
  throw new Error(
48
41
  `Failed to compile Node.js addons, couldn't find the package.json of ${import_shared.color.yellow(
49
- resource
42
+ resourcePath
50
43
  )}.`
51
44
  );
52
45
  }
53
- const getFilename = (resource2, pkgName2) => {
46
+ const getFilename = (resource, pkgName2) => {
54
47
  const reg = new RegExp(`node_modules/${pkgName2}/(.+)`);
55
- const match = resource2.match(reg);
48
+ const match = resource.match(reg);
56
49
  const filename = match == null ? void 0 : match[1];
57
50
  if (!filename) {
58
51
  return "[name].[ext]";
@@ -62,7 +55,7 @@ const pluginNodeAddons = () => ({
62
55
  const { name: pkgName } = require(pkgJSON);
63
56
  const config = api.getNormalizedConfig();
64
57
  const serverPath = (0, import_shared.getDistPath)(config.output, "server");
65
- return `${serverPath}/${getFilename(resource, pkgName)}`;
58
+ return `${serverPath}/${getFilename(resourcePath, pkgName)}`;
66
59
  };
67
60
  chain.module.rule(CHAIN_ID.RULE.NODE).test(/\.node$/).use(CHAIN_ID.USE.NODE).loader((0, import_shared.getSharedPkgCompiledPath)("node-loader")).options({
68
61
  name: getDistName
@@ -36,6 +36,8 @@ module.exports = __toCommonJS(startUrl_exports);
36
36
  var import_path = require("path");
37
37
  var import_shared = require("@rsbuild/shared");
38
38
  var import_child_process = require("child_process");
39
+ var import_util = require("util");
40
+ const execAsync = (0, import_util.promisify)(import_child_process.exec);
39
41
  const supportedChromiumBrowsers = [
40
42
  "Google Chrome Canary",
41
43
  "Google Chrome Dev",
@@ -46,10 +48,10 @@ const supportedChromiumBrowsers = [
46
48
  "Vivaldi",
47
49
  "Chromium"
48
50
  ];
49
- const getTargetBrowser = () => {
51
+ const getTargetBrowser = async () => {
50
52
  let targetBrowser = process.env.BROWSER;
51
53
  if (!targetBrowser || !supportedChromiumBrowsers.includes(targetBrowser)) {
52
- const ps = (0, import_child_process.execSync)("ps cax").toString();
54
+ const { stdout: ps } = await execAsync("ps cax");
53
55
  targetBrowser = supportedChromiumBrowsers.find((b) => ps.includes(b));
54
56
  }
55
57
  return targetBrowser;
@@ -58,14 +60,13 @@ async function openBrowser(url) {
58
60
  const shouldTryOpenChromeWithAppleScript = process.platform === "darwin";
59
61
  if (shouldTryOpenChromeWithAppleScript) {
60
62
  try {
61
- const targetBrowser = getTargetBrowser();
63
+ const targetBrowser = await getTargetBrowser();
62
64
  if (targetBrowser) {
63
- (0, import_child_process.execSync)(
65
+ await execAsync(
64
66
  `osascript openChrome.applescript "${encodeURI(
65
67
  url
66
68
  )}" "${targetBrowser}"`,
67
69
  {
68
- stdio: "ignore",
69
70
  cwd: (0, import_path.join)(__dirname, "../../static")
70
71
  }
71
72
  );
@@ -93,23 +94,18 @@ const openedURLs = [];
93
94
  function pluginStartUrl() {
94
95
  return {
95
96
  name: "plugin-start-url",
96
- async setup(api) {
97
- let port;
97
+ setup(api) {
98
98
  api.onAfterStartDevServer(async (params) => {
99
- ({ port } = params);
100
- });
101
- api.onDevCompileDone(async ({ isFirstCompile }) => {
102
- if (!isFirstCompile || !port) {
103
- return;
104
- }
99
+ const { port } = params;
105
100
  const config = api.getNormalizedConfig();
106
101
  const { startUrl, beforeStartUrl } = config.dev;
107
- const { https } = api.context.devServer || {};
108
- if (!startUrl) {
102
+ const { open, https } = api.context.devServer || {};
103
+ const shouldOpen = Boolean(startUrl) || open;
104
+ if (!shouldOpen) {
109
105
  return;
110
106
  }
111
107
  const urls = [];
112
- if (startUrl === true) {
108
+ if (startUrl === true || !startUrl) {
113
109
  const protocol = https ? "https" : "http";
114
110
  urls.push(`${protocol}://localhost:${port}`);
115
111
  } else {
@@ -0,0 +1,19 @@
1
+ import type HtmlWebpackPlugin from 'html-webpack-plugin';
2
+ import type { Compiler } from '@rspack/core';
3
+ import type { MetaAttrs } from '@rsbuild/shared';
4
+ export type HtmlInfo = {
5
+ meta?: MetaAttrs[];
6
+ title?: string;
7
+ favicon?: string;
8
+ };
9
+ export type HtmlBasicPluginOptions = {
10
+ info: Record<string, HtmlInfo>;
11
+ HtmlPlugin: typeof HtmlWebpackPlugin;
12
+ };
13
+ export declare class HtmlBasicPlugin {
14
+ readonly name: string;
15
+ readonly options: HtmlBasicPluginOptions;
16
+ readonly HtmlPlugin: typeof HtmlWebpackPlugin;
17
+ constructor(options: HtmlBasicPluginOptions);
18
+ apply(compiler: Compiler): void;
19
+ }
@@ -0,0 +1,87 @@
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 HtmlBasicPlugin_exports = {};
20
+ __export(HtmlBasicPlugin_exports, {
21
+ HtmlBasicPlugin: () => HtmlBasicPlugin
22
+ });
23
+ module.exports = __toCommonJS(HtmlBasicPlugin_exports);
24
+ class HtmlBasicPlugin {
25
+ constructor(options) {
26
+ this.name = "HtmlBasicPlugin";
27
+ this.options = options;
28
+ this.HtmlPlugin = options.HtmlPlugin;
29
+ }
30
+ apply(compiler) {
31
+ const addTitleTag = (headTags, outputName) => {
32
+ const { title } = this.options.info[outputName];
33
+ if (title) {
34
+ headTags.unshift({
35
+ tagName: "title",
36
+ innerHTML: title,
37
+ attributes: {},
38
+ voidTag: false,
39
+ meta: {}
40
+ });
41
+ }
42
+ };
43
+ const addMetaTag = (headTags, outputName) => {
44
+ const { meta } = this.options.info[outputName];
45
+ if (meta) {
46
+ headTags.unshift(
47
+ ...meta.map((attr) => ({
48
+ tagName: "meta",
49
+ attributes: attr,
50
+ meta: {},
51
+ voidTag: true
52
+ }))
53
+ );
54
+ }
55
+ };
56
+ const addFavicon = (headTags, outputName) => {
57
+ const { favicon } = this.options.info[outputName];
58
+ if (favicon) {
59
+ headTags.unshift({
60
+ tagName: "link",
61
+ voidTag: true,
62
+ attributes: {
63
+ rel: "icon",
64
+ href: favicon
65
+ },
66
+ meta: {}
67
+ });
68
+ }
69
+ };
70
+ compiler.hooks.compilation.tap(this.name, (compilation) => {
71
+ this.HtmlPlugin.getHooks(compilation).alterAssetTagGroups.tap(
72
+ this.name,
73
+ (data) => {
74
+ const { headTags, outputName } = data;
75
+ addTitleTag(headTags, outputName);
76
+ addMetaTag(headTags, outputName);
77
+ addFavicon(headTags, outputName);
78
+ return data;
79
+ }
80
+ );
81
+ });
82
+ }
83
+ }
84
+ // Annotate the CommonJS export names for ESM import in node:
85
+ 0 && (module.exports = {
86
+ HtmlBasicPlugin
87
+ });
@@ -43,16 +43,18 @@ async function createCompiler({
43
43
  const { rspack } = await Promise.resolve().then(() => __toESM(require("@rspack/core")));
44
44
  const compiler = rspack(rspackConfigs);
45
45
  let isFirstCompile = true;
46
- compiler.hooks.done.tap("done", async (stats) => {
47
- var _a;
46
+ compiler.hooks.watchRun.tap("rsbuild:compiling", () => {
47
+ import_shared.logger.start("Compiling...");
48
+ });
49
+ compiler.hooks.done.tap("rsbuild:done", async (stats) => {
48
50
  const obj = stats.toJson({
49
51
  all: false,
50
52
  timings: true
51
53
  });
52
- if (!stats.hasErrors()) {
53
- (_a = obj.children) == null ? void 0 : _a.forEach((c, index) => {
54
+ if (!stats.hasErrors() && obj.children) {
55
+ obj.children.forEach((c, index) => {
54
56
  if (c.time) {
55
- const time = (0, import_shared.prettyTime)([0, c.time * 10 ** 6]);
57
+ const time = (0, import_shared.prettyTime)(c.time / 1e3);
56
58
  const target = Array.isArray(context.target) ? context.target[index] : context.target;
57
59
  const name = import_shared.TARGET_ID_MAP[target || "web"];
58
60
  import_shared.logger.ready(`${name} compiled in ${time}`);
@@ -1,5 +1,3 @@
1
- import type { ModernDevServerOptions } from '@modern-js/server';
1
+ import { DevMiddleware } from '@rsbuild/shared';
2
2
  import type { Compiler, MultiCompiler } from '@rspack/core';
3
- type DevMiddlewareOptions = ModernDevServerOptions['devMiddleware'];
4
- export declare const getDevMiddleware: (multiCompiler: Compiler | MultiCompiler) => NonNullable<DevMiddlewareOptions>;
5
- export {};
3
+ export declare const getDevMiddleware: (multiCompiler: Compiler | MultiCompiler) => NonNullable<DevMiddleware>;
@@ -1,6 +1,5 @@
1
1
  import { StartDevServerOptions, type RspackCompiler, type RspackMultiCompiler } from '@rsbuild/shared';
2
2
  import { type InitConfigsOptions } from './initConfigs';
3
3
  type ServerOptions = Exclude<StartDevServerOptions['serverOptions'], undefined>;
4
- export declare function createDevServer(options: InitConfigsOptions, port: number, serverOptions: ServerOptions, customCompiler?: RspackCompiler | RspackMultiCompiler): Promise<import("@modern-js/server").Server>;
5
- export declare function startDevServer(options: InitConfigsOptions, startDevServerOptions?: StartDevServerOptions): Promise<import("@rsbuild/shared").StartServerResult>;
4
+ export declare function createDevServer(options: InitConfigsOptions, port: number, serverOptions: ServerOptions, customCompiler?: RspackCompiler | RspackMultiCompiler): Promise<import("@rsbuild/shared").ServerApi>;
6
5
  export {};