Skip to content

Commit

Permalink
Merge pull request #11956 from webpack/feature/asset-processing
Browse files Browse the repository at this point in the history
allow to process assets that are added during processAssets
  • Loading branch information
sokra committed Nov 26, 2020
2 parents eb5481a + ca29e7c commit 2aacd90
Show file tree
Hide file tree
Showing 13 changed files with 531 additions and 56 deletions.
136 changes: 129 additions & 7 deletions lib/Compilation.js
Expand Up @@ -289,10 +289,122 @@ class Compilation {
*/
constructor(compiler) {
const getNormalModuleLoader = () => deprecatedNormalModuleLoaderHook(this);
/** @type {AsyncSeriesHook<[CompilationAssets]>} */
/** @typedef {{ additionalAssets?: true | Function }} ProcessAssetsAdditionalOptions */
/** @type {AsyncSeriesHook<[CompilationAssets], ProcessAssetsAdditionalOptions>} */
const processAssetsHook = new AsyncSeriesHook(["assets"]);

let savedAssets = new Set();
const popNewAssets = assets => {
let newAssets = undefined;
for (const file of Object.keys(assets)) {
if (savedAssets.has(file)) continue;
if (newAssets === undefined) {
newAssets = Object.create(null);
}
newAssets[file] = assets[file];
savedAssets.add(file);
}
return newAssets;
};
processAssetsHook.intercept({
name: "Compilation",
call: () => {
savedAssets.clear();
},
register: tap => {
const { type, name } = tap;
const { fn, additionalAssets, ...remainingTap } = tap;
const additionalAssetsFn =
additionalAssets === true ? fn : additionalAssets;
let processedAssets = undefined;
switch (type) {
case "sync":
if (additionalAssetsFn) {
this.hooks.processAdditionalAssets.tap(name, assets => {
if (processedAssets === this.assets) additionalAssetsFn(assets);
});
}
return {
...remainingTap,
type: "async",
fn: (assets, callback) => {
try {
fn(assets);
} catch (e) {
return callback(e);
}
processedAssets = this.assets;
const newAssets = popNewAssets(assets);
if (newAssets !== undefined) {
this.hooks.processAdditionalAssets.callAsync(
newAssets,
callback
);
return;
}
callback();
}
};
case "async":
if (additionalAssetsFn) {
this.hooks.processAdditionalAssets.tapAsync(
name,
(assets, callback) => {
if (processedAssets === this.assets)
return additionalAssetsFn(assets, callback);
callback();
}
);
}
return {
...remainingTap,
fn: (assets, callback) => {
fn(assets, err => {
if (err) return callback(err);
processedAssets = this.assets;
const newAssets = popNewAssets(assets);
if (newAssets !== undefined) {
this.hooks.processAdditionalAssets.callAsync(
newAssets,
callback
);
return;
}
callback();
});
}
};
case "promise":
if (additionalAssetsFn) {
this.hooks.processAdditionalAssets.tapPromise(name, assets => {
if (processedAssets === this.assets)
return additionalAssetsFn(assets);
return Promise.resolve();
});
}
return {
...remainingTap,
fn: assets => {
const p = fn(assets);
if (!p || !p.then) return p;
return p.then(() => {
processedAssets = this.assets;
const newAssets = popNewAssets(assets);
if (newAssets !== undefined) {
return this.hooks.processAdditionalAssets.promise(
newAssets
);
}
});
}
};
}
}
});

/** @type {SyncHook<[CompilationAssets]>} */
const afterProcessAssetsHook = new SyncHook(["assets"]);

/**
* @template T
* @param {string} name name of the hook
Expand Down Expand Up @@ -544,6 +656,8 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si

processAssets: processAssetsHook,
afterProcessAssets: afterProcessAssetsHook,
/** @type {AsyncSeriesHook<[CompilationAssets]>} */
processAdditionalAssets: new AsyncSeriesHook(["assets"]),

/** @type {SyncBailHook<[], boolean>} */
needAdditionalSeal: new SyncBailHook([]),
Expand Down Expand Up @@ -3439,6 +3553,8 @@ Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE = 100;

/**
* Optimize the count of existing assets, e. g. by merging them.
* Only assets of the same type should be merged.
* For assets of different types see PROCESS_ASSETS_STAGE_OPTIMIZE_INLINE.
*/
Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_COUNT = 200;

Expand All @@ -3453,16 +3569,22 @@ Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_COMPATIBILITY = 300;
Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE = 400;

/**
* Summarize the list of existing assets.
* When creating new assets from this they should be fully optimized.
* e. g. creating an assets manifest of Service Workers.
* Add development tooling to assets, e. g. by extracting a SourceMap.
*/
Compilation.PROCESS_ASSETS_STAGE_SUMMARIZE = 1000;
Compilation.PROCESS_ASSETS_STAGE_DEV_TOOLING = 500;

/**
* Add development tooling to assets, e. g. by extracting a SourceMap.
* Optimize the count of existing assets, e. g. by inlining assets of into other assets.
* Only assets of different types should be inlined.
* For assets of the same type see PROCESS_ASSETS_STAGE_OPTIMIZE_COUNT.
*/
Compilation.PROCESS_ASSETS_STAGE_DEV_TOOLING = 2000;
Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_INLINE = 700;

/**
* Summarize the list of existing assets
* e. g. creating an assets manifest of Service Workers.
*/
Compilation.PROCESS_ASSETS_STAGE_SUMMARIZE = 1000;

/**
* Optimize the hashes of the assets, e. g. by generating real hashes of the asset content.
Expand Down
4 changes: 2 additions & 2 deletions lib/Compiler.js
Expand Up @@ -152,9 +152,9 @@ class Compiler {
beforeCompile: new AsyncSeriesHook(["params"]),
/** @type {SyncHook<[CompilationParams]>} */
compile: new SyncHook(["params"]),
/** @type {AsyncParallelHook<[Compilation], Module>} */
/** @type {AsyncParallelHook<[Compilation]>} */
make: new AsyncParallelHook(["compilation"]),
/** @type {AsyncParallelHook<[Compilation], Module>} */
/** @type {AsyncParallelHook<[Compilation]>} */
finishMake: new AsyncSeriesHook(["compilation"]),
/** @type {AsyncSeriesHook<[Compilation]>} */
afterCompile: new AsyncSeriesHook(["compilation"]),
Expand Down
3 changes: 2 additions & 1 deletion lib/SourceMapDevToolPlugin.js
Expand Up @@ -162,7 +162,8 @@ class SourceMapDevToolPlugin {
compilation.hooks.processAssets.tapAsync(
{
name: "SourceMapDevToolPlugin",
stage: Compilation.PROCESS_ASSETS_STAGE_DEV_TOOLING
stage: Compilation.PROCESS_ASSETS_STAGE_DEV_TOOLING,
additionalAssets: true
},
(assets, callback) => {
const chunkGraph = compilation.chunkGraph;
Expand Down
3 changes: 3 additions & 0 deletions lib/index.js
Expand Up @@ -355,6 +355,9 @@ module.exports = mergeExports(fn, {
get ModuleConcatenationPlugin() {
return require("./optimize/ModuleConcatenationPlugin");
},
get RealContentHashPlugin() {
return require("./optimize/RealContentHashPlugin");
},
get RuntimeChunkPlugin() {
return require("./optimize/RuntimeChunkPlugin");
},
Expand Down

0 comments on commit 2aacd90

Please sign in to comment.