-
-
Notifications
You must be signed in to change notification settings - Fork 579
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
optimization.moduleIds: 'natural'
(#6685)
* feat: add `optimization.moduleIds: 'natural'` Also, set default value to `'natural'` when `mode === 'none'`. * fix clippy * test: update css-extract snapshot * docs: update optimization.moduleIds * revert defaults change * test: add a config case for `moduleIds: 'natural'` * update css-extract snapshot
- Loading branch information
Showing
20 changed files
with
144 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use rspack_core::{ | ||
compare_modules_by_pre_order_index_or_identifier, ApplyContext, CompilationModuleIds, | ||
CompilerOptions, Plugin, PluginContext, | ||
}; | ||
use rspack_error::Result; | ||
use rspack_hook::{plugin, plugin_hook}; | ||
|
||
use crate::id_helpers::{assign_ascending_module_ids, get_used_module_ids_and_modules}; | ||
|
||
#[plugin] | ||
#[derive(Debug, Default)] | ||
pub struct NaturalModuleIdsPlugin; | ||
|
||
#[plugin_hook(CompilationModuleIds for NaturalModuleIdsPlugin)] | ||
fn module_ids(&self, compilation: &mut rspack_core::Compilation) -> Result<()> { | ||
let (used_ids, mut modules_in_natural_order) = get_used_module_ids_and_modules(compilation, None); | ||
|
||
let mut chunk_graph = std::mem::take(&mut compilation.chunk_graph); | ||
let module_graph = compilation.get_module_graph(); | ||
|
||
modules_in_natural_order | ||
.sort_unstable_by(|a, b| compare_modules_by_pre_order_index_or_identifier(&module_graph, a, b)); | ||
|
||
let modules_in_natural_order = modules_in_natural_order | ||
.into_iter() | ||
.filter_map(|i| module_graph.module_by_identifier(&i)) | ||
.collect::<Vec<_>>(); | ||
|
||
assign_ascending_module_ids(&used_ids, modules_in_natural_order, &mut chunk_graph); | ||
|
||
compilation.chunk_graph = chunk_graph; | ||
|
||
Ok(()) | ||
} | ||
|
||
impl Plugin for NaturalModuleIdsPlugin { | ||
fn name(&self) -> &'static str { | ||
"NaturalModuleIdsPlugin" | ||
} | ||
|
||
fn apply( | ||
&self, | ||
ctx: PluginContext<&mut ApplyContext>, | ||
_options: &mut CompilerOptions, | ||
) -> Result<()> { | ||
ctx | ||
.context | ||
.compilation_hooks | ||
.module_ids | ||
.tap(module_ids::new(self)); | ||
Ok(()) | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/rspack-test-tools/tests/configCases/loader/options/rspack.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
packages/rspack-test-tools/tests/configCases/optimization/module-ids-natural/a.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export const a = 42; | ||
|
||
import b from "./b.js"; | ||
|
||
it("should load c.js", () => { | ||
expect(b).toBe("foo"); | ||
|
||
const d = require("./d.js"); | ||
|
||
expect(d.default).toEqual({}); | ||
}); | ||
|
||
export { b } | ||
|
||
export const c = require("./c.js"); |
1 change: 1 addition & 0 deletions
1
packages/rspack-test-tools/tests/configCases/optimization/module-ids-natural/b.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = "foo"; |
3 changes: 3 additions & 0 deletions
3
packages/rspack-test-tools/tests/configCases/optimization/module-ids-natural/c.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function c() { | ||
return "bar" | ||
} |
1 change: 1 addition & 0 deletions
1
packages/rspack-test-tools/tests/configCases/optimization/module-ids-natural/d.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default {} |
7 changes: 7 additions & 0 deletions
7
packages/rspack-test-tools/tests/configCases/optimization/module-ids-natural/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { a, b } from "./a.js"; | ||
|
||
it('should load a.js', () => { | ||
expect(a).toBe(42) | ||
|
||
expect(b).toBe("foo") | ||
}) |
6 changes: 6 additions & 0 deletions
6
...ages/rspack-test-tools/tests/configCases/optimization/module-ids-natural/rspack.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** @type {import("@rspack/core").Configuration} */ | ||
module.exports = { | ||
optimization: { | ||
moduleIds: "natural" | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { BuiltinPluginName } from "@rspack/binding"; | ||
|
||
import { create } from "./base"; | ||
|
||
export const NaturalModuleIdsPlugin = create( | ||
BuiltinPluginName.NaturalModuleIdsPlugin, | ||
() => {}, | ||
"compilation" | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -893,8 +893,10 @@ const applyOptimizationDefaults = ( | |
D(optimization, "removeAvailableModules", true); | ||
D(optimization, "removeEmptyChunks", true); | ||
D(optimization, "mergeDuplicateChunks", true); | ||
F(optimization, "moduleIds", (): "named" | "deterministic" => { | ||
F(optimization, "moduleIds", (): "natural" | "named" | "deterministic" => { | ||
if (production) return "deterministic"; | ||
if (development) return "named"; | ||
// TODO([email protected]): change to `"natural"` | ||
return "named"; | ||
}); | ||
F(optimization, "chunkIds", (): "named" | "deterministic" => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
825883b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 Benchmark detail: Open
825883b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 Ran ecosystem CI: Open