@rsbuild/core 1.0.4 → 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.4",
2307
+ version: "1.0.5",
2308
2308
  rootPath,
2309
2309
  distPath: "",
2310
2310
  cachePath,
@@ -4408,6 +4408,143 @@ var init_httpServer = __esm({
4408
4408
  }
4409
4409
  });
4410
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
+
4411
4548
  // src/server/watchFiles.ts
4412
4549
  async function setupWatchFiles(options) {
4413
4550
  const { dev, server, compileMiddlewareAPI } = options;
@@ -5080,7 +5217,16 @@ async function createDevServer(options, createCompiler2, config, {
5080
5217
  await options.context.hooks.onCloseDevServer.call();
5081
5218
  await Promise.all([devMiddlewares.close(), fileWatcher?.close()]);
5082
5219
  },
5083
- printUrls
5220
+ printUrls,
5221
+ open: async () => {
5222
+ return open({
5223
+ https,
5224
+ port,
5225
+ routes,
5226
+ config,
5227
+ clearCache: true
5228
+ });
5229
+ }
5084
5230
  };
5085
5231
  import_rslog.logger.debug("create dev server done");
5086
5232
  return devServerAPI;
@@ -5098,6 +5244,7 @@ var init_devServer = __esm({
5098
5244
  init_helper();
5099
5245
  init_httpServer();
5100
5246
  init_middlewares();
5247
+ init_open();
5101
5248
  init_restart();
5102
5249
  init_watchFiles();
5103
5250
  formatDevConfig = (config, port) => {
@@ -5770,157 +5917,6 @@ var init_css = __esm({
5770
5917
  }
5771
5918
  });
5772
5919
 
5773
- // src/plugins/open.ts
5774
- var open_exports = {};
5775
- __export(open_exports, {
5776
- openBrowser: () => openBrowser,
5777
- pluginOpen: () => pluginOpen,
5778
- replacePortPlaceholder: () => replacePortPlaceholder,
5779
- resolveUrl: () => resolveUrl
5780
- });
5781
- async function openBrowser(url2) {
5782
- const shouldTryOpenChromeWithAppleScript = process.platform === "darwin";
5783
- if (shouldTryOpenChromeWithAppleScript) {
5784
- try {
5785
- const targetBrowser = await getTargetBrowser();
5786
- if (targetBrowser) {
5787
- await execAsync(
5788
- `osascript openChrome.applescript "${encodeURI(
5789
- url2
5790
- )}" "${targetBrowser}"`,
5791
- {
5792
- cwd: STATIC_PATH
5793
- }
5794
- );
5795
- return true;
5796
- }
5797
- import_rslog.logger.debug("Failed to find the target browser.");
5798
- } catch (err) {
5799
- import_rslog.logger.debug("Failed to open start URL with apple script.");
5800
- import_rslog.logger.debug(err);
5801
- }
5802
- }
5803
- try {
5804
- const { default: open } = await import("../compiled/open/index.js");
5805
- await open(url2);
5806
- return true;
5807
- } catch (err) {
5808
- import_rslog.logger.error("Failed to open start URL.");
5809
- import_rslog.logger.error(err);
5810
- return false;
5811
- }
5812
- }
5813
- function resolveUrl(str, base) {
5814
- if (canParse(str)) {
5815
- return str;
5816
- }
5817
- try {
5818
- const url2 = new URL(str, base);
5819
- return url2.href;
5820
- } catch (e) {
5821
- throw new Error(
5822
- "[rsbuild:open]: Invalid input: not a valid URL or pathname"
5823
- );
5824
- }
5825
- }
5826
- function pluginOpen() {
5827
- return {
5828
- name: "rsbuild:open",
5829
- setup(api) {
5830
- const onStartServer = async (params) => {
5831
- const { port, routes } = params;
5832
- const config = api.getNormalizedConfig();
5833
- const { https } = api.context.devServer || {};
5834
- const { targets, before } = normalizeOpenConfig(config);
5835
- const isCodesandbox = process.env.CSB === "true";
5836
- const shouldOpen = targets !== void 0 && !isCodesandbox;
5837
- if (!shouldOpen) {
5838
- return;
5839
- }
5840
- const urls = [];
5841
- const protocol = https ? "https" : "http";
5842
- const baseUrl = `${protocol}://localhost:${port}`;
5843
- if (!targets.length) {
5844
- if (routes.length) {
5845
- urls.push(`${baseUrl}${routes[0].pathname}`);
5846
- }
5847
- } else {
5848
- urls.push(
5849
- ...targets.map(
5850
- (target) => resolveUrl(replacePortPlaceholder(target, port), baseUrl)
5851
- )
5852
- );
5853
- }
5854
- const openUrls = () => {
5855
- for (const url2 of urls) {
5856
- if (!openedURLs.includes(url2)) {
5857
- openBrowser(url2);
5858
- openedURLs.push(url2);
5859
- }
5860
- }
5861
- };
5862
- if (before) {
5863
- await before();
5864
- }
5865
- openUrls();
5866
- };
5867
- api.onAfterStartDevServer(onStartServer);
5868
- api.onAfterStartProdServer(onStartServer);
5869
- }
5870
- };
5871
- }
5872
- var import_node_child_process, import_node_util, execAsync, supportedChromiumBrowsers, getTargetBrowser, replacePortPlaceholder, openedURLs, normalizeOpenConfig;
5873
- var init_open = __esm({
5874
- "src/plugins/open.ts"() {
5875
- "use strict";
5876
- import_node_child_process = require("child_process");
5877
- import_node_util = require("util");
5878
- init_constants();
5879
- init_helpers();
5880
- init_logger();
5881
- execAsync = (0, import_node_util.promisify)(import_node_child_process.exec);
5882
- supportedChromiumBrowsers = [
5883
- "Google Chrome Canary",
5884
- "Google Chrome Dev",
5885
- "Google Chrome Beta",
5886
- "Google Chrome",
5887
- "Microsoft Edge",
5888
- "Brave Browser",
5889
- "Vivaldi",
5890
- "Chromium"
5891
- ];
5892
- getTargetBrowser = async () => {
5893
- let targetBrowser = process.env.BROWSER;
5894
- if (!targetBrowser || !supportedChromiumBrowsers.includes(targetBrowser)) {
5895
- const { stdout: ps } = await execAsync("ps cax");
5896
- targetBrowser = supportedChromiumBrowsers.find((b) => ps.includes(b));
5897
- }
5898
- return targetBrowser;
5899
- };
5900
- replacePortPlaceholder = (url2, port) => url2.replace(/<port>/g, String(port));
5901
- openedURLs = [];
5902
- normalizeOpenConfig = (config) => {
5903
- const { open } = config.server;
5904
- if (open === false) {
5905
- return {};
5906
- }
5907
- if (open === true) {
5908
- return { targets: [] };
5909
- }
5910
- if (typeof open === "string") {
5911
- return { targets: [open] };
5912
- }
5913
- if (Array.isArray(open)) {
5914
- return { targets: open };
5915
- }
5916
- return {
5917
- targets: open.target ? castArray(open.target) : [],
5918
- before: open.before
5919
- };
5920
- };
5921
- }
5922
- });
5923
-
5924
5920
  // src/plugins/output.ts
5925
5921
  var output_exports = {};
5926
5922
  __export(output_exports, {
@@ -5961,8 +5957,8 @@ var init_output = __esm({
5961
5957
  init_constants();
5962
5958
  init_helpers();
5963
5959
  init_pluginHelper();
5964
- init_css();
5965
5960
  init_open();
5961
+ init_css();
5966
5962
  getJsAsyncPath = (jsPath, isServer, jsAsync) => {
5967
5963
  if (jsAsync !== void 0) {
5968
5964
  return jsAsync;
@@ -7504,9 +7500,6 @@ function getDefaultSwcConfig(browserslist3, cacheRoot) {
7504
7500
  syntax: "typescript",
7505
7501
  decorators: true
7506
7502
  },
7507
- // Avoid the webpack magic comment to be removed
7508
- // https://github.com/swc-project/swc/issues/6403
7509
- preserveAllComments: true,
7510
7503
  experimental: {
7511
7504
  cacheRoot
7512
7505
  }
@@ -8602,9 +8595,23 @@ var init_server = __esm({
8602
8595
  import_node_fs9 = __toESM(require("fs"));
8603
8596
  import_node_path33 = require("path");
8604
8597
  init_config();
8598
+ init_open();
8605
8599
  pluginServer = () => ({
8606
8600
  name: "rsbuild:server",
8607
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);
8608
8615
  api.onBeforeBuild(async ({ isFirstCompile }) => {
8609
8616
  if (!isFirstCompile) {
8610
8617
  return;
@@ -9423,7 +9430,6 @@ async function applyDefaultPlugins(pluginManager, context) {
9423
9430
  Promise.resolve().then(() => (init_splitChunks(), splitChunks_exports)).then(
9424
9431
  ({ pluginSplitChunks: pluginSplitChunks2 }) => pluginSplitChunks2()
9425
9432
  ),
9426
- Promise.resolve().then(() => (init_open(), open_exports)).then(({ pluginOpen: pluginOpen2 }) => pluginOpen2()),
9427
9433
  Promise.resolve().then(() => (init_inlineChunk(), inlineChunk_exports)).then(
9428
9434
  ({ pluginInlineChunk: pluginInlineChunk2 }) => pluginInlineChunk2()
9429
9435
  ),
@@ -9679,7 +9685,7 @@ var init_init = __esm({
9679
9685
 
9680
9686
  // src/cli/commands.ts
9681
9687
  function runCli() {
9682
- import_commander.program.name("rsbuild").usage("<command> [options]").version("1.0.4");
9688
+ import_commander.program.name("rsbuild").usage("<command> [options]").version("1.0.5");
9683
9689
  const devCommand = import_commander.program.command("dev");
9684
9690
  const buildCommand = import_commander.program.command("build");
9685
9691
  const previewCommand = import_commander.program.command("preview");
@@ -9802,7 +9808,7 @@ function prepareCli() {
9802
9808
  if (!npm_execpath || npm_execpath.includes("npx-cli.js") || npm_execpath.includes(".bun")) {
9803
9809
  console.log();
9804
9810
  }
9805
- import_rslog.logger.greet(` ${`Rsbuild v${"1.0.4"}`}
9811
+ import_rslog.logger.greet(` ${`Rsbuild v${"1.0.5"}`}
9806
9812
  `);
9807
9813
  }
9808
9814
  var init_prepare = __esm({
@@ -9886,7 +9892,7 @@ init_logger();
9886
9892
  init_mergeConfig();
9887
9893
  init_helpers();
9888
9894
  init_constants();
9889
- var version = "1.0.4";
9895
+ var version = "1.0.5";
9890
9896
  // Annotate the CommonJS export names for ESM import in node:
9891
9897
  0 && (module.exports = {
9892
9898
  PLUGIN_CSS_NAME,