Skip to content

Commit

Permalink
fix: use incremental in production build (#8311)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahabhgk authored Nov 1, 2024
1 parent 7f96765 commit 1161016
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module.exports = {
entry: {
main: "./src/index.js"
},
mode: "development",
plugins: [
new rspack.HtmlRspackPlugin({
template: "./src/index.html"
Expand Down
18 changes: 16 additions & 2 deletions packages/rspack-test-tools/tests/defaultsCases/mode/production.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ module.exports = {
- Expected
+ Received
@@ ... @@
- "incremental": Object {
- "buildChunkGraph": false,
- "dependenciesDiagnostics": false,
- "emitAssets": true,
- "inferAsyncModules": false,
- "make": true,
- "modulesCodegen": false,
- "modulesHashes": false,
- "modulesRuntimeRequirements": false,
- "providedExports": false,
- },
+ "incremental": false,
@@ ... @@
- "mode": "none",
+ "mode": "production",
Expand Down Expand Up @@ -51,11 +64,12 @@ module.exports = {
- "usedExports": false,
+ "usedExports": true,
@@ ... @@
- "performance": false,
+ },
+ "performance": Object {
+ "hints": "warning",
+ "maxAssetSize": 250000,
+ "maxEntrypointSize": 250000,
+ },
@@ ... @@
- "performance": false,
`)
};
18 changes: 16 additions & 2 deletions packages/rspack-test-tools/tests/defaultsCases/mode/undefined.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ module.exports = {
- Expected
+ Received
@@ ... @@
- "incremental": Object {
- "buildChunkGraph": false,
- "dependenciesDiagnostics": false,
- "emitAssets": true,
- "inferAsyncModules": false,
- "make": true,
- "modulesCodegen": false,
- "modulesHashes": false,
- "modulesRuntimeRequirements": false,
- "providedExports": false,
- },
+ "incremental": false,
@@ ... @@
- "mode": "none",
+ "mode": undefined,
Expand Down Expand Up @@ -51,11 +64,12 @@ module.exports = {
- "usedExports": false,
+ "usedExports": true,
@@ ... @@
- "performance": false,
+ },
+ "performance": Object {
+ "hints": "warning",
+ "maxAssetSize": 250000,
+ "maxEntrypointSize": 250000,
+ },
@@ ... @@
- "performance": false,
`)
};
44 changes: 37 additions & 7 deletions packages/rspack/src/builtin-plugin/FlagDependencyUsagePlugin.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,39 @@
import { BuiltinPluginName } from "@rspack/binding";
import { type BuiltinPlugin, BuiltinPluginName } from "@rspack/binding";
import type { Compiler } from "../Compiler";
import type { Incremental } from "../config";
import { RspackBuiltinPlugin, createBuiltinPlugin } from "./base";

import { create } from "./base";
export class FlagDependencyUsagePlugin extends RspackBuiltinPlugin {
name = BuiltinPluginName.FlagDependencyUsagePlugin;
affectedHooks = "compilation" as const;

export const FlagDependencyUsagePlugin = create(
BuiltinPluginName.FlagDependencyUsagePlugin,
(global: boolean) => global,
"compilation"
);
constructor(private global: boolean) {
super();
}

raw(compiler: Compiler): BuiltinPlugin {
const incremental = compiler.options.experiments.incremental as Incremental;
const logger = compiler.getInfrastructureLogger(
"rspack.FlagDependencyUsagePlugin"
);
if (incremental.modulesHashes) {
incremental.modulesHashes = false;
logger.warn(
"`optimization.usedExports` can't be used with `incremental.modulesHashes` as export usage is a global effect. `incremental.modulesHashes` has been overridden to false."
);
}
if (incremental.modulesCodegen) {
incremental.modulesCodegen = false;
logger.warn(
"`optimization.usedExports` can't be used with `incremental.modulesCodegen` as export usage is a global effect. `incremental.modulesCodegen` has been overridden to false."
);
}
if (incremental.modulesRuntimeRequirements) {
incremental.modulesRuntimeRequirements = false;
logger.warn(
"`optimization.usedExports` can't be used with `incremental.modulesRuntimeRequirements` as export usage is a global effect. `incremental.modulesRuntimeRequirements` has been overridden to false."
);
}
return createBuiltinPlugin(this.name, this.global);
}
}
44 changes: 37 additions & 7 deletions packages/rspack/src/builtin-plugin/MangleExportsPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,39 @@
import { BuiltinPluginName } from "@rspack/binding";
import { type BuiltinPlugin, BuiltinPluginName } from "@rspack/binding";
import type { Compiler } from "../Compiler";
import type { Incremental } from "../config";
import { RspackBuiltinPlugin, createBuiltinPlugin } from "./base";

import { create } from "./base";
export class MangleExportsPlugin extends RspackBuiltinPlugin {
name = BuiltinPluginName.MangleExportsPlugin;
affectedHooks = "compilation" as const;

export const MangleExportsPlugin = create(
BuiltinPluginName.MangleExportsPlugin,
(deterministic: boolean) => deterministic,
"compilation"
);
constructor(private deterministic: boolean) {
super();
}

raw(compiler: Compiler): BuiltinPlugin {
const incremental = compiler.options.experiments.incremental as Incremental;
const logger = compiler.getInfrastructureLogger(
"rspack.MangleExportsPlugin"
);
if (incremental.modulesHashes) {
incremental.modulesHashes = false;
logger.warn(
"`optimization.mangleExports` can't be used with `incremental.modulesHashes` as export mangling is a global effect. `incremental.modulesHashes` has been overridden to false."
);
}
if (incremental.modulesCodegen) {
incremental.modulesCodegen = false;
logger.warn(
"`optimization.mangleExports` can't be used with `incremental.modulesCodegen` as export mangling is a global effect. `incremental.modulesCodegen` has been overridden to false."
);
}
if (incremental.modulesRuntimeRequirements) {
incremental.modulesRuntimeRequirements = false;
logger.warn(
"`optimization.mangleExports` can't be used with `incremental.modulesRuntimeRequirements` as export mangling is a global effect. `incremental.modulesRuntimeRequirements` has been overridden to false."
);
}
return createBuiltinPlugin(this.name, this.deterministic);
}
}
40 changes: 33 additions & 7 deletions packages/rspack/src/builtin-plugin/ModuleConcatenationPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@
import { BuiltinPluginName } from "@rspack/binding";
import { type BuiltinPlugin, BuiltinPluginName } from "@rspack/binding";
import type { Compiler } from "../Compiler";
import type { Incremental } from "../config";
import { RspackBuiltinPlugin, createBuiltinPlugin } from "./base";

import { create } from "./base";
export class ModuleConcatenationPlugin extends RspackBuiltinPlugin {
name = BuiltinPluginName.ModuleConcatenationPlugin;
affectedHooks = "compilation" as const;

export const ModuleConcatenationPlugin = create(
BuiltinPluginName.ModuleConcatenationPlugin,
() => {},
"compilation"
);
raw(compiler: Compiler): BuiltinPlugin {
const incremental = compiler.options.experiments.incremental as Incremental;
const logger = compiler.getInfrastructureLogger(
"rspack.ModuleConcatenationPlugin"
);
if (incremental.modulesHashes) {
incremental.modulesHashes = false;
logger.warn(
"`optimization.concatenateModules` can't be used with `incremental.modulesHashes` as module concatenation is a global effect. `incremental.modulesHashes` has been overridden to false."
);
}
if (incremental.modulesCodegen) {
incremental.modulesCodegen = false;
logger.warn(
"`optimization.concatenateModules` can't be used with `incremental.modulesCodegen` as module concatenation is a global effect. `incremental.modulesCodegen` has been overridden to false."
);
}
if (incremental.modulesRuntimeRequirements) {
incremental.modulesRuntimeRequirements = false;
logger.warn(
"`optimization.concatenateModules` can't be used with `incremental.modulesRuntimeRequirements` as module concatenation is a global effect. `incremental.modulesRuntimeRequirements` has been overridden to false."
);
}
return createBuiltinPlugin(this.name, undefined);
}
}
13 changes: 8 additions & 5 deletions packages/rspack/src/config/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export const applyRspackOptionsDefaults = (
// but Rspack currently does not support this option
F(options, "cache", () => development);

applyExperimentsDefaults(options.experiments);
applyExperimentsDefaults(options.experiments, { production });

applySnapshotDefaults(options.snapshot, { production });

Expand Down Expand Up @@ -192,7 +192,10 @@ const applyInfrastructureLoggingDefaults = (
D(infrastructureLogging, "appendOnly", !tty);
};

const applyExperimentsDefaults = (experiments: ExperimentsNormalized) => {
const applyExperimentsDefaults = (
experiments: ExperimentsNormalized,
{ production }: { production: boolean }
) => {
D(experiments, "futureDefaults", false);
// IGNORE(experiments.lazyCompilation): In webpack, lazyCompilation is undefined by default
D(experiments, "lazyCompilation", false);
Expand All @@ -202,17 +205,17 @@ const applyExperimentsDefaults = (experiments: ExperimentsNormalized) => {
D(experiments, "topLevelAwait", true);

// IGNORE(experiments.incremental): Rspack specific configuration for incremental
D(experiments, "incremental", {});
D(experiments, "incremental", !production ? {} : false);
if (typeof experiments.incremental === "object") {
D(experiments.incremental, "make", true);
D(experiments.incremental, "emitAssets", true);
D(experiments.incremental, "inferAsyncModules", false);
D(experiments.incremental, "providedExports", false);
D(experiments.incremental, "dependenciesDiagnostics", false);
D(experiments.incremental, "buildChunkGraph", false);
D(experiments.incremental, "modulesHashes", false);
D(experiments.incremental, "modulesCodegen", false);
D(experiments.incremental, "modulesRuntimeRequirements", false);
D(experiments.incremental, "buildChunkGraph", false);
D(experiments.incremental, "emitAssets", true);
}
// IGNORE(experiments.rspackFuture): Rspack specific configuration
D(experiments, "rspackFuture", {});
Expand Down
24 changes: 20 additions & 4 deletions website/docs/en/config/experiments.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ApiMeta, Stability } from '../../../components/ApiMeta';
import WebpackLicense from '@components/WebpackLicense';
import PropertyType from '@components/PropertyType';

<WebpackLicense from="https://webpack.js.org/configuration/experiments/" />

Expand Down Expand Up @@ -229,8 +230,13 @@ module.exports = {

<ApiMeta addedVersion="1.1.0-beta.0" />

- **Type:** `undefined | boolean | Incremental`
- **Default:** `undefined`
<PropertyType
type="boolean | Incremental"
defaultValueList={[
{ defaultValue: 'false', mode: 'production' },
{ defaultValue: '{ make: true, emitAssets: true }', mode: 'development' },
]}
/>

```ts
type Incremental = {
Expand All @@ -246,11 +252,21 @@ type Incremental = {
};
```

Whether to enable incremental rebuild to speed up the rebuild speed.
Whether to enable incremental rebuild to speed up the rebuild speed. It is recommended to enable it only during development.

```js
const isDev = process.env.NODE_ENV;
module.exports = {
mode: isDev ? 'development' : 'production',
experiments: {
incremental: isDev,
},
};
```

`true` means enable incremental for all stages. `false` means disable incremental for all stages. Incremental can also be enabled only for specified partial stages:

```ts
```js
module.exports = {
experiments: {
// enable incremental for all stages
Expand Down
24 changes: 20 additions & 4 deletions website/docs/zh/config/experiments.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ApiMeta, Stability } from '../../../components/ApiMeta';
import WebpackLicense from '@components/WebpackLicense';
import PropertyType from '@components/PropertyType';

<WebpackLicense from="https://webpack.js.org/configuration/experiments/" />

Expand Down Expand Up @@ -227,8 +228,13 @@ module.exports = {

<ApiMeta addedVersion="1.1.0-beta.0" />

- **类型:** `undefined | boolean | Incremental`
- **默认值:** `undefined`
<PropertyType
type="boolean | Incremental"
defaultValueList={[
{ defaultValue: 'false', mode: 'production' },
{ defaultValue: '{ make: true, emitAssets: true }', mode: 'development' },
]}
/>

```ts
type Incremental = {
Expand All @@ -244,11 +250,21 @@ type Incremental = {
};
```

是否增量地进行重构建,加快重构建的速度。
是否增量地进行重构建,加快重构建或 HMR 的速度,建议仅在开发时启用:

```js
const isDev = process.env.NODE_ENV;
module.exports = {
mode: isDev ? 'development' : 'production',
experiments: {
incremental: isDev,
},
};
```

`true` 表示对全部阶段启用增量,`false` 表示对全部阶段关闭增量,也可以仅对指定的部分阶段开启增量:

```ts
```js
module.exports = {
experiments: {
// 对全部阶段启用增量
Expand Down

4 comments on commit 1161016

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ran ecosystem CI: Open

suite result
modernjs ❌ failure
_selftest ✅ success
rspress ✅ success
rslib ✅ success
rsbuild ❌ failure
examples ❌ failure
devserver ✅ success

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Benchmark detail: Open

Name Base (2024-11-01 4aeee85) Current Change
10000_big_production-mode + exec 47.5 s ± 1.45 s 45.6 s ± 1.66 s -4.06 %
10000_development-mode + exec 2.08 s ± 13 ms 1.83 s ± 19 ms -11.89 %
10000_development-mode_hmr + exec 649 ms ± 13 ms 646 ms ± 7.1 ms -0.59 %
10000_production-mode + exec 2.62 s ± 29 ms 2.42 s ± 47 ms -7.65 %
arco-pro_development-mode + exec 1.78 s ± 79 ms 1.78 s ± 67 ms +0.12 %
arco-pro_development-mode_hmr + exec 428 ms ± 1.1 ms 429 ms ± 2 ms +0.12 %
arco-pro_production-mode + exec 3.22 s ± 82 ms 3.23 s ± 87 ms +0.57 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.22 s ± 79 ms 3.25 s ± 86 ms +0.84 %
threejs_development-mode_10x + exec 1.64 s ± 12 ms 1.58 s ± 13 ms -3.83 %
threejs_development-mode_10x_hmr + exec 784 ms ± 14 ms 776 ms ± 3.8 ms -0.99 %
threejs_production-mode_10x + exec 5.03 s ± 52 ms 4.95 s ± 17 ms -1.54 %

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ran ecosystem CI: Open

suite result
modernjs ❌ failure
_selftest ✅ success
rspress ✅ success
rslib ✅ success
rsbuild ❌ failure
examples ❌ failure
devserver ✅ success

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Benchmark detail: Open

Name Base (2024-11-04 a987332) Current Change
10000_big_production-mode + exec 45.9 s ± 613 ms 45.7 s ± 1.68 s -0.49 %
10000_development-mode + exec 1.84 s ± 17 ms 1.83 s ± 14 ms -0.72 %
10000_development-mode_hmr + exec 645 ms ± 12 ms 649 ms ± 12 ms +0.60 %
10000_production-mode + exec 2.4 s ± 19 ms 2.41 s ± 23 ms +0.68 %
arco-pro_development-mode + exec 1.79 s ± 63 ms 1.8 s ± 61 ms +0.74 %
arco-pro_development-mode_hmr + exec 429 ms ± 1.2 ms 429 ms ± 2.3 ms +0.17 %
arco-pro_production-mode + exec 3.19 s ± 87 ms 3.21 s ± 68 ms +0.84 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.27 s ± 50 ms 3.25 s ± 85 ms -0.59 %
threejs_development-mode_10x + exec 1.58 s ± 7.7 ms 1.58 s ± 16 ms -0.27 %
threejs_development-mode_10x_hmr + exec 784 ms ± 4.2 ms 782 ms ± 9.4 ms -0.22 %
threejs_production-mode_10x + exec 4.93 s ± 40 ms 4.94 s ± 21 ms +0.19 %

Please sign in to comment.