Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add asset modules concatenation #15515

Merged
merged 5 commits into from
Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
63 changes: 38 additions & 25 deletions lib/ChunkGraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,20 +220,7 @@ class ChunkGraphChunk {
/** @type {Set<string>} */
this.runtimeRequirementsInTree = new Set();

this._modulesBySourceType = undefined;
}

get modulesBySourceType() {
if (!this._modulesBySourceType) {
if (this.sourceTypesByModule) {
this._modulesBySourceType = modulesBySourceType(
this.sourceTypesByModule
);
} else {
return defaultModulesBySourceType;
}
}
return this._modulesBySourceType;
this._modulesBySourceType = defaultModulesBySourceType;
}
}

Expand Down Expand Up @@ -336,6 +323,7 @@ class ChunkGraph {
const cgm = this._getChunkGraphModule(module);
const cgc = this._getChunkGraphChunk(chunk);
cgc.modules.delete(module);
// No need to invalidate cgc._modulesBySourceType because we modified cgc.modules anyway
if (cgc.sourceTypesByModule) cgc.sourceTypesByModule.delete(module);
cgm.chunks.delete(chunk);
}
Expand Down Expand Up @@ -590,7 +578,7 @@ class ChunkGraph {
getChunkModulesIterableBySourceType(chunk, sourceType) {
const cgc = this._getChunkGraphChunk(chunk);
const modulesWithSourceType = cgc.modules
.getFromUnorderedCache(cgc.modulesBySourceType)
.getFromUnorderedCache(cgc._modulesBySourceType)
.get(sourceType);
return modulesWithSourceType;
}
Expand All @@ -606,6 +594,8 @@ class ChunkGraph {
cgc.sourceTypesByModule = new WeakMap();
vankop marked this conversation as resolved.
Show resolved Hide resolved
}
cgc.sourceTypesByModule.set(module, sourceTypes);
// Update cgc._modulesBySourceType to invalidate the cache
cgc._modulesBySourceType = modulesBySourceType(cgc.sourceTypesByModule);
}

/**
Expand All @@ -626,25 +616,44 @@ class ChunkGraph {
* @returns {Set<string>} source types
*/
getModuleSourceTypes(module) {
return (
this._getOverwrittenModuleSourceTypes(module) || module.getSourceTypes()
);
}

/**
* @param {Module} module module
* @returns {Set<string> | undefined} source types
*/
_getOverwrittenModuleSourceTypes(module) {
let newSet = false;
let sourceTypes;
for (const chunk of this.getModuleChunksIterable(module)) {
const cgc = this._getChunkGraphChunk(chunk);
if (cgc.sourceTypesByModule === undefined) continue;
if (cgc.sourceTypesByModule === undefined) return;
const st = cgc.sourceTypesByModule.get(module);
if (st) {
if (!sourceTypes) {
sourceTypes = st;
continue;
} else if (!newSet) {
newSet = true;
sourceTypes = new Set(sourceTypes);
if (st === undefined) return;
if (!sourceTypes) {
sourceTypes = st;
continue;
} else if (!newSet) {
for (const type of st) {
if (!newSet) {
if (!sourceTypes.has(type)) {
newSet = true;
sourceTypes = new Set(sourceTypes);
sourceTypes.add(type);
}
} else {
sourceTypes.add(type);
}
}
} else {
for (const type of st) sourceTypes.add(type);
}
}

return sourceTypes || module.getSourceTypes();
return sourceTypes;
}

/**
Expand All @@ -667,7 +676,7 @@ class ChunkGraph {
getOrderedChunkModulesIterableBySourceType(chunk, sourceType, comparator) {
const cgc = this._getChunkGraphChunk(chunk);
const modulesWithSourceType = cgc.modules
.getFromUnorderedCache(cgc.modulesBySourceType)
.getFromUnorderedCache(cgc._modulesBySourceType)
.get(sourceType);
if (modulesWithSourceType === undefined) return undefined;
modulesWithSourceType.sortWith(comparator);
Expand Down Expand Up @@ -1547,6 +1556,10 @@ Caller might not support runtime-dependent code generation (opt-out via optimiza
const graphHash = cgm.graphHashes.provide(runtime, () => {
const hash = createHash(this._hashFunction);
hash.update(`${cgm.id}${this.moduleGraph.isAsync(module)}`);
const sourceTypes = this._getOverwrittenModuleSourceTypes(module);
if (sourceTypes !== undefined) {
for (const type of sourceTypes) hash.update(type);
vankop marked this conversation as resolved.
Show resolved Hide resolved
}
this.moduleGraph.getExportsInfo(module).updateHash(hash, runtime);
return BigInt(`0x${/** @type {string} */ (hash.digest("hex"))}`);
});
Expand Down
2 changes: 1 addition & 1 deletion lib/Module.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const makeSerializable = require("./util/makeSerializable");
* @property {ConcatenationScope=} concatenationScope when in concatenated module, information about other concatenated modules
* @property {CodeGenerationResults} codeGenerationResults code generation results of other modules (need to have a codeGenerationDependency to use that)
* @property {Compilation=} compilation the compilation
* @property {Omit<Set<string>, "delete"|"add">=} sourceTypes source types
* @property {ReadonlySet<string>=} sourceTypes source types
*/

/**
Expand Down
20 changes: 10 additions & 10 deletions test/__snapshots__/StatsTestCases.basictest.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,13 @@ exports[`StatsTestCases should print correct stats for asset 1`] = `
asset bundle.js 13.4 KiB [emitted] (name: main)
asset static/file.html 12 bytes [emitted] [from: static/file.html] (auxiliary name: main)
runtime modules 1.06 KiB 2 modules
modules by path ./ 9.35 KiB (javascript) 14.6 KiB (asset)
modules by path ./ 9.36 KiB (javascript) 14.6 KiB (asset)
modules by path ./images/ 8.86 KiB (javascript) 14.6 KiB (asset)
./images/file.png 42 bytes (javascript) 14.6 KiB (asset) [built] [code generated]
./images/file.svg 915 bytes [built] [code generated]
./images/file.jpg 7.92 KiB [built] [code generated]
modules by path ./*.js 424 bytes
./index.js 399 bytes [built] [code generated]
modules by path ./*.js 427 bytes
./index.js 402 bytes [built] [code generated]
./a.source.js 25 bytes [built] [code generated]
./static/file.html 42 bytes (javascript) 12 bytes (asset) [built] [code generated]
./a.css 41.4 bytes [built] [code generated]
Expand Down Expand Up @@ -2582,15 +2582,15 @@ LOG from webpack.FileSystemInfo
exports[`StatsTestCases should print correct stats for real-content-hash 1`] = `
"a-normal:
assets by path *.js 3.2 KiB
asset 47b075da20b490895144-47b075.js 2.75 KiB [emitted] [immutable] [minimized] (name: runtime)
asset e9785128a82e17f93bc4-e97851.js 2.75 KiB [emitted] [immutable] [minimized] (name: runtime)
asset f96e917feecf51c4fc5c-f96e91.js 232 bytes [emitted] [immutable] [minimized] (name: lazy)
asset f17033ffbf027f2d71b7-f17033.js 212 bytes [emitted] [immutable] [minimized] (name: index)
asset 666f2b8847021ccc7608-666f2b.js 21 bytes [emitted] [immutable] [minimized] (name: a, b)
assets by chunk 20.4 KiB (auxiliary name: lazy)
asset 89a353e9c515885abd8e.png 14.6 KiB [emitted] [immutable] [from: file.png] (auxiliary name: lazy)
asset 7382fad5b015914e0811.jpg?query 5.89 KiB [cached] [immutable] [from: file.jpg?query] (auxiliary name: lazy)
asset 7382fad5b015914e0811.jpg 5.89 KiB [emitted] [immutable] [from: file.jpg] (auxiliary name: index)
Entrypoint index 2.96 KiB (5.89 KiB) = 47b075da20b490895144-47b075.js 2.75 KiB f17033ffbf027f2d71b7-f17033.js 212 bytes 1 auxiliary asset
Entrypoint index 2.96 KiB (5.89 KiB) = e9785128a82e17f93bc4-e97851.js 2.75 KiB f17033ffbf027f2d71b7-f17033.js 212 bytes 1 auxiliary asset
Entrypoint a 21 bytes = 666f2b8847021ccc7608-666f2b.js
Entrypoint b 21 bytes = 666f2b8847021ccc7608-666f2b.js
runtime modules 7.28 KiB 9 modules
Expand All @@ -2609,15 +2609,15 @@ exports[`StatsTestCases should print correct stats for real-content-hash 1`] = `

b-normal:
assets by path *.js 3.2 KiB
asset e9785128a82e17f93bc4-e97851.js 2.75 KiB [emitted] [immutable] [minimized] (name: runtime)
asset 165a2ea225896183fda9-165a2e.js 2.75 KiB [emitted] [immutable] [minimized] (name: runtime)
asset f96e917feecf51c4fc5c-f96e91.js 232 bytes [emitted] [immutable] [minimized] (name: lazy)
asset f17033ffbf027f2d71b7-f17033.js 212 bytes [emitted] [immutable] [minimized] (name: index)
asset 666f2b8847021ccc7608-666f2b.js 21 bytes [emitted] [immutable] [minimized] (name: a, b)
assets by chunk 20.4 KiB (auxiliary name: lazy)
asset 89a353e9c515885abd8e.png 14.6 KiB [emitted] [immutable] [from: file.png] (auxiliary name: lazy)
asset 7382fad5b015914e0811.jpg?query 5.89 KiB [cached] [immutable] [from: file.jpg?query] (auxiliary name: lazy)
asset 7382fad5b015914e0811.jpg 5.89 KiB [emitted] [immutable] [from: file.jpg] (auxiliary name: index)
Entrypoint index 2.96 KiB (5.89 KiB) = e9785128a82e17f93bc4-e97851.js 2.75 KiB f17033ffbf027f2d71b7-f17033.js 212 bytes 1 auxiliary asset
Entrypoint index 2.96 KiB (5.89 KiB) = 165a2ea225896183fda9-165a2e.js 2.75 KiB f17033ffbf027f2d71b7-f17033.js 212 bytes 1 auxiliary asset
Entrypoint a 21 bytes = 666f2b8847021ccc7608-666f2b.js
Entrypoint b 21 bytes = 666f2b8847021ccc7608-666f2b.js
runtime modules 7.28 KiB 9 modules
Expand All @@ -2636,8 +2636,8 @@ b-normal:

a-source-map:
assets by path *.js 3.42 KiB
asset d53ad272b1161b559c05-d53ad2.js 2.8 KiB [emitted] [immutable] [minimized] (name: runtime)
sourceMap d53ad272b1161b559c05-d53ad2.js.map 14.5 KiB [emitted] [dev] (auxiliary name: runtime)
asset 1289a35df2e6455ef167-1289a3.js 2.8 KiB [emitted] [immutable] [minimized] (name: runtime)
sourceMap 1289a35df2e6455ef167-1289a3.js.map 14.5 KiB [emitted] [dev] (auxiliary name: runtime)
asset 0a8aef384737d9f64f44-0a8aef.js 288 bytes [emitted] [immutable] [minimized] (name: lazy)
sourceMap 0a8aef384737d9f64f44-0a8aef.js.map 409 bytes [emitted] [dev] (auxiliary name: lazy)
asset da629d4acf5998c06668-da629d.js 268 bytes [emitted] [immutable] [minimized] (name: index)
Expand All @@ -2648,7 +2648,7 @@ a-source-map:
asset 89a353e9c515885abd8e.png 14.6 KiB [emitted] [immutable] [from: file.png] (auxiliary name: lazy)
asset 7382fad5b015914e0811.jpg?query 5.89 KiB [cached] [immutable] [from: file.jpg?query] (auxiliary name: lazy)
asset 7382fad5b015914e0811.jpg 5.89 KiB [emitted] [immutable] [from: file.jpg] (auxiliary name: index)
Entrypoint index 3.06 KiB (20.7 KiB) = d53ad272b1161b559c05-d53ad2.js 2.8 KiB da629d4acf5998c06668-da629d.js 268 bytes 3 auxiliary assets
Entrypoint index 3.06 KiB (20.7 KiB) = 1289a35df2e6455ef167-1289a3.js 2.8 KiB da629d4acf5998c06668-da629d.js 268 bytes 3 auxiliary assets
Entrypoint a 77 bytes (254 bytes) = 222c2acc68675174e6b2-222c2a.js 1 auxiliary asset
Entrypoint b 77 bytes (254 bytes) = 222c2acc68675174e6b2-222c2a.js 1 auxiliary asset
runtime modules 7.28 KiB 9 modules
Expand Down
6 changes: 3 additions & 3 deletions test/statsCases/asset/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import png from "./images/file.png";
import svg from "./images/file.svg";
import jpg from "./images/file.jpg";
import html from "./static/file.html";
import text1 from "data:text/plain;base64,szsaAAdsadasdfafasfasAADas123aasdasd=="
import text2 from "data:text/plain,asd="
import text3 from "data:text/plain,XXXXXXXXXXXXXXXXX" // 17 chars
import text1 from "data:text/plain;base64,szsaAAdsadasdfafasfasAADas123aasdasd==";
import text2 from "data:text/plain,asd=";
import text3 from "data:text/plain,XXXXXXXXXXXXXXXXX"; // 17 chars
import css from "./a.css";
import source from "./a.source";
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions test/watchCases/cache/asset-concat/0/images/file.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions test/watchCases/cache/asset-concat/0/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import png from "./images/file.png";
import svg from "./images/file.svg";
import jpg from "./images/file.jpg";

it("should generate assets correctly", () => {
expect(png).toMatch(/^data:image\/png;base64,/);
expect(svg).toMatch(/^data:image\/svg\+xml;base64,/);
expect(jpg).toMatch(/\.jpg$/);
});
9 changes: 9 additions & 0 deletions test/watchCases/cache/asset-concat/1/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
it("should generate assets correctly", () => {
const png = require("./images/file.png");
const svg = require("./images/file.svg");
const jpg = require("./images/file.jpg");

expect(png).toMatch(/^data:image\/png;base64,/);
expect(svg).toMatch(/^data:image\/svg\+xml;base64,/);
expect(jpg).toMatch(/\.jpg$/);
});
26 changes: 26 additions & 0 deletions test/watchCases/cache/asset-concat/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/** @type {import("../../../../").Configuration} */
module.exports = {
mode: "development",
cache: {
type: "memory"
},
module: {
rules: [
{
test: /\.png$/,
type: "asset/inline"
},
{
test: /\.jpg$/,
type: "asset/resource"
},
{
test: /\.svg$/,
type: "asset"
}
]
},
optimization: {
concatenateModules: true
}
};