Skip to content

Commit

Permalink
add ability to set module source types in chunk
Browse files Browse the repository at this point in the history
  • Loading branch information
vankop committed Mar 16, 2022
1 parent 5f86eb5 commit 1c0f5c7
Show file tree
Hide file tree
Showing 13 changed files with 146 additions and 56 deletions.
30 changes: 24 additions & 6 deletions lib/ChunkGraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,17 @@ const getModuleRuntimes = chunks => {
};

/**
* @param {SortableSet<Module>} set the set
* @returns {Map<string, SortableSet<Module>>} modules by source type
* @param {WeakMap<Module, Set<string>> | undefined} sourceTypesByModule sourceTypesByModule
* @returns {function (SortableSet<Module>): Map<string, SortableSet<Module>>} modules by source type
*/
const modulesBySourceType = set => {
const modulesBySourceType = sourceTypesByModule => set => {
/** @type {Map<string, SortableSet<Module>>} */
const map = new Map();
for (const module of set) {
for (const sourceType of module.getSourceTypes()) {
const sourceTypes =
(sourceTypesByModule && sourceTypesByModule.get(module)) ||
module.getSourceTypes();
for (const sourceType of sourceTypes) {
let innerSet = map.get(sourceType);
if (innerSet === undefined) {
innerSet = new SortableSet();
Expand Down Expand Up @@ -201,6 +204,8 @@ class ChunkGraphChunk {
constructor() {
/** @type {SortableSet<Module>} */
this.modules = new SortableSet();
/** @type {WeakMap<Module, Set<string>> | undefined} */
this.sourceTypesByModule = undefined;
/** @type {Map<Module, Entrypoint>} */
this.entryModules = new Map();
/** @type {SortableSet<RuntimeModule>} */
Expand Down Expand Up @@ -568,11 +573,24 @@ class ChunkGraph {
getChunkModulesIterableBySourceType(chunk, sourceType) {
const cgc = this._getChunkGraphChunk(chunk);
const modulesWithSourceType = cgc.modules
.getFromUnorderedCache(modulesBySourceType)
.getFromUnorderedCache(modulesBySourceType(cgc.sourceTypesByModule))
.get(sourceType);
return modulesWithSourceType;
}

/**
* @param {Chunk} chunk chunk
* @param {Module} module chunk module
* @param {Set<string>} sourceTypes source types
*/
setChunkModuleSourceTypes(chunk, module, sourceTypes) {
const cgc = this._getChunkGraphChunk(chunk);
if (cgc.sourceTypesByModule === undefined) {
cgc.sourceTypesByModule = new WeakMap();
}
cgc.sourceTypesByModule.set(module, sourceTypes);
}

/**
* @param {Chunk} chunk the chunk
* @param {function(Module, Module): -1|0|1} comparator comparator function
Expand All @@ -593,7 +611,7 @@ class ChunkGraph {
getOrderedChunkModulesIterableBySourceType(chunk, sourceType, comparator) {
const cgc = this._getChunkGraphChunk(chunk);
const modulesWithSourceType = cgc.modules
.getFromUnorderedCache(modulesBySourceType)
.getFromUnorderedCache(modulesBySourceType(cgc.sourceTypesByModule))
.get(sourceType);
if (modulesWithSourceType === undefined) return undefined;
modulesWithSourceType.sortWith(comparator);
Expand Down
13 changes: 12 additions & 1 deletion lib/optimize/ModuleConcatenationPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,18 @@ class ModuleConcatenationPlugin {
for (const chunk of chunkGraph.getModuleChunksIterable(
rootModule
)) {
chunkGraph.disconnectChunkAndModule(chunk, m);
const sourceTypes = m.getSourceTypes();
if (sourceTypes.size === 1) {
chunkGraph.disconnectChunkAndModule(chunk, m);
} else {
const newSourceTypes = new Set(sourceTypes);
newSourceTypes.delete("javascript");
chunkGraph.setChunkModuleSourceTypes(
chunk,
m,
newSourceTypes
);
}
}
}
}
Expand Down
87 changes: 51 additions & 36 deletions test/__snapshots__/StatsTestCases.basictest.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -157,29 +157,36 @@ webpack/runtime/make namespace object 274 bytes {main} [code generated]

exports[`StatsTestCases should print correct stats for asset 1`] = `
"asset 89a353e9c515885abd8e.png 14.6 KiB [emitted] [immutable] [from: images/file.png] (auxiliary name: main)
asset bundle.js 13.5 KiB [emitted] (name: main)
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.36 KiB (javascript) 14.6 KiB (asset)
modules by path ./ 9.35 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 452 bytes
modules by path ./*.js 424 bytes
./index.js 399 bytes [built] [code generated]
./a.source.js 53.4 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 16 bytes [built] [code generated]
./a.css 41.4 bytes [built] [code generated]
modules by mime type text/plain 172 bytes
data:text/plain;base64,szsaAAdsadasdfaf.. 72.2 bytes [built] [code generated]
data:text/plain,asd= 41.4 bytes [built] [code generated]
data:text/plain,XXXXXXXXXXXXXXX.. 58.8 bytes [built] [code generated]
webpack x.x.x compiled successfully in X ms
webpack x.x.x compiled successfully in X ms"
`;

asset bundle.js 11.7 KiB [emitted] (name: main)
orphan modules 9.13 KiB (javascript) 14.6 KiB (asset) [orphan] 9 modules
exports[`StatsTestCases should print correct stats for asset-concat 1`] = `
"asset 89a353e9c515885abd8e.png 14.6 KiB [emitted] [immutable] [from: images/file.png] (auxiliary name: main)
asset bundle.js 12.5 KiB [emitted] (name: main)
asset static/file.html 12 bytes [emitted] [from: static/file.html] (auxiliary name: main)
orphan modules 9.05 KiB [orphan] 7 modules
runtime modules 1.06 KiB 2 modules
./index.js + 9 modules 9.52 KiB [built] [code generated]
cacheable modules 9.6 KiB (javascript) 14.6 KiB (asset)
./index.js + 9 modules 9.52 KiB [built] [code generated]
./images/file.png 42 bytes (javascript) 14.6 KiB (asset) [built] [code generated]
./static/file.html 42 bytes (javascript) 12 bytes (asset) [built] [code generated]
webpack x.x.x compiled successfully in X ms"
`;

Expand Down Expand Up @@ -2575,80 +2582,86 @@ 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 e9785128a82e17f93bc4-e97851.js 2.75 KiB [emitted] [immutable] [minimized] (name: runtime)
asset 47b075da20b490895144-47b075.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 path *.jpg 11.8 KiB
asset 7382fad5b015914e0811.jpg 5.89 KiB [emitted] [immutable] [from: file.jpg] (auxiliary name: index)
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)
Entrypoint index 2.96 KiB (5.89 KiB) = e9785128a82e17f93bc4-e97851.js 2.75 KiB f17033ffbf027f2d71b7-f17033.js 212 bytes 1 auxiliary asset
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 a 21 bytes = 666f2b8847021ccc7608-666f2b.js
Entrypoint b 21 bytes = 666f2b8847021ccc7608-666f2b.js
runtime modules 7.28 KiB 9 modules
orphan modules 65 bytes (javascript) 14.6 KiB (asset) [orphan] 2 modules
cacheable modules 514 bytes (javascript) 11.8 KiB (asset)
orphan modules 23 bytes [orphan] 1 module
cacheable modules 556 bytes (javascript) 26.3 KiB (asset)
javascript modules 430 bytes
./a/index.js 150 bytes [built] [code generated]
./a/a.js 22 bytes [built] [code generated]
./a/b.js 66 bytes [built] [code generated]
./a/lazy.js + 2 modules 192 bytes [built] [code generated]
asset modules 84 bytes (javascript) 11.8 KiB (asset)
asset modules 126 bytes (javascript) 26.3 KiB (asset)
./a/file.jpg 42 bytes (javascript) 5.89 KiB (asset) [built] [code generated]
./a/file.png 42 bytes (javascript) 14.6 KiB (asset) [built] [code generated]
./a/file.jpg?query 42 bytes (javascript) 5.89 KiB (asset) [built] [code generated]
a-normal (webpack x.x.x) compiled successfully in X ms
b-normal:
assets by path *.js 3.2 KiB
asset 032acf9d06089f57085f-032acf.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 path *.jpg 11.8 KiB
asset 7382fad5b015914e0811.jpg 5.89 KiB [emitted] [immutable] [from: file.jpg] (auxiliary name: index)
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)
Entrypoint index 2.96 KiB (5.89 KiB) = 032acf9d06089f57085f-032acf.js 2.75 KiB f17033ffbf027f2d71b7-f17033.js 212 bytes 1 auxiliary asset
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 a 21 bytes = 666f2b8847021ccc7608-666f2b.js
Entrypoint b 21 bytes = 666f2b8847021ccc7608-666f2b.js
runtime modules 7.28 KiB 9 modules
orphan modules 61 bytes (javascript) 14.6 KiB (asset) [orphan] 2 modules
cacheable modules 469 bytes (javascript) 11.8 KiB (asset)
orphan modules 19 bytes [orphan] 1 module
cacheable modules 511 bytes (javascript) 26.3 KiB (asset)
javascript modules 385 bytes
./b/index.js 109 bytes [built] [code generated]
./b/a.js 22 bytes [built] [code generated]
./b/b.js 66 bytes [built] [code generated]
./b/lazy.js + 2 modules 188 bytes [built] [code generated]
asset modules 84 bytes (javascript) 11.8 KiB (asset)
asset modules 126 bytes (javascript) 26.3 KiB (asset)
./b/file.jpg 42 bytes (javascript) 5.89 KiB (asset) [built] [code generated]
./b/file.png 42 bytes (javascript) 14.6 KiB (asset) [built] [code generated]
./b/file.jpg?query 42 bytes (javascript) 5.89 KiB (asset) [built] [code generated]
b-normal (webpack x.x.x) compiled successfully in X ms
a-source-map:
assets by path *.js 3.42 KiB
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 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 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)
sourceMap da629d4acf5998c06668-da629d.js.map 366 bytes [emitted] [dev] (auxiliary name: index)
asset 222c2acc68675174e6b2-222c2a.js 77 bytes [emitted] [immutable] [minimized] (name: a, b)
sourceMap 222c2acc68675174e6b2-222c2a.js.map 254 bytes [emitted] [dev] (auxiliary name: a, b)
assets by path *.jpg 11.8 KiB
asset 7382fad5b015914e0811.jpg 5.89 KiB [emitted] [immutable] [from: file.jpg] (auxiliary name: index)
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)
Entrypoint index 3.06 KiB (20.7 KiB) = 1289a35df2e6455ef167-1289a3.js 2.8 KiB da629d4acf5998c06668-da629d.js 268 bytes 3 auxiliary assets
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 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
orphan modules 65 bytes (javascript) 14.6 KiB (asset) [orphan] 2 modules
cacheable modules 514 bytes (javascript) 11.8 KiB (asset)
orphan modules 23 bytes [orphan] 1 module
cacheable modules 556 bytes (javascript) 26.3 KiB (asset)
javascript modules 430 bytes
./a/index.js 150 bytes [built] [code generated]
./a/a.js 22 bytes [built] [code generated]
./a/b.js 66 bytes [built] [code generated]
./a/lazy.js + 2 modules 192 bytes [built] [code generated]
asset modules 84 bytes (javascript) 11.8 KiB (asset)
asset modules 126 bytes (javascript) 26.3 KiB (asset)
./a/file.jpg 42 bytes (javascript) 5.89 KiB (asset) [built] [code generated]
./a/file.png 42 bytes (javascript) 14.6 KiB (asset) [built] [code generated]
./a/file.jpg?query 42 bytes (javascript) 5.89 KiB (asset) [built] [code generated]
a-source-map (webpack x.x.x) compiled successfully in X ms
Expand All @@ -2662,22 +2675,24 @@ b-source-map:
sourceMap da629d4acf5998c06668-da629d.js.map 323 bytes [emitted] [dev] (auxiliary name: index)
asset 222c2acc68675174e6b2-222c2a.js 77 bytes [emitted] [immutable] [minimized] (name: a, b)
sourceMap 222c2acc68675174e6b2-222c2a.js.map 254 bytes [emitted] [dev] (auxiliary name: a, b)
assets by path *.jpg 11.8 KiB
asset 7382fad5b015914e0811.jpg 5.89 KiB [emitted] [immutable] [from: file.jpg] (auxiliary name: index)
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 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
orphan modules 61 bytes (javascript) 14.6 KiB (asset) [orphan] 2 modules
cacheable modules 469 bytes (javascript) 11.8 KiB (asset)
orphan modules 19 bytes [orphan] 1 module
cacheable modules 511 bytes (javascript) 26.3 KiB (asset)
javascript modules 385 bytes
./b/index.js 109 bytes [built] [code generated]
./b/a.js 22 bytes [built] [code generated]
./b/b.js 66 bytes [built] [code generated]
./b/lazy.js + 2 modules 188 bytes [built] [code generated]
asset modules 84 bytes (javascript) 11.8 KiB (asset)
asset modules 126 bytes (javascript) 26.3 KiB (asset)
./b/file.jpg 42 bytes (javascript) 5.89 KiB (asset) [built] [code generated]
./b/file.png 42 bytes (javascript) 14.6 KiB (asset) [built] [code generated]
./b/file.jpg?query 42 bytes (javascript) 5.89 KiB (asset) [built] [code generated]
b-source-map (webpack x.x.x) compiled successfully in X ms"
`;
Expand Down
1 change: 1 addition & 0 deletions test/statsCases/asset-concat/a.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a{}
1 change: 1 addition & 0 deletions test/statsCases/asset-concat/a.source.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const b = 1;
Binary file added test/statsCases/asset-concat/images/file.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/statsCases/asset-concat/images/file.png
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/statsCases/asset-concat/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/statsCases/asset-concat/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";
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 css from "./a.css";
import source from "./a.source";
1 change: 1 addition & 0 deletions test/statsCases/asset-concat/static/file.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div></div>
35 changes: 35 additions & 0 deletions test/statsCases/asset-concat/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/** @type {import("../../../").Configuration} */
module.exports = {
mode: "production",
entry: "./index.js",
module: {
rules: [
{
test: /\.(png|jpg|svg)$/,
type: "asset"
},
{
test: /\.html$/,
type: "asset/resource",
generator: {
filename: "static/[name][ext]"
}
},
{
test: /\.css$/,
type: "asset/inline"
},
{
test: /\.source\.js$/,
type: "asset/source"
},
{
mimetype: "text/plain",
type: "asset"
}
]
},
output: {
filename: "bundle.js"
}
};
19 changes: 6 additions & 13 deletions test/statsCases/asset/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** @type {import("../../../").Configuration} */
const base = {
module.exports = {
mode: "production",
entry: "./index.js",
module: {
Expand All @@ -17,29 +17,22 @@ const base = {
},
{
test: /\.css$/,
type: "asset/source"
type: "asset/inline"
},
{
test: /\.source\.js$/,
type: "asset/inline"
type: "asset/source"
},
{
mimetype: "text/plain",
type: "asset"
}
]
},
optimization: {
concatenateModules: false
},
output: {
filename: "bundle.js"
}
};

module.exports = [
{
...base,
optimization: {
concatenateModules: false
}
},
base
];
5 changes: 5 additions & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,11 @@ declare class ChunkGraph {
chunk: Chunk,
sourceType: string
): undefined | Iterable<Module>;
setChunkModuleSourceTypes(
chunk: Chunk,
module: Module,
sourceTypes: Set<string>
): void;
getOrderedChunkModulesIterable(
chunk: Chunk,
comparator: (arg0: Module, arg1: Module) => 0 | 1 | -1
Expand Down

0 comments on commit 1c0f5c7

Please sign in to comment.