Skip to content

Commit

Permalink
feat: make namedChunkGroups iterable
Browse files Browse the repository at this point in the history
  • Loading branch information
inottn committed Sep 9, 2024
1 parent 8134f86 commit c9ccf76
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,36 @@ function plugin(compiler) {
expect(namedChunkGroups.get("a").getFiles()).toEqual(['a.js']);
expect(namedChunkGroups.get("b").getFiles()).toEqual(['b.js']);

// for of
const result1 = [];
for (const [key, value] of namedChunkGroups) {
result1.push([key, value.getFiles()]);
}
result1.sort(([a], [b]) => (a > b ? 1 : -1));
expect(result1).toEqual([
["a", ["a.js"]],
["b", ["b.js"]]
]);

// forEach
const result2 = [];
namedChunkGroups.forEach((value, key) => {
result2.push([key, value.getFiles()]);
});
result2.sort(([a], [b]) => (a > b ? 1 : -1));
expect(result2).toEqual([
["a", ["a.js"]],
["b", ["b.js"]]
]);

// values
const result3 = [];
for (const value of namedChunkGroups.values()) {
result3.push(value.getFiles());
}
result3.sort(([a], [b]) => (a[0] > b[0] ? 1 : -1));
expect(result3).toEqual([["a.js"], ["b.js"]]);

const origins = chunkGroups.reduce((res, i) => {
res.push(...i.origins.map(i => i.module?.rawRequest).filter(Boolean));
return res;
Expand Down
17 changes: 9 additions & 8 deletions packages/rspack/src/Compilation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import { StatsFactory } from "./stats/StatsFactory";
import { StatsPrinter } from "./stats/StatsPrinter";
import { type AssetInfo, JsAssetInfo } from "./util/AssetInfo";
import MergeCaller from "./util/MergeCaller";
import { createReadonlyMap } from "./util/createReadonlyMap";
import { createFakeCompilationDependencies } from "./util/fake";
import type { InputFileSystem } from "./util/fs";
import type Hash from "./util/hash";
Expand Down Expand Up @@ -403,10 +404,10 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
/**
* Get the named chunk groups.
*
* Note: This is a proxy for webpack internal API, only method `get` and `keys` are supported now.
* Note: This is a proxy for webpack internal API, only method `get`, `keys`, `values` and `entries` are supported now.
*/
get namedChunkGroups(): ReadonlyMap<string, Readonly<ChunkGroup>> {
return {
get namedChunkGroups() {
return createReadonlyMap<ChunkGroup>({
keys: (): IterableIterator<string> => {
const names = this.#inner.getNamedChunkGroupKeys();
return names[Symbol.iterator]();
Expand All @@ -417,7 +418,7 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
return chunk && ChunkGroup.__from_binding(chunk, this.#inner);
}
}
} as Map<string, Readonly<ChunkGroup>>;
});
}

get modules(): ReadonlySet<Module> {
Expand All @@ -433,10 +434,10 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
/**
* Get the named chunks.
*
* Note: This is a proxy for webpack internal API, only method `get` and `keys` is supported now.
* Note: This is a proxy for webpack internal API, only method `get`, `keys`, `values` and `entries` are supported now.
*/
get namedChunks(): ReadonlyMap<string, Readonly<Chunk>> {
return {
get namedChunks() {
return createReadonlyMap<Chunk>({
keys: (): IterableIterator<string> => {
const names = this.#inner.getNamedChunkKeys();
return names[Symbol.iterator]();
Expand All @@ -447,7 +448,7 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
return chunk && Chunk.__from_binding(chunk, this.#inner);
}
}
} as Map<string, Readonly<Chunk>>;
});
}

get entries(): Map<string, EntryData> {
Expand Down
34 changes: 34 additions & 0 deletions packages/rspack/src/util/createReadonlyMap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
export function createReadonlyMap<T>(
obj: Pick<ReadonlyMap<string, T>, "get" | "keys">
) {
return {
...obj,
*values() {
const keys = this.keys();
for (const key of keys) {
yield this.get(key);
}
},
*entries() {
const keys = this.keys();
for (const key of keys) {
yield [key, this.get(key)];
}
},
forEach(
callback: (
value: T,
key: string,
map: ReadonlyMap<string, Readonly<T>>
) => void,
thisArg?: any
): void {
for (const [key, value] of this) {
callback.call(thisArg, value, key, this);
}
},
[Symbol.iterator]() {
return this.entries();
}
} as ReadonlyMap<string, Readonly<T>>;
}

0 comments on commit c9ccf76

Please sign in to comment.