@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.js CHANGED
@@ -46,12 +46,12 @@ var __publicField = (obj, key, value) => {
46
46
  return value;
47
47
  };
48
48
 
49
- // ../../node_modules/.pnpm/@modern-js+module-tools@2.59.0_typescript@5.5.2/node_modules/@modern-js/module-tools/shims/esm.js
49
+ // ../../node_modules/.pnpm/@modern-js+module-tools@2.60.0_typescript@5.5.2/node_modules/@modern-js/module-tools/shims/esm.js
50
50
  import path from "path";
51
51
  import { fileURLToPath } from "url";
52
52
  var getFilename, getDirname, __dirname, __filename;
53
53
  var init_esm = __esm({
54
- "../../node_modules/.pnpm/@modern-js+module-tools@2.59.0_typescript@5.5.2/node_modules/@modern-js/module-tools/shims/esm.js"() {
54
+ "../../node_modules/.pnpm/@modern-js+module-tools@2.60.0_typescript@5.5.2/node_modules/@modern-js/module-tools/shims/esm.js"() {
55
55
  "use strict";
56
56
  getFilename = () => fileURLToPath(import.meta.url);
57
57
  getDirname = () => path.dirname(getFilename());
@@ -2342,7 +2342,7 @@ async function createContext(options, userConfig, bundlerType) {
2342
2342
  const rsbuildConfig = await withDefaultConfig(rootPath, userConfig);
2343
2343
  const cachePath = join6(rootPath, "node_modules", ".cache");
2344
2344
  return {
2345
- version: "1.0.4",
2345
+ version: "1.0.5",
2346
2346
  rootPath,
2347
2347
  distPath: "",
2348
2348
  cachePath,
@@ -4463,6 +4463,144 @@ var init_httpServer = __esm({
4463
4463
  }
4464
4464
  });
4465
4465
 
4466
+ // src/server/open.ts
4467
+ import { exec } from "child_process";
4468
+ import { promisify } from "util";
4469
+ async function openBrowser(url2) {
4470
+ const shouldTryOpenChromeWithAppleScript = process.platform === "darwin";
4471
+ if (shouldTryOpenChromeWithAppleScript) {
4472
+ try {
4473
+ const targetBrowser = await getTargetBrowser();
4474
+ if (targetBrowser) {
4475
+ await execAsync(
4476
+ `osascript openChrome.applescript "${encodeURI(
4477
+ url2
4478
+ )}" "${targetBrowser}"`,
4479
+ {
4480
+ cwd: STATIC_PATH
4481
+ }
4482
+ );
4483
+ return true;
4484
+ }
4485
+ logger.debug("Failed to find the target browser.");
4486
+ } catch (err) {
4487
+ logger.debug("Failed to open start URL with apple script.");
4488
+ logger.debug(err);
4489
+ }
4490
+ }
4491
+ try {
4492
+ const { default: open2 } = await import("../compiled/open/index.js");
4493
+ await open2(url2);
4494
+ return true;
4495
+ } catch (err) {
4496
+ logger.error("Failed to open start URL.");
4497
+ logger.error(err);
4498
+ return false;
4499
+ }
4500
+ }
4501
+ function resolveUrl(str, base) {
4502
+ if (canParse(str)) {
4503
+ return str;
4504
+ }
4505
+ try {
4506
+ const url2 = new URL(str, base);
4507
+ return url2.href;
4508
+ } catch (e) {
4509
+ throw new Error(
4510
+ "[rsbuild:open]: Invalid input: not a valid URL or pathname"
4511
+ );
4512
+ }
4513
+ }
4514
+ async function open({
4515
+ https,
4516
+ port,
4517
+ routes,
4518
+ config,
4519
+ clearCache
4520
+ }) {
4521
+ const { targets, before } = normalizeOpenConfig(config);
4522
+ const isCodesandbox = process.env.CSB === "true";
4523
+ if (isCodesandbox) {
4524
+ return;
4525
+ }
4526
+ if (clearCache) {
4527
+ clearOpenedURLs();
4528
+ }
4529
+ const urls = [];
4530
+ const protocol = https ? "https" : "http";
4531
+ const baseUrl = `${protocol}://localhost:${port}`;
4532
+ if (!targets.length) {
4533
+ if (routes.length) {
4534
+ urls.push(`${baseUrl}${routes[0].pathname}`);
4535
+ }
4536
+ } else {
4537
+ urls.push(
4538
+ ...targets.map(
4539
+ (target) => resolveUrl(replacePortPlaceholder(target, port), baseUrl)
4540
+ )
4541
+ );
4542
+ }
4543
+ if (before) {
4544
+ await before();
4545
+ }
4546
+ for (const url2 of urls) {
4547
+ if (!openedURLs.includes(url2)) {
4548
+ openBrowser(url2);
4549
+ openedURLs.push(url2);
4550
+ }
4551
+ }
4552
+ }
4553
+ var execAsync, supportedChromiumBrowsers, getTargetBrowser, openedURLs, clearOpenedURLs, replacePortPlaceholder, normalizeOpenConfig;
4554
+ var init_open = __esm({
4555
+ "src/server/open.ts"() {
4556
+ "use strict";
4557
+ init_esm();
4558
+ init_constants();
4559
+ init_helpers();
4560
+ init_logger();
4561
+ execAsync = promisify(exec);
4562
+ supportedChromiumBrowsers = [
4563
+ "Google Chrome Canary",
4564
+ "Google Chrome Dev",
4565
+ "Google Chrome Beta",
4566
+ "Google Chrome",
4567
+ "Microsoft Edge",
4568
+ "Brave Browser",
4569
+ "Vivaldi",
4570
+ "Chromium"
4571
+ ];
4572
+ getTargetBrowser = async () => {
4573
+ let targetBrowser = process.env.BROWSER;
4574
+ if (!targetBrowser || !supportedChromiumBrowsers.includes(targetBrowser)) {
4575
+ const { stdout: ps } = await execAsync("ps cax");
4576
+ targetBrowser = supportedChromiumBrowsers.find((b) => ps.includes(b));
4577
+ }
4578
+ return targetBrowser;
4579
+ };
4580
+ openedURLs = [];
4581
+ clearOpenedURLs = () => {
4582
+ openedURLs = [];
4583
+ };
4584
+ replacePortPlaceholder = (url2, port) => url2.replace(/<port>/g, String(port));
4585
+ normalizeOpenConfig = (config) => {
4586
+ const { open: open2 } = config.server;
4587
+ if (typeof open2 === "boolean") {
4588
+ return { targets: [] };
4589
+ }
4590
+ if (typeof open2 === "string") {
4591
+ return { targets: [open2] };
4592
+ }
4593
+ if (Array.isArray(open2)) {
4594
+ return { targets: open2 };
4595
+ }
4596
+ return {
4597
+ targets: open2.target ? castArray(open2.target) : [],
4598
+ before: open2.before
4599
+ };
4600
+ };
4601
+ }
4602
+ });
4603
+
4466
4604
  // src/server/watchFiles.ts
4467
4605
  async function setupWatchFiles(options) {
4468
4606
  const { dev, server, compileMiddlewareAPI } = options;
@@ -5140,7 +5278,16 @@ async function createDevServer(options, createCompiler2, config, {
5140
5278
  await options.context.hooks.onCloseDevServer.call();
5141
5279
  await Promise.all([devMiddlewares.close(), fileWatcher?.close()]);
5142
5280
  },
5143
- printUrls
5281
+ printUrls,
5282
+ open: async () => {
5283
+ return open({
5284
+ https,
5285
+ port,
5286
+ routes,
5287
+ config,
5288
+ clearCache: true
5289
+ });
5290
+ }
5144
5291
  };
5145
5292
  logger.debug("create dev server done");
5146
5293
  return devServerAPI;
@@ -5158,6 +5305,7 @@ var init_devServer = __esm({
5158
5305
  init_helper();
5159
5306
  init_httpServer();
5160
5307
  init_middlewares();
5308
+ init_open();
5161
5309
  init_restart();
5162
5310
  init_watchFiles();
5163
5311
  formatDevConfig = (config, port) => {
@@ -5838,158 +5986,6 @@ var init_css = __esm({
5838
5986
  }
5839
5987
  });
5840
5988
 
5841
- // src/plugins/open.ts
5842
- var open_exports = {};
5843
- __export(open_exports, {
5844
- openBrowser: () => openBrowser,
5845
- pluginOpen: () => pluginOpen,
5846
- replacePortPlaceholder: () => replacePortPlaceholder,
5847
- resolveUrl: () => resolveUrl
5848
- });
5849
- import { exec } from "child_process";
5850
- import { promisify } from "util";
5851
- async function openBrowser(url2) {
5852
- const shouldTryOpenChromeWithAppleScript = process.platform === "darwin";
5853
- if (shouldTryOpenChromeWithAppleScript) {
5854
- try {
5855
- const targetBrowser = await getTargetBrowser();
5856
- if (targetBrowser) {
5857
- await execAsync(
5858
- `osascript openChrome.applescript "${encodeURI(
5859
- url2
5860
- )}" "${targetBrowser}"`,
5861
- {
5862
- cwd: STATIC_PATH
5863
- }
5864
- );
5865
- return true;
5866
- }
5867
- logger.debug("Failed to find the target browser.");
5868
- } catch (err) {
5869
- logger.debug("Failed to open start URL with apple script.");
5870
- logger.debug(err);
5871
- }
5872
- }
5873
- try {
5874
- const { default: open } = await import("../compiled/open/index.js");
5875
- await open(url2);
5876
- return true;
5877
- } catch (err) {
5878
- logger.error("Failed to open start URL.");
5879
- logger.error(err);
5880
- return false;
5881
- }
5882
- }
5883
- function resolveUrl(str, base) {
5884
- if (canParse(str)) {
5885
- return str;
5886
- }
5887
- try {
5888
- const url2 = new URL(str, base);
5889
- return url2.href;
5890
- } catch (e) {
5891
- throw new Error(
5892
- "[rsbuild:open]: Invalid input: not a valid URL or pathname"
5893
- );
5894
- }
5895
- }
5896
- function pluginOpen() {
5897
- return {
5898
- name: "rsbuild:open",
5899
- setup(api) {
5900
- const onStartServer = async (params) => {
5901
- const { port, routes } = params;
5902
- const config = api.getNormalizedConfig();
5903
- const { https } = api.context.devServer || {};
5904
- const { targets, before } = normalizeOpenConfig(config);
5905
- const isCodesandbox = process.env.CSB === "true";
5906
- const shouldOpen = targets !== void 0 && !isCodesandbox;
5907
- if (!shouldOpen) {
5908
- return;
5909
- }
5910
- const urls = [];
5911
- const protocol = https ? "https" : "http";
5912
- const baseUrl = `${protocol}://localhost:${port}`;
5913
- if (!targets.length) {
5914
- if (routes.length) {
5915
- urls.push(`${baseUrl}${routes[0].pathname}`);
5916
- }
5917
- } else {
5918
- urls.push(
5919
- ...targets.map(
5920
- (target) => resolveUrl(replacePortPlaceholder(target, port), baseUrl)
5921
- )
5922
- );
5923
- }
5924
- const openUrls = () => {
5925
- for (const url2 of urls) {
5926
- if (!openedURLs.includes(url2)) {
5927
- openBrowser(url2);
5928
- openedURLs.push(url2);
5929
- }
5930
- }
5931
- };
5932
- if (before) {
5933
- await before();
5934
- }
5935
- openUrls();
5936
- };
5937
- api.onAfterStartDevServer(onStartServer);
5938
- api.onAfterStartProdServer(onStartServer);
5939
- }
5940
- };
5941
- }
5942
- var execAsync, supportedChromiumBrowsers, getTargetBrowser, replacePortPlaceholder, openedURLs, normalizeOpenConfig;
5943
- var init_open = __esm({
5944
- "src/plugins/open.ts"() {
5945
- "use strict";
5946
- init_esm();
5947
- init_constants();
5948
- init_helpers();
5949
- init_logger();
5950
- execAsync = promisify(exec);
5951
- supportedChromiumBrowsers = [
5952
- "Google Chrome Canary",
5953
- "Google Chrome Dev",
5954
- "Google Chrome Beta",
5955
- "Google Chrome",
5956
- "Microsoft Edge",
5957
- "Brave Browser",
5958
- "Vivaldi",
5959
- "Chromium"
5960
- ];
5961
- getTargetBrowser = async () => {
5962
- let targetBrowser = process.env.BROWSER;
5963
- if (!targetBrowser || !supportedChromiumBrowsers.includes(targetBrowser)) {
5964
- const { stdout: ps } = await execAsync("ps cax");
5965
- targetBrowser = supportedChromiumBrowsers.find((b) => ps.includes(b));
5966
- }
5967
- return targetBrowser;
5968
- };
5969
- replacePortPlaceholder = (url2, port) => url2.replace(/<port>/g, String(port));
5970
- openedURLs = [];
5971
- normalizeOpenConfig = (config) => {
5972
- const { open } = config.server;
5973
- if (open === false) {
5974
- return {};
5975
- }
5976
- if (open === true) {
5977
- return { targets: [] };
5978
- }
5979
- if (typeof open === "string") {
5980
- return { targets: [open] };
5981
- }
5982
- if (Array.isArray(open)) {
5983
- return { targets: open };
5984
- }
5985
- return {
5986
- targets: open.target ? castArray(open.target) : [],
5987
- before: open.before
5988
- };
5989
- };
5990
- }
5991
- });
5992
-
5993
5989
  // src/plugins/output.ts
5994
5990
  var output_exports = {};
5995
5991
  __export(output_exports, {
@@ -6031,8 +6027,8 @@ var init_output = __esm({
6031
6027
  init_constants();
6032
6028
  init_helpers();
6033
6029
  init_pluginHelper();
6034
- init_css();
6035
6030
  init_open();
6031
+ init_css();
6036
6032
  getJsAsyncPath = (jsPath, isServer, jsAsync) => {
6037
6033
  if (jsAsync !== void 0) {
6038
6034
  return jsAsync;
@@ -7589,9 +7585,6 @@ function getDefaultSwcConfig(browserslist3, cacheRoot) {
7589
7585
  syntax: "typescript",
7590
7586
  decorators: true
7591
7587
  },
7592
- // Avoid the webpack magic comment to be removed
7593
- // https://github.com/swc-project/swc/issues/6403
7594
- preserveAllComments: true,
7595
7588
  experimental: {
7596
7589
  cacheRoot
7597
7590
  }
@@ -8699,9 +8692,23 @@ var init_server = __esm({
8699
8692
  "use strict";
8700
8693
  init_esm();
8701
8694
  init_config();
8695
+ init_open();
8702
8696
  pluginServer = () => ({
8703
8697
  name: "rsbuild:server",
8704
8698
  setup(api) {
8699
+ const onStartServer = async ({ port, routes }) => {
8700
+ const config = api.getNormalizedConfig();
8701
+ if (config.server.open) {
8702
+ open({
8703
+ https: api.context.devServer?.https,
8704
+ port,
8705
+ routes,
8706
+ config
8707
+ });
8708
+ }
8709
+ };
8710
+ api.onAfterStartDevServer(onStartServer);
8711
+ api.onAfterStartProdServer(onStartServer);
8705
8712
  api.onBeforeBuild(async ({ isFirstCompile }) => {
8706
8713
  if (!isFirstCompile) {
8707
8714
  return;
@@ -9528,7 +9535,6 @@ async function applyDefaultPlugins(pluginManager, context) {
9528
9535
  Promise.resolve().then(() => (init_splitChunks(), splitChunks_exports)).then(
9529
9536
  ({ pluginSplitChunks: pluginSplitChunks2 }) => pluginSplitChunks2()
9530
9537
  ),
9531
- Promise.resolve().then(() => (init_open(), open_exports)).then(({ pluginOpen: pluginOpen2 }) => pluginOpen2()),
9532
9538
  Promise.resolve().then(() => (init_inlineChunk(), inlineChunk_exports)).then(
9533
9539
  ({ pluginInlineChunk: pluginInlineChunk2 }) => pluginInlineChunk2()
9534
9540
  ),
@@ -9788,7 +9794,7 @@ import { existsSync } from "fs";
9788
9794
  import { program } from "../compiled/commander/index.js";
9789
9795
  import color16 from "../compiled/picocolors/index.js";
9790
9796
  function runCli() {
9791
- program.name("rsbuild").usage("<command> [options]").version("1.0.4");
9797
+ program.name("rsbuild").usage("<command> [options]").version("1.0.5");
9792
9798
  const devCommand = program.command("dev");
9793
9799
  const buildCommand = program.command("build");
9794
9800
  const previewCommand = program.command("preview");
@@ -9909,7 +9915,7 @@ function prepareCli() {
9909
9915
  if (!npm_execpath || npm_execpath.includes("npx-cli.js") || npm_execpath.includes(".bun")) {
9910
9916
  console.log();
9911
9917
  }
9912
- logger.greet(` ${`Rsbuild v${"1.0.4"}`}
9918
+ logger.greet(` ${`Rsbuild v${"1.0.5"}`}
9913
9919
  `);
9914
9920
  }
9915
9921
  var init_prepare = __esm({
@@ -9980,7 +9986,7 @@ init_mergeConfig();
9980
9986
  init_helpers();
9981
9987
  init_constants();
9982
9988
  import { rspack as rspack10 } from "@rspack/core";
9983
- var version = "1.0.4";
9989
+ var version = "1.0.5";
9984
9990
  export {
9985
9991
  PLUGIN_CSS_NAME,
9986
9992
  PLUGIN_SWC_NAME,
@@ -49,6 +49,10 @@ export type RsbuildDevServer = {
49
49
  * Print the server URLs.
50
50
  */
51
51
  printUrls: () => void;
52
+ /**
53
+ * Open URL in the browser after starting the server.
54
+ */
55
+ open: () => Promise<void>;
52
56
  };
53
57
  export declare function createDevServer<Options extends {
54
58
  context: InternalContext;
@@ -0,0 +1,10 @@
1
+ import type { NormalizedConfig, Routes } from '../types';
2
+ export declare const replacePortPlaceholder: (url: string, port: number) => string;
3
+ export declare function resolveUrl(str: string, base: string): string;
4
+ export declare function open({ https, port, routes, config, clearCache, }: {
5
+ https?: boolean;
6
+ port: number;
7
+ routes: Routes;
8
+ config: NormalizedConfig;
9
+ clearCache?: boolean;
10
+ }): Promise<void>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rsbuild/core",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "description": "The Rspack-based build tool.",
5
5
  "homepage": "https://rsbuild.dev",
6
6
  "bugs": {
@@ -49,7 +49,7 @@
49
49
  "@rspack/core": "~1.0.5",
50
50
  "@rspack/lite-tapable": "~1.0.0",
51
51
  "@swc/helpers": "^0.5.13",
52
- "caniuse-lite": "^1.0.30001659",
52
+ "caniuse-lite": "^1.0.30001660",
53
53
  "core-js": "~3.38.1"
54
54
  },
55
55
  "devDependencies": {
@@ -78,7 +78,7 @@
78
78
  "on-finished": "2.4.1",
79
79
  "open": "^8.4.0",
80
80
  "picocolors": "^1.1.0",
81
- "postcss": "^8.4.45",
81
+ "postcss": "^8.4.47",
82
82
  "postcss-load-config": "6.0.1",
83
83
  "postcss-loader": "8.1.1",
84
84
  "prebundle": "1.2.2",
@@ -1,13 +0,0 @@
1
- import type { RsbuildPlugin } from '../types';
2
- /**
3
- * This method is modified based on source found in
4
- * https://github.com/facebook/create-react-app
5
- *
6
- * MIT Licensed
7
- * Copyright (c) 2015-present, Facebook, Inc.
8
- * https://github.com/facebook/create-react-app/blob/master/LICENSE
9
- */
10
- export declare function openBrowser(url: string): Promise<boolean>;
11
- export declare const replacePortPlaceholder: (url: string, port: number) => string;
12
- export declare function resolveUrl(str: string, base: string): string;
13
- export declare function pluginOpen(): RsbuildPlugin;