Skip to content

Instantly share code, notes, and snippets.

@ScriptedAlchemy
Last active November 25, 2019 00:49
Show Gist options
  • Select an option

  • Save ScriptedAlchemy/60a77d203a423302ae66f46b6d7876e2 to your computer and use it in GitHub Desktop.

Select an option

Save ScriptedAlchemy/60a77d203a423302ae66f46b6d7876e2 to your computer and use it in GitHub Desktop.
Emitting a manifest map with dependencies
let files = compilation.chunks.reduce((files, chunk) => chunk.files.reduce((files, path) => {
let name = chunk.name ? chunk.name : null;
const dependencyChains = {};
if (name) {
name = `${name}.${this.getFileType(path)}`;
} else {
// For nameless chunks, just map the files directly.
name = path;
}
// check if the current chunk exists in externalModules, an object that is keeping track of keys
// when we were hashing modules
if (externalModules[chunk.id] || externalModules[chunk.name]) {
// TODO: swap forEachModle out with const of
// for(const module of chunk.modulesIterable){
chunk.forEachModule((module) => {
if (module.dependencies) {
// if the module has dependencies, loop over them
module.dependencies.forEach((dependency) => {
// check if theres anu modules attached to each dependecy, some can be consts from elsewhere in the file.
const dependencyModuleSet = dependency.getReference?.()?.module;
if (!dependencyModuleSet) return null;
// if this is the first dependency for this module, create an empty hashmap
if (!dependencyChains[chunk.id]) {
Object.assign(dependencyChains, { [chunk.id]: [] });
}
// determine the order the dependency is required in!!
const dependencyChainMap = dependencyChains[chunk.id][dependency.sourceOrder] = {};
// assign meta data to the dependency map
Object.assign(dependencyChainMap, {
order: dependency.sourceOrder,
name: dependencyModuleSet.rawRequest,
id: dependencyModuleSet.id,
sourceFiles: [],
});
// loop over the dependency set and locate the files a module exists within
for (const module of dependencyModuleSet.chunksIterable) {
if (module && module.files) {
if (dependencyChains[chunk.id]) {
// concat any source files that contain the dependency
dependencyChainMap.sourceFiles = dependencyChainMap?.sourceFiles?.concat?.(module.files) || null;
}
}
}
});
}
});
}
let currentDependencyChain = [];
// Webpack 4: .isOnlyInitial()
// Webpack 3: .isInitial()
// Webpack 1/2: .initial
// const modules = chunk.modulesIterable;
// let i = 0;
// while (i < modules.length) {
// getMeta(modules[i]);
// i++;
// }
return files.concat({
path,
chunk,
name,
dependencies: dependencyChains?.[chunk.id]?.removeNull?.().reverse?.(),
isInitial: chunk.isOnlyInitial ? chunk.isOnlyInitial() : (chunk.isInitial ? chunk.isInitial() : chunk.initial),
isChunk: true,
isAsset: false,
isModuleAsset: false,
});
}, files), []);
// module assets don't show up in assetsByChunkName.
// we're getting them this way;
files = stats.assets.reduce((files, asset) => {
const name = moduleAssets[asset.name];
if (name) {
return files.concat({
path: asset.name,
name,
isInitial: false,
isChunk: false,
isAsset: true,
isModuleAsset: true,
});
}
const isEntryAsset = asset.chunks.length > 0;
if (isEntryAsset) {
return files;
}
return files.concat({
path: asset.name,
name: asset.name,
isInitial: false,
isChunk: false,
isAsset: true,
isModuleAsset: false,
});
}, files);
files = files.filter((file) => {
// Don't add hot updates to manifest
const isUpdateChunk = file.path.includes('hot-update');
// Don't add manifest from another instance
const isManifest = emitCountMap.get(path.join(outputFolder, file.name)) !== undefined;
return !isUpdateChunk && !isManifest;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment