@rsbuild/core 1.0.3 → 1.0.5

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
@@ -2304,7 +2304,7 @@ async function createContext(options, userConfig, bundlerType) {
2304
2304
  const rsbuildConfig = await withDefaultConfig(rootPath, userConfig);
2305
2305
  const cachePath = (0, import_node_path9.join)(rootPath, "node_modules", ".cache");
2306
2306
  return {
2307
- version: "1.0.3",
2307
+ version: "1.0.5",
2308
2308
  rootPath,
2309
2309
  distPath: "",
2310
2310
  cachePath,
@@ -4184,7 +4184,11 @@ var init_helper = __esm({
4184
4184
  init_helpers();
4185
4185
  init_logger();
4186
4186
  normalizeUrl = (url2) => url2.replace(/([^:]\/)\/+/g, "$1");
4187
- formatPrefix = (prefix) => {
4187
+ formatPrefix = (input) => {
4188
+ let prefix = input;
4189
+ if (prefix?.startsWith("./")) {
4190
+ prefix = prefix.replace("./", "");
4191
+ }
4188
4192
  if (!prefix) {
4189
4193
  return "/";
4190
4194
  }
@@ -4404,6 +4408,143 @@ var init_httpServer = __esm({
4404
4408
  }
4405
4409
  });
4406
4410
 
4411
+ // src/server/open.ts
4412
+ async function openBrowser(url2) {
4413
+ const shouldTryOpenChromeWithAppleScript = process.platform === "darwin";
4414
+ if (shouldTryOpenChromeWithAppleScript) {
4415
+ try {
4416
+ const targetBrowser = await getTargetBrowser();
4417
+ if (targetBrowser) {
4418
+ await execAsync(
4419
+ `osascript openChrome.applescript "${encodeURI(
4420
+ url2
4421
+ )}" "${targetBrowser}"`,
4422
+ {
4423
+ cwd: STATIC_PATH
4424
+ }
4425
+ );
4426
+ return true;
4427
+ }
4428
+ import_rslog.logger.debug("Failed to find the target browser.");
4429
+ } catch (err) {
4430
+ import_rslog.logger.debug("Failed to open start URL with apple script.");
4431
+ import_rslog.logger.debug(err);
4432
+ }
4433
+ }
4434
+ try {
4435
+ const { default: open2 } = await import("../compiled/open/index.js");
4436
+ await open2(url2);
4437
+ return true;
4438
+ } catch (err) {
4439
+ import_rslog.logger.error("Failed to open start URL.");
4440
+ import_rslog.logger.error(err);
4441
+ return false;
4442
+ }
4443
+ }
4444
+ function resolveUrl(str, base) {
4445
+ if (canParse(str)) {
4446
+ return str;
4447
+ }
4448
+ try {
4449
+ const url2 = new URL(str, base);
4450
+ return url2.href;
4451
+ } catch (e) {
4452
+ throw new Error(
4453
+ "[rsbuild:open]: Invalid input: not a valid URL or pathname"
4454
+ );
4455
+ }
4456
+ }
4457
+ async function open({
4458
+ https,
4459
+ port,
4460
+ routes,
4461
+ config,
4462
+ clearCache
4463
+ }) {
4464
+ const { targets, before } = normalizeOpenConfig(config);
4465
+ const isCodesandbox = process.env.CSB === "true";
4466
+ if (isCodesandbox) {
4467
+ return;
4468
+ }
4469
+ if (clearCache) {
4470
+ clearOpenedURLs();
4471
+ }
4472
+ const urls = [];
4473
+ const protocol = https ? "https" : "http";
4474
+ const baseUrl = `${protocol}://localhost:${port}`;
4475
+ if (!targets.length) {
4476
+ if (routes.length) {
4477
+ urls.push(`${baseUrl}${routes[0].pathname}`);
4478
+ }
4479
+ } else {
4480
+ urls.push(
4481
+ ...targets.map(
4482
+ (target) => resolveUrl(replacePortPlaceholder(target, port), baseUrl)
4483
+ )
4484
+ );
4485
+ }
4486
+ if (before) {
4487
+ await before();
4488
+ }
4489
+ for (const url2 of urls) {
4490
+ if (!openedURLs.includes(url2)) {
4491
+ openBrowser(url2);
4492
+ openedURLs.push(url2);
4493
+ }
4494
+ }
4495
+ }
4496
+ var import_node_child_process, import_node_util, execAsync, supportedChromiumBrowsers, getTargetBrowser, openedURLs, clearOpenedURLs, replacePortPlaceholder, normalizeOpenConfig;
4497
+ var init_open = __esm({
4498
+ "src/server/open.ts"() {
4499
+ "use strict";
4500
+ import_node_child_process = require("child_process");
4501
+ import_node_util = require("util");
4502
+ init_constants();
4503
+ init_helpers();
4504
+ init_logger();
4505
+ execAsync = (0, import_node_util.promisify)(import_node_child_process.exec);
4506
+ supportedChromiumBrowsers = [
4507
+ "Google Chrome Canary",
4508
+ "Google Chrome Dev",
4509
+ "Google Chrome Beta",
4510
+ "Google Chrome",
4511
+ "Microsoft Edge",
4512
+ "Brave Browser",
4513
+ "Vivaldi",
4514
+ "Chromium"
4515
+ ];
4516
+ getTargetBrowser = async () => {
4517
+ let targetBrowser = process.env.BROWSER;
4518
+ if (!targetBrowser || !supportedChromiumBrowsers.includes(targetBrowser)) {
4519
+ const { stdout: ps } = await execAsync("ps cax");
4520
+ targetBrowser = supportedChromiumBrowsers.find((b) => ps.includes(b));
4521
+ }
4522
+ return targetBrowser;
4523
+ };
4524
+ openedURLs = [];
4525
+ clearOpenedURLs = () => {
4526
+ openedURLs = [];
4527
+ };
4528
+ replacePortPlaceholder = (url2, port) => url2.replace(/<port>/g, String(port));
4529
+ normalizeOpenConfig = (config) => {
4530
+ const { open: open2 } = config.server;
4531
+ if (typeof open2 === "boolean") {
4532
+ return { targets: [] };
4533
+ }
4534
+ if (typeof open2 === "string") {
4535
+ return { targets: [open2] };
4536
+ }
4537
+ if (Array.isArray(open2)) {
4538
+ return { targets: open2 };
4539
+ }
4540
+ return {
4541
+ targets: open2.target ? castArray(open2.target) : [],
4542
+ before: open2.before
4543
+ };
4544
+ };
4545
+ }
4546
+ });
4547
+
4407
4548
  // src/server/watchFiles.ts
4408
4549
  async function setupWatchFiles(options) {
4409
4550
  const { dev, server, compileMiddlewareAPI } = options;
@@ -5076,7 +5217,16 @@ async function createDevServer(options, createCompiler2, config, {
5076
5217
  await options.context.hooks.onCloseDevServer.call();
5077
5218
  await Promise.all([devMiddlewares.close(), fileWatcher?.close()]);
5078
5219
  },
5079
- printUrls
5220
+ printUrls,
5221
+ open: async () => {
5222
+ return open({
5223
+ https,
5224
+ port,
5225
+ routes,
5226
+ config,
5227
+ clearCache: true
5228
+ });
5229
+ }
5080
5230
  };
5081
5231
  import_rslog.logger.debug("create dev server done");
5082
5232
  return devServerAPI;
@@ -5094,6 +5244,7 @@ var init_devServer = __esm({
5094
5244
  init_helper();
5095
5245
  init_httpServer();
5096
5246
  init_middlewares();
5247
+ init_open();
5097
5248
  init_restart();
5098
5249
  init_watchFiles();
5099
5250
  formatDevConfig = (config, port) => {
@@ -5766,157 +5917,6 @@ var init_css = __esm({
5766
5917
  }
5767
5918
  });
5768
5919
 
5769
- // src/plugins/open.ts
5770
- var open_exports = {};
5771
- __export(open_exports, {
5772
- openBrowser: () => openBrowser,
5773
- pluginOpen: () => pluginOpen,
5774
- replacePortPlaceholder: () => replacePortPlaceholder,
5775
- resolveUrl: () => resolveUrl
5776
- });
5777
- async function openBrowser(url2) {
5778
- const shouldTryOpenChromeWithAppleScript = process.platform === "darwin";
5779
- if (shouldTryOpenChromeWithAppleScript) {
5780
- try {
5781
- const targetBrowser = await getTargetBrowser();
5782
- if (targetBrowser) {
5783
- await execAsync(
5784
- `osascript openChrome.applescript "${encodeURI(
5785
- url2
5786
- )}" "${targetBrowser}"`,
5787
- {
5788
- cwd: STATIC_PATH
5789
- }
5790
- );
5791
- return true;
5792
- }
5793
- import_rslog.logger.debug("Failed to find the target browser.");
5794
- } catch (err) {
5795
- import_rslog.logger.debug("Failed to open start URL with apple script.");
5796
- import_rslog.logger.debug(err);
5797
- }
5798
- }
5799
- try {
5800
- const { default: open } = await import("../compiled/open/index.js");
5801
- await open(url2);
5802
- return true;
5803
- } catch (err) {
5804
- import_rslog.logger.error("Failed to open start URL.");
5805
- import_rslog.logger.error(err);
5806
- return false;
5807
- }
5808
- }
5809
- function resolveUrl(str, base) {
5810
- if (canParse(str)) {
5811
- return str;
5812
- }
5813
- try {
5814
- const url2 = new URL(str, base);
5815
- return url2.href;
5816
- } catch (e) {
5817
- throw new Error(
5818
- "[rsbuild:open]: Invalid input: not a valid URL or pathname"
5819
- );
5820
- }
5821
- }
5822
- function pluginOpen() {
5823
- return {
5824
- name: "rsbuild:open",
5825
- setup(api) {
5826
- const onStartServer = async (params) => {
5827
- const { port, routes } = params;
5828
- const config = api.getNormalizedConfig();
5829
- const { https } = api.context.devServer || {};
5830
- const { targets, before } = normalizeOpenConfig(config);
5831
- const isCodesandbox = process.env.CSB === "true";
5832
- const shouldOpen = targets !== void 0 && !isCodesandbox;
5833
- if (!shouldOpen) {
5834
- return;
5835
- }
5836
- const urls = [];
5837
- const protocol = https ? "https" : "http";
5838
- const baseUrl = `${protocol}://localhost:${port}`;
5839
- if (!targets.length) {
5840
- if (routes.length) {
5841
- urls.push(`${baseUrl}${routes[0].pathname}`);
5842
- }
5843
- } else {
5844
- urls.push(
5845
- ...targets.map(
5846
- (target) => resolveUrl(replacePortPlaceholder(target, port), baseUrl)
5847
- )
5848
- );
5849
- }
5850
- const openUrls = () => {
5851
- for (const url2 of urls) {
5852
- if (!openedURLs.includes(url2)) {
5853
- openBrowser(url2);
5854
- openedURLs.push(url2);
5855
- }
5856
- }
5857
- };
5858
- if (before) {
5859
- await before();
5860
- }
5861
- openUrls();
5862
- };
5863
- api.onAfterStartDevServer(onStartServer);
5864
- api.onAfterStartProdServer(onStartServer);
5865
- }
5866
- };
5867
- }
5868
- var import_node_child_process, import_node_util, execAsync, supportedChromiumBrowsers, getTargetBrowser, replacePortPlaceholder, openedURLs, normalizeOpenConfig;
5869
- var init_open = __esm({
5870
- "src/plugins/open.ts"() {
5871
- "use strict";
5872
- import_node_child_process = require("child_process");
5873
- import_node_util = require("util");
5874
- init_constants();
5875
- init_helpers();
5876
- init_logger();
5877
- execAsync = (0, import_node_util.promisify)(import_node_child_process.exec);
5878
- supportedChromiumBrowsers = [
5879
- "Google Chrome Canary",
5880
- "Google Chrome Dev",
5881
- "Google Chrome Beta",
5882
- "Google Chrome",
5883
- "Microsoft Edge",
5884
- "Brave Browser",
5885
- "Vivaldi",
5886
- "Chromium"
5887
- ];
5888
- getTargetBrowser = async () => {
5889
- let targetBrowser = process.env.BROWSER;
5890
- if (!targetBrowser || !supportedChromiumBrowsers.includes(targetBrowser)) {
5891
- const { stdout: ps } = await execAsync("ps cax");
5892
- targetBrowser = supportedChromiumBrowsers.find((b) => ps.includes(b));
5893
- }
5894
- return targetBrowser;
5895
- };
5896
- replacePortPlaceholder = (url2, port) => url2.replace(/<port>/g, String(port));
5897
- openedURLs = [];
5898
- normalizeOpenConfig = (config) => {
5899
- const { open } = config.server;
5900
- if (open === false) {
5901
- return {};
5902
- }
5903
- if (open === true) {
5904
- return { targets: [] };
5905
- }
5906
- if (typeof open === "string") {
5907
- return { targets: [open] };
5908
- }
5909
- if (Array.isArray(open)) {
5910
- return { targets: open };
5911
- }
5912
- return {
5913
- targets: open.target ? castArray(open.target) : [],
5914
- before: open.before
5915
- };
5916
- };
5917
- }
5918
- });
5919
-
5920
5920
  // src/plugins/output.ts
5921
5921
  var output_exports = {};
5922
5922
  __export(output_exports, {
@@ -5957,8 +5957,8 @@ var init_output = __esm({
5957
5957
  init_constants();
5958
5958
  init_helpers();
5959
5959
  init_pluginHelper();
5960
- init_css();
5961
5960
  init_open();
5961
+ init_css();
5962
5962
  getJsAsyncPath = (jsPath, isServer, jsAsync) => {
5963
5963
  if (jsAsync !== void 0) {
5964
5964
  return jsAsync;
@@ -7500,9 +7500,6 @@ function getDefaultSwcConfig(browserslist3, cacheRoot) {
7500
7500
  syntax: "typescript",
7501
7501
  decorators: true
7502
7502
  },
7503
- // Avoid the webpack magic comment to be removed
7504
- // https://github.com/swc-project/swc/issues/6403
7505
- preserveAllComments: true,
7506
7503
  experimental: {
7507
7504
  cacheRoot
7508
7505
  }
@@ -8598,9 +8595,23 @@ var init_server = __esm({
8598
8595
  import_node_fs9 = __toESM(require("fs"));
8599
8596
  import_node_path33 = require("path");
8600
8597
  init_config();
8598
+ init_open();
8601
8599
  pluginServer = () => ({
8602
8600
  name: "rsbuild:server",
8603
8601
  setup(api) {
8602
+ const onStartServer = async ({ port, routes }) => {
8603
+ const config = api.getNormalizedConfig();
8604
+ if (config.server.open) {
8605
+ open({
8606
+ https: api.context.devServer?.https,
8607
+ port,
8608
+ routes,
8609
+ config
8610
+ });
8611
+ }
8612
+ };
8613
+ api.onAfterStartDevServer(onStartServer);
8614
+ api.onAfterStartProdServer(onStartServer);
8604
8615
  api.onBeforeBuild(async ({ isFirstCompile }) => {
8605
8616
  if (!isFirstCompile) {
8606
8617
  return;
@@ -9419,7 +9430,6 @@ async function applyDefaultPlugins(pluginManager, context) {
9419
9430
  Promise.resolve().then(() => (init_splitChunks(), splitChunks_exports)).then(
9420
9431
  ({ pluginSplitChunks: pluginSplitChunks2 }) => pluginSplitChunks2()
9421
9432
  ),
9422
- Promise.resolve().then(() => (init_open(), open_exports)).then(({ pluginOpen: pluginOpen2 }) => pluginOpen2()),
9423
9433
  Promise.resolve().then(() => (init_inlineChunk(), inlineChunk_exports)).then(
9424
9434
  ({ pluginInlineChunk: pluginInlineChunk2 }) => pluginInlineChunk2()
9425
9435
  ),
@@ -9675,7 +9685,7 @@ var init_init = __esm({
9675
9685
 
9676
9686
  // src/cli/commands.ts
9677
9687
  function runCli() {
9678
- import_commander.program.name("rsbuild").usage("<command> [options]").version("1.0.3");
9688
+ import_commander.program.name("rsbuild").usage("<command> [options]").version("1.0.5");
9679
9689
  const devCommand = import_commander.program.command("dev");
9680
9690
  const buildCommand = import_commander.program.command("build");
9681
9691
  const previewCommand = import_commander.program.command("preview");
@@ -9798,7 +9808,7 @@ function prepareCli() {
9798
9808
  if (!npm_execpath || npm_execpath.includes("npx-cli.js") || npm_execpath.includes(".bun")) {
9799
9809
  console.log();
9800
9810
  }
9801
- import_rslog.logger.greet(` ${`Rsbuild v${"1.0.3"}`}
9811
+ import_rslog.logger.greet(` ${`Rsbuild v${"1.0.5"}`}
9802
9812
  `);
9803
9813
  }
9804
9814
  var init_prepare = __esm({
@@ -9882,7 +9892,7 @@ init_logger();
9882
9892
  init_mergeConfig();
9883
9893
  init_helpers();
9884
9894
  init_constants();
9885
- var version = "1.0.3";
9895
+ var version = "1.0.5";
9886
9896
  // Annotate the CommonJS export names for ESM import in node:
9887
9897
  0 && (module.exports = {
9888
9898
  PLUGIN_CSS_NAME,