@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.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.3",
2345
+ version: "1.0.5",
2346
2346
  rootPath,
2347
2347
  distPath: "",
2348
2348
  cachePath,
@@ -4238,7 +4238,11 @@ var init_helper = __esm({
4238
4238
  init_helpers();
4239
4239
  init_logger();
4240
4240
  normalizeUrl = (url2) => url2.replace(/([^:]\/)\/+/g, "$1");
4241
- formatPrefix = (prefix) => {
4241
+ formatPrefix = (input) => {
4242
+ let prefix = input;
4243
+ if (prefix?.startsWith("./")) {
4244
+ prefix = prefix.replace("./", "");
4245
+ }
4242
4246
  if (!prefix) {
4243
4247
  return "/";
4244
4248
  }
@@ -4459,6 +4463,144 @@ var init_httpServer = __esm({
4459
4463
  }
4460
4464
  });
4461
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
+
4462
4604
  // src/server/watchFiles.ts
4463
4605
  async function setupWatchFiles(options) {
4464
4606
  const { dev, server, compileMiddlewareAPI } = options;
@@ -5136,7 +5278,16 @@ async function createDevServer(options, createCompiler2, config, {
5136
5278
  await options.context.hooks.onCloseDevServer.call();
5137
5279
  await Promise.all([devMiddlewares.close(), fileWatcher?.close()]);
5138
5280
  },
5139
- printUrls
5281
+ printUrls,
5282
+ open: async () => {
5283
+ return open({
5284
+ https,
5285
+ port,
5286
+ routes,
5287
+ config,
5288
+ clearCache: true
5289
+ });
5290
+ }
5140
5291
  };
5141
5292
  logger.debug("create dev server done");
5142
5293
  return devServerAPI;
@@ -5154,6 +5305,7 @@ var init_devServer = __esm({
5154
5305
  init_helper();
5155
5306
  init_httpServer();
5156
5307
  init_middlewares();
5308
+ init_open();
5157
5309
  init_restart();
5158
5310
  init_watchFiles();
5159
5311
  formatDevConfig = (config, port) => {
@@ -5834,158 +5986,6 @@ var init_css = __esm({
5834
5986
  }
5835
5987
  });
5836
5988
 
5837
- // src/plugins/open.ts
5838
- var open_exports = {};
5839
- __export(open_exports, {
5840
- openBrowser: () => openBrowser,
5841
- pluginOpen: () => pluginOpen,
5842
- replacePortPlaceholder: () => replacePortPlaceholder,
5843
- resolveUrl: () => resolveUrl
5844
- });
5845
- import { exec } from "child_process";
5846
- import { promisify } from "util";
5847
- async function openBrowser(url2) {
5848
- const shouldTryOpenChromeWithAppleScript = process.platform === "darwin";
5849
- if (shouldTryOpenChromeWithAppleScript) {
5850
- try {
5851
- const targetBrowser = await getTargetBrowser();
5852
- if (targetBrowser) {
5853
- await execAsync(
5854
- `osascript openChrome.applescript "${encodeURI(
5855
- url2
5856
- )}" "${targetBrowser}"`,
5857
- {
5858
- cwd: STATIC_PATH
5859
- }
5860
- );
5861
- return true;
5862
- }
5863
- logger.debug("Failed to find the target browser.");
5864
- } catch (err) {
5865
- logger.debug("Failed to open start URL with apple script.");
5866
- logger.debug(err);
5867
- }
5868
- }
5869
- try {
5870
- const { default: open } = await import("../compiled/open/index.js");
5871
- await open(url2);
5872
- return true;
5873
- } catch (err) {
5874
- logger.error("Failed to open start URL.");
5875
- logger.error(err);
5876
- return false;
5877
- }
5878
- }
5879
- function resolveUrl(str, base) {
5880
- if (canParse(str)) {
5881
- return str;
5882
- }
5883
- try {
5884
- const url2 = new URL(str, base);
5885
- return url2.href;
5886
- } catch (e) {
5887
- throw new Error(
5888
- "[rsbuild:open]: Invalid input: not a valid URL or pathname"
5889
- );
5890
- }
5891
- }
5892
- function pluginOpen() {
5893
- return {
5894
- name: "rsbuild:open",
5895
- setup(api) {
5896
- const onStartServer = async (params) => {
5897
- const { port, routes } = params;
5898
- const config = api.getNormalizedConfig();
5899
- const { https } = api.context.devServer || {};
5900
- const { targets, before } = normalizeOpenConfig(config);
5901
- const isCodesandbox = process.env.CSB === "true";
5902
- const shouldOpen = targets !== void 0 && !isCodesandbox;
5903
- if (!shouldOpen) {
5904
- return;
5905
- }
5906
- const urls = [];
5907
- const protocol = https ? "https" : "http";
5908
- const baseUrl = `${protocol}://localhost:${port}`;
5909
- if (!targets.length) {
5910
- if (routes.length) {
5911
- urls.push(`${baseUrl}${routes[0].pathname}`);
5912
- }
5913
- } else {
5914
- urls.push(
5915
- ...targets.map(
5916
- (target) => resolveUrl(replacePortPlaceholder(target, port), baseUrl)
5917
- )
5918
- );
5919
- }
5920
- const openUrls = () => {
5921
- for (const url2 of urls) {
5922
- if (!openedURLs.includes(url2)) {
5923
- openBrowser(url2);
5924
- openedURLs.push(url2);
5925
- }
5926
- }
5927
- };
5928
- if (before) {
5929
- await before();
5930
- }
5931
- openUrls();
5932
- };
5933
- api.onAfterStartDevServer(onStartServer);
5934
- api.onAfterStartProdServer(onStartServer);
5935
- }
5936
- };
5937
- }
5938
- var execAsync, supportedChromiumBrowsers, getTargetBrowser, replacePortPlaceholder, openedURLs, normalizeOpenConfig;
5939
- var init_open = __esm({
5940
- "src/plugins/open.ts"() {
5941
- "use strict";
5942
- init_esm();
5943
- init_constants();
5944
- init_helpers();
5945
- init_logger();
5946
- execAsync = promisify(exec);
5947
- supportedChromiumBrowsers = [
5948
- "Google Chrome Canary",
5949
- "Google Chrome Dev",
5950
- "Google Chrome Beta",
5951
- "Google Chrome",
5952
- "Microsoft Edge",
5953
- "Brave Browser",
5954
- "Vivaldi",
5955
- "Chromium"
5956
- ];
5957
- getTargetBrowser = async () => {
5958
- let targetBrowser = process.env.BROWSER;
5959
- if (!targetBrowser || !supportedChromiumBrowsers.includes(targetBrowser)) {
5960
- const { stdout: ps } = await execAsync("ps cax");
5961
- targetBrowser = supportedChromiumBrowsers.find((b) => ps.includes(b));
5962
- }
5963
- return targetBrowser;
5964
- };
5965
- replacePortPlaceholder = (url2, port) => url2.replace(/<port>/g, String(port));
5966
- openedURLs = [];
5967
- normalizeOpenConfig = (config) => {
5968
- const { open } = config.server;
5969
- if (open === false) {
5970
- return {};
5971
- }
5972
- if (open === true) {
5973
- return { targets: [] };
5974
- }
5975
- if (typeof open === "string") {
5976
- return { targets: [open] };
5977
- }
5978
- if (Array.isArray(open)) {
5979
- return { targets: open };
5980
- }
5981
- return {
5982
- targets: open.target ? castArray(open.target) : [],
5983
- before: open.before
5984
- };
5985
- };
5986
- }
5987
- });
5988
-
5989
5989
  // src/plugins/output.ts
5990
5990
  var output_exports = {};
5991
5991
  __export(output_exports, {
@@ -6027,8 +6027,8 @@ var init_output = __esm({
6027
6027
  init_constants();
6028
6028
  init_helpers();
6029
6029
  init_pluginHelper();
6030
- init_css();
6031
6030
  init_open();
6031
+ init_css();
6032
6032
  getJsAsyncPath = (jsPath, isServer, jsAsync) => {
6033
6033
  if (jsAsync !== void 0) {
6034
6034
  return jsAsync;
@@ -7585,9 +7585,6 @@ function getDefaultSwcConfig(browserslist3, cacheRoot) {
7585
7585
  syntax: "typescript",
7586
7586
  decorators: true
7587
7587
  },
7588
- // Avoid the webpack magic comment to be removed
7589
- // https://github.com/swc-project/swc/issues/6403
7590
- preserveAllComments: true,
7591
7588
  experimental: {
7592
7589
  cacheRoot
7593
7590
  }
@@ -8695,9 +8692,23 @@ var init_server = __esm({
8695
8692
  "use strict";
8696
8693
  init_esm();
8697
8694
  init_config();
8695
+ init_open();
8698
8696
  pluginServer = () => ({
8699
8697
  name: "rsbuild:server",
8700
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);
8701
8712
  api.onBeforeBuild(async ({ isFirstCompile }) => {
8702
8713
  if (!isFirstCompile) {
8703
8714
  return;
@@ -9524,7 +9535,6 @@ async function applyDefaultPlugins(pluginManager, context) {
9524
9535
  Promise.resolve().then(() => (init_splitChunks(), splitChunks_exports)).then(
9525
9536
  ({ pluginSplitChunks: pluginSplitChunks2 }) => pluginSplitChunks2()
9526
9537
  ),
9527
- Promise.resolve().then(() => (init_open(), open_exports)).then(({ pluginOpen: pluginOpen2 }) => pluginOpen2()),
9528
9538
  Promise.resolve().then(() => (init_inlineChunk(), inlineChunk_exports)).then(
9529
9539
  ({ pluginInlineChunk: pluginInlineChunk2 }) => pluginInlineChunk2()
9530
9540
  ),
@@ -9784,7 +9794,7 @@ import { existsSync } from "fs";
9784
9794
  import { program } from "../compiled/commander/index.js";
9785
9795
  import color16 from "../compiled/picocolors/index.js";
9786
9796
  function runCli() {
9787
- program.name("rsbuild").usage("<command> [options]").version("1.0.3");
9797
+ program.name("rsbuild").usage("<command> [options]").version("1.0.5");
9788
9798
  const devCommand = program.command("dev");
9789
9799
  const buildCommand = program.command("build");
9790
9800
  const previewCommand = program.command("preview");
@@ -9905,7 +9915,7 @@ function prepareCli() {
9905
9915
  if (!npm_execpath || npm_execpath.includes("npx-cli.js") || npm_execpath.includes(".bun")) {
9906
9916
  console.log();
9907
9917
  }
9908
- logger.greet(` ${`Rsbuild v${"1.0.3"}`}
9918
+ logger.greet(` ${`Rsbuild v${"1.0.5"}`}
9909
9919
  `);
9910
9920
  }
9911
9921
  var init_prepare = __esm({
@@ -9976,7 +9986,7 @@ init_mergeConfig();
9976
9986
  init_helpers();
9977
9987
  init_constants();
9978
9988
  import { rspack as rspack10 } from "@rspack/core";
9979
- var version = "1.0.3";
9989
+ var version = "1.0.5";
9980
9990
  export {
9981
9991
  PLUGIN_CSS_NAME,
9982
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.3",
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;