@rsbuild/webpack 1.0.1-beta.1 → 1.0.1-beta.11

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
@@ -39,7 +39,7 @@ var __publicField = (obj, key, value) => {
39
39
  };
40
40
 
41
41
  // src/shared.ts
42
- var import_core, getChainUtils, initRsbuildConfig, createDevServer, formatStats, getDevMiddleware, getStatsOptions, stringifyConfig, outputInspectConfigFiles, getRsbuildInspectConfig, chainToConfig, modifyBundlerChain, onCompileDone, prettyTime, castArray;
42
+ var import_core, getChainUtils, initRsbuildConfig, createDevServer, formatStats, getDevMiddleware, getStatsOptions, stringifyConfig, outputInspectConfigFiles, getRsbuildInspectConfig, chainToConfig, modifyBundlerChain, registerDevHook, registerBuildHook, prettyTime, castArray;
43
43
  var init_shared = __esm({
44
44
  "src/shared.ts"() {
45
45
  "use strict";
@@ -56,7 +56,8 @@ var init_shared = __esm({
56
56
  getRsbuildInspectConfig,
57
57
  chainToConfig,
58
58
  modifyBundlerChain,
59
- onCompileDone,
59
+ registerDevHook,
60
+ registerBuildHook,
60
61
  prettyTime
61
62
  } = import_core.__internalHelper);
62
63
  castArray = (arr) => {
@@ -80,8 +81,8 @@ async function inspectConfig({
80
81
  bundlerConfigs,
81
82
  inspectOptions = {}
82
83
  }) {
83
- if (inspectOptions.env) {
84
- process.env.NODE_ENV = inspectOptions.env;
84
+ if (inspectOptions.mode) {
85
+ process.env.NODE_ENV = inspectOptions.mode;
85
86
  } else if (!process.env.NODE_ENV) {
86
87
  process.env.NODE_ENV = "development";
87
88
  }
@@ -104,10 +105,7 @@ async function inspectConfig({
104
105
  inspectOptions,
105
106
  pluginManager
106
107
  });
107
- let outputPath = inspectOptions.outputPath || context.distPath;
108
- if (!(0, import_node_path.isAbsolute)(outputPath)) {
109
- outputPath = (0, import_node_path.join)(context.rootPath, outputPath);
110
- }
108
+ const outputPath = getInspectOutputPath(context, inspectOptions);
111
109
  if (inspectOptions.writeToDisk) {
112
110
  await outputInspectConfigFiles({
113
111
  rawBundlerConfigs,
@@ -130,23 +128,32 @@ async function inspectConfig({
130
128
  }
131
129
  };
132
130
  }
133
- var import_node_path;
131
+ var import_node_path, getInspectOutputPath;
134
132
  var init_inspectConfig = __esm({
135
133
  "src/inspectConfig.ts"() {
136
134
  "use strict";
137
135
  import_node_path = require("path");
138
136
  init_initConfigs();
139
137
  init_shared();
138
+ getInspectOutputPath = (context, inspectOptions) => {
139
+ if (inspectOptions.outputPath) {
140
+ if ((0, import_node_path.isAbsolute)(inspectOptions.outputPath)) {
141
+ return inspectOptions.outputPath;
142
+ }
143
+ return (0, import_node_path.join)(context.distPath, inspectOptions.outputPath);
144
+ }
145
+ return context.distPath;
146
+ };
140
147
  }
141
148
  });
142
149
 
143
150
  // src/webpackConfig.ts
144
151
  async function modifyWebpackChain(context, utils, chain) {
145
152
  import_core2.logger.debug("modify webpack chain");
146
- const [modifiedChain] = await context.hooks.modifyWebpackChain.call(
147
- chain,
148
- utils
149
- );
153
+ const [modifiedChain] = await context.hooks.modifyWebpackChain.callInEnvironment({
154
+ environment: utils.environment.name,
155
+ args: [chain, utils]
156
+ });
150
157
  if (utils.environment.config.tools?.webpackChain) {
151
158
  for (const item of castArray(utils.environment.config.tools.webpackChain)) {
152
159
  item(modifiedChain, utils);
@@ -157,10 +164,10 @@ async function modifyWebpackChain(context, utils, chain) {
157
164
  }
158
165
  async function modifyWebpackConfig(context, webpackConfig, utils) {
159
166
  import_core2.logger.debug("modify webpack config");
160
- let [modifiedConfig] = await context.hooks.modifyWebpackConfig.call(
161
- webpackConfig,
162
- utils
163
- );
167
+ let [modifiedConfig] = await context.hooks.modifyWebpackConfig.callInEnvironment({
168
+ environment: utils.environment.name,
169
+ args: [webpackConfig, utils]
170
+ });
164
171
  if (utils.environment.config.tools?.webpack) {
165
172
  modifiedConfig = (0, import_reduce_configs.reduceConfigsWithContext)({
166
173
  initial: modifiedConfig,
@@ -269,7 +276,11 @@ async function initConfigs({
269
276
  bundlerConfigs: webpackConfigs
270
277
  });
271
278
  };
272
- context.hooks.onBeforeBuild.tap(inspect);
279
+ context.hooks.onBeforeBuild.tap(({ isFirstCompile }) => {
280
+ if (isFirstCompile) {
281
+ inspect();
282
+ }
283
+ });
273
284
  context.hooks.onAfterStartDevServer.tap(inspect);
274
285
  }
275
286
  return {
@@ -304,7 +315,7 @@ async function createCompiler({
304
315
  });
305
316
  const { default: webpack2 } = await import("webpack");
306
317
  const compiler = webpackConfigs.length === 1 ? webpack2(webpackConfigs[0]) : webpack2(webpackConfigs);
307
- const done = async (stats) => {
318
+ const done = (stats) => {
308
319
  const { message, level } = formatStats(
309
320
  stats,
310
321
  getStatsOptions(compiler)
@@ -315,17 +326,18 @@ async function createCompiler({
315
326
  if (level === "warning") {
316
327
  import_core4.logger.warn(message);
317
328
  }
318
- if (process.env.NODE_ENV === "development") {
319
- await context.hooks.onDevCompileDone.call({
320
- isFirstCompile,
321
- stats,
322
- environments: context.environments
323
- });
324
- }
325
- isFirstCompile = false;
326
329
  };
327
- let isFirstCompile = true;
328
- onCompileDone(compiler, done, import_MultiStats.default);
330
+ compiler.hooks.done.tap("rsbuild:done", (stats) => {
331
+ done(stats);
332
+ });
333
+ if (context.normalizedConfig?.mode === "development") {
334
+ registerDevHook({
335
+ compiler,
336
+ context,
337
+ bundlerConfigs: webpackConfigs,
338
+ MultiStatsCtor: import_MultiStats.default
339
+ });
340
+ }
329
341
  await context.hooks.onAfterCreateCompiler.call({
330
342
  compiler,
331
343
  environments: context.environments
@@ -357,7 +369,6 @@ var init_createCompiler = __esm({
357
369
  import_MultiStats = __toESM(require("webpack/lib/MultiStats.js"));
358
370
  init_initConfigs();
359
371
  init_shared();
360
- init_shared();
361
372
  }
362
373
  });
363
374
 
@@ -2434,9 +2445,9 @@ var init_build = __esm({
2434
2445
  init_createCompiler();
2435
2446
  init_initConfigs();
2436
2447
  init_shared();
2437
- build = async (initOptions, { mode = "production", watch, compiler: customCompiler } = {}) => {
2448
+ build = async (initOptions, { watch, compiler: customCompiler } = {}) => {
2438
2449
  if (!process.env.NODE_ENV) {
2439
- process.env.NODE_ENV = mode;
2450
+ process.env.NODE_ENV = "production";
2440
2451
  }
2441
2452
  const { context } = initOptions;
2442
2453
  let compiler;
@@ -2451,28 +2462,24 @@ var init_build = __esm({
2451
2462
  });
2452
2463
  bundlerConfigs = webpackConfigs;
2453
2464
  }
2454
- let isFirstCompile = true;
2455
- await context.hooks.onBeforeBuild.call({
2465
+ registerBuildHook({
2466
+ context,
2456
2467
  bundlerConfigs,
2457
- environments: context.environments
2468
+ compiler,
2469
+ isWatch: Boolean(watch),
2470
+ MultiStatsCtor: import_MultiStats2.default
2458
2471
  });
2459
- const onDone = async (stats) => {
2460
- const p = context.hooks.onAfterBuild.call({
2461
- isFirstCompile,
2462
- stats,
2463
- environments: context.environments
2464
- });
2465
- isFirstCompile = false;
2466
- await p;
2467
- };
2468
- onCompileDone(compiler, onDone, import_MultiStats2.default);
2469
2472
  if (watch) {
2470
- compiler.watch({}, (err) => {
2473
+ const watching = compiler.watch({}, (err) => {
2471
2474
  if (err) {
2472
2475
  import_core7.logger.error(err);
2473
2476
  }
2474
2477
  });
2475
- return;
2478
+ return {
2479
+ close: () => new Promise((resolve) => {
2480
+ watching.close(resolve);
2481
+ })
2482
+ };
2476
2483
  }
2477
2484
  await new Promise(
2478
2485
  (resolve, reject) => {
package/dist/index.js CHANGED
@@ -46,18 +46,18 @@ var __publicField = (obj, key, value) => {
46
46
  return value;
47
47
  };
48
48
 
49
- // ../../../node_modules/.pnpm/@modern-js+module-tools@2.55.0_eslint@9.6.0_typescript@5.5.2/node_modules/@modern-js/module-tools/shims/esm.js
49
+ // ../../../node_modules/.pnpm/@modern-js+module-tools@2.57.1_typescript@5.5.2/node_modules/@modern-js/module-tools/shims/esm.js
50
50
  import { fileURLToPath } from "url";
51
51
  import path from "path";
52
52
  var init_esm = __esm({
53
- "../../../node_modules/.pnpm/@modern-js+module-tools@2.55.0_eslint@9.6.0_typescript@5.5.2/node_modules/@modern-js/module-tools/shims/esm.js"() {
53
+ "../../../node_modules/.pnpm/@modern-js+module-tools@2.57.1_typescript@5.5.2/node_modules/@modern-js/module-tools/shims/esm.js"() {
54
54
  "use strict";
55
55
  }
56
56
  });
57
57
 
58
58
  // src/shared.ts
59
59
  import { __internalHelper } from "@rsbuild/core";
60
- var getChainUtils, initRsbuildConfig, createDevServer, formatStats, getDevMiddleware, getStatsOptions, stringifyConfig, outputInspectConfigFiles, getRsbuildInspectConfig, chainToConfig, modifyBundlerChain, onCompileDone, prettyTime, castArray;
60
+ var getChainUtils, initRsbuildConfig, createDevServer, formatStats, getDevMiddleware, getStatsOptions, stringifyConfig, outputInspectConfigFiles, getRsbuildInspectConfig, chainToConfig, modifyBundlerChain, registerDevHook, registerBuildHook, prettyTime, castArray;
61
61
  var init_shared = __esm({
62
62
  "src/shared.ts"() {
63
63
  "use strict";
@@ -74,7 +74,8 @@ var init_shared = __esm({
74
74
  getRsbuildInspectConfig,
75
75
  chainToConfig,
76
76
  modifyBundlerChain,
77
- onCompileDone,
77
+ registerDevHook,
78
+ registerBuildHook,
78
79
  prettyTime
79
80
  } = __internalHelper);
80
81
  castArray = (arr) => {
@@ -99,8 +100,8 @@ async function inspectConfig({
99
100
  bundlerConfigs,
100
101
  inspectOptions = {}
101
102
  }) {
102
- if (inspectOptions.env) {
103
- process.env.NODE_ENV = inspectOptions.env;
103
+ if (inspectOptions.mode) {
104
+ process.env.NODE_ENV = inspectOptions.mode;
104
105
  } else if (!process.env.NODE_ENV) {
105
106
  process.env.NODE_ENV = "development";
106
107
  }
@@ -123,10 +124,7 @@ async function inspectConfig({
123
124
  inspectOptions,
124
125
  pluginManager
125
126
  });
126
- let outputPath = inspectOptions.outputPath || context.distPath;
127
- if (!isAbsolute(outputPath)) {
128
- outputPath = join(context.rootPath, outputPath);
129
- }
127
+ const outputPath = getInspectOutputPath(context, inspectOptions);
130
128
  if (inspectOptions.writeToDisk) {
131
129
  await outputInspectConfigFiles({
132
130
  rawBundlerConfigs,
@@ -149,12 +147,22 @@ async function inspectConfig({
149
147
  }
150
148
  };
151
149
  }
150
+ var getInspectOutputPath;
152
151
  var init_inspectConfig = __esm({
153
152
  "src/inspectConfig.ts"() {
154
153
  "use strict";
155
154
  init_esm();
156
155
  init_initConfigs();
157
156
  init_shared();
157
+ getInspectOutputPath = (context, inspectOptions) => {
158
+ if (inspectOptions.outputPath) {
159
+ if (isAbsolute(inspectOptions.outputPath)) {
160
+ return inspectOptions.outputPath;
161
+ }
162
+ return join(context.distPath, inspectOptions.outputPath);
163
+ }
164
+ return context.distPath;
165
+ };
158
166
  }
159
167
  });
160
168
 
@@ -166,10 +174,10 @@ import {
166
174
  import { reduceConfigsWithContext } from "reduce-configs";
167
175
  async function modifyWebpackChain(context, utils, chain) {
168
176
  logger.debug("modify webpack chain");
169
- const [modifiedChain] = await context.hooks.modifyWebpackChain.call(
170
- chain,
171
- utils
172
- );
177
+ const [modifiedChain] = await context.hooks.modifyWebpackChain.callInEnvironment({
178
+ environment: utils.environment.name,
179
+ args: [chain, utils]
180
+ });
173
181
  if (utils.environment.config.tools?.webpackChain) {
174
182
  for (const item of castArray(utils.environment.config.tools.webpackChain)) {
175
183
  item(modifiedChain, utils);
@@ -180,10 +188,10 @@ async function modifyWebpackChain(context, utils, chain) {
180
188
  }
181
189
  async function modifyWebpackConfig(context, webpackConfig, utils) {
182
190
  logger.debug("modify webpack config");
183
- let [modifiedConfig] = await context.hooks.modifyWebpackConfig.call(
184
- webpackConfig,
185
- utils
186
- );
191
+ let [modifiedConfig] = await context.hooks.modifyWebpackConfig.callInEnvironment({
192
+ environment: utils.environment.name,
193
+ args: [webpackConfig, utils]
194
+ });
187
195
  if (utils.environment.config.tools?.webpack) {
188
196
  modifiedConfig = reduceConfigsWithContext({
189
197
  initial: modifiedConfig,
@@ -293,7 +301,11 @@ async function initConfigs({
293
301
  bundlerConfigs: webpackConfigs
294
302
  });
295
303
  };
296
- context.hooks.onBeforeBuild.tap(inspect);
304
+ context.hooks.onBeforeBuild.tap(({ isFirstCompile }) => {
305
+ if (isFirstCompile) {
306
+ inspect();
307
+ }
308
+ });
297
309
  context.hooks.onAfterStartDevServer.tap(inspect);
298
310
  }
299
311
  return {
@@ -329,7 +341,7 @@ async function createCompiler({
329
341
  });
330
342
  const { default: webpack2 } = await import("webpack");
331
343
  const compiler = webpackConfigs.length === 1 ? webpack2(webpackConfigs[0]) : webpack2(webpackConfigs);
332
- const done = async (stats) => {
344
+ const done = (stats) => {
333
345
  const { message, level } = formatStats(
334
346
  stats,
335
347
  getStatsOptions(compiler)
@@ -340,17 +352,18 @@ async function createCompiler({
340
352
  if (level === "warning") {
341
353
  logger3.warn(message);
342
354
  }
343
- if (process.env.NODE_ENV === "development") {
344
- await context.hooks.onDevCompileDone.call({
345
- isFirstCompile,
346
- stats,
347
- environments: context.environments
348
- });
349
- }
350
- isFirstCompile = false;
351
355
  };
352
- let isFirstCompile = true;
353
- onCompileDone(compiler, done, WebpackMultiStats);
356
+ compiler.hooks.done.tap("rsbuild:done", (stats) => {
357
+ done(stats);
358
+ });
359
+ if (context.normalizedConfig?.mode === "development") {
360
+ registerDevHook({
361
+ compiler,
362
+ context,
363
+ bundlerConfigs: webpackConfigs,
364
+ MultiStatsCtor: WebpackMultiStats
365
+ });
366
+ }
354
367
  await context.hooks.onAfterCreateCompiler.call({
355
368
  compiler,
356
369
  environments: context.environments
@@ -380,7 +393,6 @@ var init_createCompiler = __esm({
380
393
  init_esm();
381
394
  init_initConfigs();
382
395
  init_shared();
383
- init_shared();
384
396
  }
385
397
  });
386
398
 
@@ -2482,9 +2494,9 @@ var init_build = __esm({
2482
2494
  init_createCompiler();
2483
2495
  init_initConfigs();
2484
2496
  init_shared();
2485
- build = async (initOptions, { mode = "production", watch, compiler: customCompiler } = {}) => {
2497
+ build = async (initOptions, { watch, compiler: customCompiler } = {}) => {
2486
2498
  if (!process.env.NODE_ENV) {
2487
- process.env.NODE_ENV = mode;
2499
+ process.env.NODE_ENV = "production";
2488
2500
  }
2489
2501
  const { context } = initOptions;
2490
2502
  let compiler;
@@ -2499,28 +2511,24 @@ var init_build = __esm({
2499
2511
  });
2500
2512
  bundlerConfigs = webpackConfigs;
2501
2513
  }
2502
- let isFirstCompile = true;
2503
- await context.hooks.onBeforeBuild.call({
2514
+ registerBuildHook({
2515
+ context,
2504
2516
  bundlerConfigs,
2505
- environments: context.environments
2517
+ compiler,
2518
+ isWatch: Boolean(watch),
2519
+ MultiStatsCtor: WebpackMultiStats2
2506
2520
  });
2507
- const onDone = async (stats) => {
2508
- const p = context.hooks.onAfterBuild.call({
2509
- isFirstCompile,
2510
- stats,
2511
- environments: context.environments
2512
- });
2513
- isFirstCompile = false;
2514
- await p;
2515
- };
2516
- onCompileDone(compiler, onDone, WebpackMultiStats2);
2517
2521
  if (watch) {
2518
- compiler.watch({}, (err) => {
2522
+ const watching = compiler.watch({}, (err) => {
2519
2523
  if (err) {
2520
2524
  logger6.error(err);
2521
2525
  }
2522
2526
  });
2523
- return;
2527
+ return {
2528
+ close: () => new Promise((resolve) => {
2529
+ watching.close(resolve);
2530
+ })
2531
+ };
2524
2532
  }
2525
2533
  await new Promise(
2526
2534
  (resolve, reject) => {
@@ -1,3 +1,5 @@
1
1
  import type { BuildOptions } from '@rsbuild/core';
2
2
  import { type InitConfigsOptions } from './initConfigs';
3
- export declare const build: (initOptions: InitConfigsOptions, { mode, watch, compiler: customCompiler }?: BuildOptions) => Promise<void>;
3
+ export declare const build: (initOptions: InitConfigsOptions, { watch, compiler: customCompiler }?: BuildOptions) => Promise<void | {
4
+ close: () => Promise<void>;
5
+ }>;
@@ -1,10 +1,10 @@
1
- import { type CreateRsbuildOptions, type PluginManager } from '@rsbuild/core';
1
+ import { type PluginManager, type ResolvedCreateRsbuildOptions } from '@rsbuild/core';
2
2
  import { type InternalContext } from './shared';
3
3
  import type { WebpackConfig } from './types';
4
4
  export type InitConfigsOptions = {
5
5
  context: InternalContext;
6
6
  pluginManager: PluginManager;
7
- rsbuildOptions: Required<CreateRsbuildOptions>;
7
+ rsbuildOptions: ResolvedCreateRsbuildOptions;
8
8
  };
9
9
  export declare function initConfigs({ context, pluginManager, rsbuildOptions, }: InitConfigsOptions): Promise<{
10
10
  webpackConfigs: WebpackConfig[];
@@ -11,7 +11,18 @@ declare const getChainUtils: typeof __internalHelper.getChainUtils, initRsbuildC
11
11
  content: string;
12
12
  }>;
13
13
  environmentConfigs: import("@rsbuild/core").InspectConfigResult["origin"]["environmentConfigs"];
14
- }, chainToConfig: typeof __internalHelper.chainToConfig, modifyBundlerChain: typeof __internalHelper.modifyBundlerChain, onCompileDone: (compiler: import("@rspack/core").Compiler | import("@rspack/core").MultiCompiler, onDone: (stats: import("@rspack/core").Stats | import("@rspack/core").MultiStats) => Promise<void>, MultiStatsCtor: new (stats: import("@rspack/core").Stats[]) => import("@rspack/core").MultiStats) => void, prettyTime: (seconds: number) => string;
15
- export { getChainUtils, initRsbuildConfig, createDevServer, formatStats, getDevMiddleware, getStatsOptions, stringifyConfig, outputInspectConfigFiles, chainToConfig, modifyBundlerChain, onCompileDone, prettyTime, getRsbuildInspectConfig, };
14
+ }, chainToConfig: typeof __internalHelper.chainToConfig, modifyBundlerChain: typeof __internalHelper.modifyBundlerChain, registerDevHook: ({ context, compiler, bundlerConfigs, MultiStatsCtor, }: {
15
+ bundlerConfigs?: import("@rspack/core").Configuration[];
16
+ context: __internalHelper.InternalContext;
17
+ compiler: import("@rspack/core").Compiler | import("@rspack/core").MultiCompiler;
18
+ MultiStatsCtor: new (stats: import("@rspack/core").Stats[]) => import("@rspack/core").MultiStats;
19
+ }) => void, registerBuildHook: ({ context, isWatch, compiler, bundlerConfigs, MultiStatsCtor, }: {
20
+ bundlerConfigs?: import("@rspack/core").Configuration[];
21
+ context: __internalHelper.InternalContext;
22
+ compiler: import("@rspack/core").Compiler | import("@rspack/core").MultiCompiler;
23
+ isWatch: boolean;
24
+ MultiStatsCtor: new (stats: import("@rspack/core").Stats[]) => import("@rspack/core").MultiStats;
25
+ }) => void, prettyTime: (seconds: number) => string;
26
+ export { getChainUtils, initRsbuildConfig, createDevServer, formatStats, getDevMiddleware, getStatsOptions, stringifyConfig, outputInspectConfigFiles, chainToConfig, modifyBundlerChain, registerDevHook, registerBuildHook, prettyTime, getRsbuildInspectConfig, };
16
27
  export type InternalContext = __internalHelper.InternalContext;
17
28
  export declare const castArray: <T>(arr?: T | T[]) => T[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rsbuild/webpack",
3
- "version": "1.0.1-beta.1",
3
+ "version": "1.0.1-beta.11",
4
4
  "homepage": "https://rsbuild.dev",
5
5
  "repository": {
6
6
  "type": "git",
@@ -30,7 +30,7 @@
30
30
  "reduce-configs": "^1.0.0",
31
31
  "tsconfig-paths-webpack-plugin": "4.1.0",
32
32
  "webpack": "^5.93.0",
33
- "@rsbuild/core": "1.0.1-beta.1"
33
+ "@rsbuild/core": "1.0.1-beta.11"
34
34
  },
35
35
  "devDependencies": {
36
36
  "@types/node": "18.x",
@@ -38,7 +38,7 @@
38
38
  "cli-truncate": "2.1.0",
39
39
  "patch-console": "1.0.0",
40
40
  "typescript": "^5.5.2",
41
- "@scripts/test-helper": "1.0.1-beta.1"
41
+ "@scripts/test-helper": "1.0.1-beta.11"
42
42
  },
43
43
  "publishConfig": {
44
44
  "access": "public",