@rsbuild/core 0.3.4 → 0.3.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.
@@ -1,6 +1,7 @@
1
1
  import type { RsbuildMode } from '..';
2
2
  export type CommonOptions = {
3
3
  config?: string;
4
+ envMode?: string;
4
5
  open?: boolean | string;
5
6
  host?: string;
6
7
  port?: number;
@@ -49,12 +49,25 @@ async function init({
49
49
  }
50
50
  try {
51
51
  const root = process.cwd();
52
- const envs = (0, import_loadEnv.loadEnv)({ cwd: root });
53
- (0, import_shared.isDev)() && (0, import_restart.onBeforeRestartServer)(envs.cleanup);
54
- const config = await (0, import_config.loadConfig)({
52
+ const envs = (0, import_loadEnv.loadEnv)({
53
+ cwd: root,
54
+ mode: cliOptions?.envMode
55
+ });
56
+ if ((0, import_shared.isDev)()) {
57
+ (0, import_restart.onBeforeRestartServer)(envs.cleanup);
58
+ }
59
+ const { content: config, filePath: configFilePath } = await (0, import_config.loadConfigV2)({
55
60
  cwd: root,
56
61
  path: commonOpts.config
57
62
  });
63
+ const command = process.argv[2];
64
+ if (command === "dev") {
65
+ const files = [...envs.filePaths];
66
+ if (configFilePath) {
67
+ files.push(configFilePath);
68
+ }
69
+ (0, import_config.watchFiles)(files);
70
+ }
58
71
  const { createRsbuild } = await Promise.resolve().then(() => __toESM(require("../createRsbuild")));
59
72
  config.source || (config.source = {});
60
73
  config.source.define = {
@@ -85,18 +98,29 @@ async function init({
85
98
  }
86
99
  }
87
100
  }
88
- function runCli() {
89
- import_commander.program.name("rsbuild").usage("<command> [options]").version("0.3.4");
90
- import_commander.program.command("dev").option("-o --open [url]", "open the page in browser on startup").option(
91
- "--port <port>",
92
- "specify a port number for Rsbuild Server to listen"
93
- ).option(
94
- "--host <host>",
95
- "specify the host that the Rsbuild Server listens to"
96
- ).option(
101
+ const applyCommonOptions = (command) => {
102
+ command.option(
97
103
  "-c --config <config>",
98
104
  "specify the configuration file, can be a relative or absolute path"
99
- ).description("starting the dev server").action(async (options) => {
105
+ ).option(
106
+ "--env-mode <mode>",
107
+ "specify the env mode to load the `.env.[mode]` file"
108
+ );
109
+ };
110
+ const applyServerOptions = (command) => {
111
+ command.option("-o --open [url]", "open the page in browser on startup").option("--port <port>", "specify a port number for server to listen").option("--host <host>", "specify the host that the server listens to");
112
+ };
113
+ function runCli() {
114
+ import_commander.program.name("rsbuild").usage("<command> [options]").version("0.3.5");
115
+ const devCommand = import_commander.program.command("dev");
116
+ const buildCommand = import_commander.program.command("build");
117
+ const previewCommand = import_commander.program.command("preview");
118
+ const inspectCommand = import_commander.program.command("inspect");
119
+ [devCommand, buildCommand, previewCommand, inspectCommand].forEach(
120
+ applyCommonOptions
121
+ );
122
+ [devCommand, previewCommand].forEach(applyServerOptions);
123
+ devCommand.description("starting the dev server").action(async (options) => {
100
124
  try {
101
125
  const rsbuild = await init({ cliOptions: options });
102
126
  await rsbuild?.startDevServer();
@@ -106,10 +130,7 @@ function runCli() {
106
130
  process.exit(1);
107
131
  }
108
132
  });
109
- import_commander.program.command("build").option("-w --watch", "turn on watch mode, watch for changes and rebuild").option(
110
- "-c --config <config>",
111
- "specify the configuration file, can be a relative or absolute path"
112
- ).description("build the app for production").action(async (options) => {
133
+ buildCommand.option("-w --watch", "turn on watch mode, watch for changes and rebuild").description("build the app for production").action(async (options) => {
113
134
  try {
114
135
  const rsbuild = await init({ cliOptions: options });
115
136
  await rsbuild?.build({
@@ -121,16 +142,7 @@ function runCli() {
121
142
  process.exit(1);
122
143
  }
123
144
  });
124
- import_commander.program.command("preview").option("-o --open [url]", "open the page in browser on startup").option(
125
- "--port <port>",
126
- "specify a port number for Rsbuild Server to listen"
127
- ).option(
128
- "--host <host>",
129
- "specify the host that the Rsbuild Server listens to"
130
- ).option(
131
- "-c --config <config>",
132
- "specify the configuration file, can be a relative or absolute path"
133
- ).description("preview the production build locally").action(async (options) => {
145
+ previewCommand.description("preview the production build locally").action(async (options) => {
134
146
  try {
135
147
  const rsbuild = await init({ cliOptions: options });
136
148
  if (rsbuild && !(0, import_fs.existsSync)(rsbuild.context.distPath)) {
@@ -147,10 +159,7 @@ function runCli() {
147
159
  process.exit(1);
148
160
  }
149
161
  });
150
- import_commander.program.command("inspect").description("inspect the Rspack and Rsbuild configs").option("--env <env>", "specify env mode", "development").option("--output <output>", "specify inspect content output path", "/").option("--verbose", "show full function definitions in output").option(
151
- "-c --config <config>",
152
- "specify the configuration file, can be a relative or absolute path"
153
- ).action(async (options) => {
162
+ inspectCommand.description("inspect the Rspack and Rsbuild configs").option("--env <env>", "specify env mode", "development").option("--output <output>", "specify inspect content output path", "/").option("--verbose", "show full function definitions in output").action(async (options) => {
154
163
  try {
155
164
  const rsbuild = await init({ cliOptions: options });
156
165
  await rsbuild?.inspectConfig({
@@ -14,7 +14,16 @@ export declare function defineConfig(config: RsbuildConfig): RsbuildConfig;
14
14
  export declare function defineConfig(config: RsbuildConfigSyncFn): RsbuildConfigSyncFn;
15
15
  export declare function defineConfig(config: RsbuildConfigAsyncFn): RsbuildConfigAsyncFn;
16
16
  export declare function defineConfig(config: RsbuildConfigExport): RsbuildConfigExport;
17
+ export declare function watchFiles(files: string[]): Promise<void>;
18
+ export declare function loadConfigByPath(configFile: string): Promise<RsbuildConfig>;
17
19
  export declare function loadConfig({ cwd, path, }: {
18
20
  cwd: string;
19
21
  path?: string;
20
22
  }): Promise<RsbuildConfig>;
23
+ export declare function loadConfigV2({ cwd, path, }: {
24
+ cwd: string;
25
+ path?: string;
26
+ }): Promise<{
27
+ content: RsbuildConfig;
28
+ filePath: string | null;
29
+ }>;
@@ -29,13 +29,15 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
29
29
  var config_exports = {};
30
30
  __export(config_exports, {
31
31
  defineConfig: () => defineConfig,
32
- loadConfig: () => loadConfig
32
+ loadConfig: () => loadConfig,
33
+ loadConfigByPath: () => loadConfigByPath,
34
+ loadConfigV2: () => loadConfigV2,
35
+ watchFiles: () => watchFiles
33
36
  });
34
37
  module.exports = __toCommonJS(config_exports);
35
38
  var import_fs = __toESM(require("fs"));
36
39
  var import_path = require("path");
37
40
  var import_shared = require("@rsbuild/shared");
38
- var import_loadEnv = require("../loadEnv");
39
41
  var import_restart = require("../server/restart");
40
42
  function defineConfig(config) {
41
43
  return config;
@@ -65,10 +67,12 @@ const resolveConfigPath = (root, customConfig) => {
65
67
  }
66
68
  return null;
67
69
  };
68
- async function watchConfig(root, configFile) {
70
+ async function watchFiles(files) {
71
+ if (!files.length) {
72
+ return;
73
+ }
69
74
  const chokidar = await Promise.resolve().then(() => __toESM(require("@rsbuild/shared/chokidar")));
70
- const envFiles = (0, import_loadEnv.getEnvFiles)().map((filename) => (0, import_path.join)(root, filename));
71
- const watcher = chokidar.watch([configFile, ...envFiles], {
75
+ const watcher = chokidar.watch(files, {
72
76
  // do not trigger add for initial files
73
77
  ignoreInitial: true,
74
78
  // If watching fails due to read permissions, the errors will be suppressed silently.
@@ -86,14 +90,7 @@ async function watchConfig(root, configFile) {
86
90
  watcher.on("change", callback);
87
91
  watcher.on("unlink", callback);
88
92
  }
89
- async function loadConfig({
90
- cwd,
91
- path
92
- }) {
93
- const configFile = resolveConfigPath(cwd, path);
94
- if (!configFile) {
95
- return {};
96
- }
93
+ async function loadConfigByPath(configFile) {
97
94
  try {
98
95
  const { default: jiti } = await Promise.resolve().then(() => __toESM(require("@rsbuild/shared/jiti")));
99
96
  const loadConfig2 = jiti(__filename, {
@@ -102,12 +99,9 @@ async function loadConfig({
102
99
  requireCache: false,
103
100
  interopDefault: true
104
101
  });
105
- const command = process.argv[2];
106
- if (command === "dev") {
107
- watchConfig(cwd, configFile);
108
- }
109
102
  const configExport = loadConfig2(configFile);
110
103
  if (typeof configExport === "function") {
104
+ const command = process.argv[2];
111
105
  const params = {
112
106
  env: (0, import_shared.getNodeEnv)(),
113
107
  command
@@ -124,8 +118,37 @@ async function loadConfig({
124
118
  throw err;
125
119
  }
126
120
  }
121
+ async function loadConfig({
122
+ cwd,
123
+ path
124
+ }) {
125
+ const configFile = resolveConfigPath(cwd, path);
126
+ if (!configFile) {
127
+ return {};
128
+ }
129
+ return loadConfigByPath(configFile);
130
+ }
131
+ async function loadConfigV2({
132
+ cwd,
133
+ path
134
+ }) {
135
+ const configFile = resolveConfigPath(cwd, path);
136
+ if (!configFile) {
137
+ return {
138
+ content: {},
139
+ filePath: configFile
140
+ };
141
+ }
142
+ return {
143
+ content: await loadConfigByPath(configFile),
144
+ filePath: configFile
145
+ };
146
+ }
127
147
  // Annotate the CommonJS export names for ESM import in node:
128
148
  0 && (module.exports = {
129
149
  defineConfig,
130
- loadConfig
150
+ loadConfig,
151
+ loadConfigByPath,
152
+ loadConfigV2,
153
+ watchFiles
131
154
  });
@@ -34,7 +34,7 @@ function prepareCli() {
34
34
  if (!npm_execpath || npm_execpath.includes("npx-cli.js")) {
35
35
  console.log();
36
36
  }
37
- import_rslog.logger.greet(` ${`Rsbuild v${"0.3.4"}`}
37
+ import_rslog.logger.greet(` ${`Rsbuild v${"0.3.5"}`}
38
38
  `);
39
39
  }
40
40
  // Annotate the CommonJS export names for ESM import in node:
package/dist/index.js CHANGED
@@ -38,7 +38,7 @@ var import_createRsbuild = require("./createRsbuild");
38
38
  var import_config = require("./cli/config");
39
39
  var import_shared = require("@rsbuild/shared");
40
40
  var import_constants = require("./constants");
41
- const version = "0.3.4";
41
+ const version = "0.3.5";
42
42
  // Annotate the CommonJS export names for ESM import in node:
43
43
  0 && (module.exports = {
44
44
  PLUGIN_BABEL_NAME,
package/dist/loadEnv.d.ts CHANGED
@@ -1,9 +1,27 @@
1
- export declare const getEnvFiles: () => string[];
2
- export declare function loadEnv({ cwd, prefixes, }?: {
1
+ export type LoadEnvOptions = {
2
+ /**
3
+ * The root path to load the env file
4
+ * @default process.cwd()
5
+ */
3
6
  cwd?: string;
7
+ /**
8
+ * Used to specify the name of the .env.[mode] file
9
+ * @default process.env.NODE_ENV
10
+ */
11
+ mode?: string;
12
+ /**
13
+ * The prefix of public variables
14
+ * @default ['PUBLIC_']
15
+ */
4
16
  prefixes?: string[];
5
- }): {
17
+ };
18
+ export declare function loadEnv({ cwd, mode, prefixes, }?: LoadEnvOptions): {
19
+ /** All environment variables in the .env file */
6
20
  parsed: Record<string, string>;
21
+ /** The absolute paths to all env files */
22
+ filePaths: string[];
23
+ /** Environment variables that start with prefixes */
7
24
  publicVars: Record<string, string>;
25
+ /** Clear the environment variables mounted on `process.env` */
8
26
  cleanup: () => void;
9
27
  };
package/dist/loadEnv.js CHANGED
@@ -28,7 +28,6 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
28
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
29
  var loadEnv_exports = {};
30
30
  __export(loadEnv_exports, {
31
- getEnvFiles: () => getEnvFiles,
32
31
  loadEnv: () => loadEnv
33
32
  });
34
33
  module.exports = __toCommonJS(loadEnv_exports);
@@ -37,17 +36,20 @@ var import_path = require("path");
37
36
  var import_shared = require("@rsbuild/shared");
38
37
  var import_dotenv = require("../compiled/dotenv");
39
38
  var import_dotenv_expand = require("../compiled/dotenv-expand");
40
- const getEnvFiles = () => {
41
- const nodeEnv = (0, import_shared.getNodeEnv)();
42
- return [".env", ".env.local", `.env.${nodeEnv}`, `.env.${nodeEnv}.local`];
43
- };
44
39
  function loadEnv({
45
40
  cwd = process.cwd(),
41
+ mode = (0, import_shared.getNodeEnv)(),
46
42
  prefixes = ["PUBLIC_"]
47
43
  } = {}) {
48
- const envPaths = getEnvFiles().map((filename) => (0, import_path.join)(cwd, filename)).filter(import_shared.isFileSync);
44
+ const filenames = [
45
+ ".env",
46
+ ".env.local",
47
+ `.env.${mode}`,
48
+ `.env.${mode}.local`
49
+ ];
50
+ const filePaths = filenames.map((filename) => (0, import_path.join)(cwd, filename)).filter(import_shared.isFileSync);
49
51
  const parsed = {};
50
- envPaths.forEach((envPath) => {
52
+ filePaths.forEach((envPath) => {
51
53
  Object.assign(parsed, (0, import_dotenv.parse)(import_fs.default.readFileSync(envPath)));
52
54
  });
53
55
  (0, import_dotenv_expand.expand)({ parsed });
@@ -71,12 +73,12 @@ function loadEnv({
71
73
  };
72
74
  return {
73
75
  parsed,
74
- publicVars,
75
- cleanup
76
+ cleanup,
77
+ filePaths,
78
+ publicVars
76
79
  };
77
80
  }
78
81
  // Annotate the CommonJS export names for ESM import in node:
79
82
  0 && (module.exports = {
80
- getEnvFiles,
81
83
  loadEnv
82
84
  });
@@ -37,10 +37,18 @@ const pluginServer = () => ({
37
37
  if (!import_shared.fse.existsSync(publicDir)) {
38
38
  return;
39
39
  }
40
- await import_shared.fse.copy(publicDir, api.context.distPath, {
41
- // dereference symlinks
42
- dereference: true
43
- });
40
+ try {
41
+ await import_shared.fse.copy(publicDir, api.context.distPath, {
42
+ // dereference symlinks
43
+ dereference: true
44
+ });
45
+ } catch (err) {
46
+ if (err instanceof Error) {
47
+ err.message = `Copy public dir (${publicDir}) to dist failed:
48
+ ${err.message}`;
49
+ }
50
+ throw err;
51
+ }
44
52
  }
45
53
  });
46
54
  }
@@ -43,7 +43,7 @@ async function createContextByConfig(options, bundlerType, config = {}) {
43
43
  const context = {
44
44
  entry: config.source?.entry || {},
45
45
  targets: config.output?.targets || [],
46
- version: "0.3.4",
46
+ version: "0.3.5",
47
47
  rootPath,
48
48
  distPath,
49
49
  cachePath,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rsbuild/core",
3
- "version": "0.3.4",
3
+ "version": "0.3.5",
4
4
  "description": "The Rspack-based build tool.",
5
5
  "homepage": "https://rsbuild.dev",
6
6
  "bugs": {
@@ -57,7 +57,7 @@
57
57
  "core-js": "~3.32.2",
58
58
  "html-webpack-plugin": "npm:html-rspack-plugin@5.5.7",
59
59
  "postcss": "^8.4.33",
60
- "@rsbuild/shared": "0.3.4"
60
+ "@rsbuild/shared": "0.3.5"
61
61
  },
62
62
  "devDependencies": {
63
63
  "@types/node": "16.x",