Skip to content

Commit

Permalink
Porting over change with auxiliary files
Browse files Browse the repository at this point in the history
  • Loading branch information
weaverryan committed Feb 9, 2021
1 parent 96dc431 commit 2552104
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
23 changes: 20 additions & 3 deletions lib/webpack-manifest-plugin/helpers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { dirname, join } = require('path');
const { dirname, join, basename } = require('path');

const generateManifest = (compilation, files, { generate, seed = {} }) => {
let result;
Expand Down Expand Up @@ -62,8 +62,24 @@ const reduceAssets = (files, asset, moduleAssets, assetTypeModuleAssets) => {
});
};

const reduceChunk = (files, chunk, options) =>
Array.from(chunk.files).reduce((prev, path) => {
const reduceChunk = (files, chunk, options, auxiliaryFiles) => {
// auxiliary files contain things like images, fonts AND, most
// importantly, other files like .map sourcemap files
// we modify the auxiliaryFiles so that we can add any of these
// to the manifest that was not added by another method
// (sourcemaps files are not added via any other method)
Array.from(chunk.auxiliaryFiles || []).forEach((auxiliaryFile) => {
auxiliaryFiles[auxiliaryFile] = {
path: auxiliaryFile,
name: basename(auxiliaryFile),
isInitial: false,
isChunk: false,
isAsset: true,
isModuleAsset: true
};
});

return Array.from(chunk.files).reduce((prev, path) => {
let name = chunk.name ? chunk.name : null;
// chunk name, or for nameless chunks, just map the files directly.
name = name
Expand All @@ -82,6 +98,7 @@ const reduceChunk = (files, chunk, options) =>
isModuleAsset: false
});
}, files);
};

const standardizeFilePaths = (file) => {
const result = Object.assign({}, file);
Expand Down
34 changes: 23 additions & 11 deletions lib/webpack-manifest-plugin/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ const emitHook = function emit(

emitCountMap.set(manifestFileName, emitCount);

const auxiliaryFiles = {};
let files = Array.from(compilation.chunks).reduce(
(prev, chunk) => reduceChunk(prev, chunk, options),
(prev, chunk) => reduceChunk(prev, chunk, options, auxiliaryFiles),
[]
);

Expand All @@ -66,6 +67,17 @@ const emitHook = function emit(
typeof emitCountMap.get(join(compiler.options.output.path, name)) === 'undefined'
);

// auxiliary files are "extra" files that are probably already included
// in other ways. Loop over files and remove any from auxiliaryFiles
files.forEach((file) => {
delete auxiliaryFiles[file.path];
});
// if there are any auxiliaryFiles left, add them to the files
// this handles, specifically, sourcemaps
Object.keys(auxiliaryFiles).forEach((auxiliaryFile) => {
files = files.concat(auxiliaryFiles[auxiliaryFile]);
});

files = files.map((file) => {
const changes = {
// Append optional basepath onto all references. This allows output path to be reflected in the manifest.
Expand Down Expand Up @@ -119,16 +131,16 @@ const normalModuleLoaderHook = ({ moduleAssets, assetTypeModuleAssets }, loaderC
// the "emitFile" callback is never called on asset modules
// so, we create a different map that can be used later in the "emit" hook
if (['asset', 'asset/inline', 'asset/resource', 'asset/source'].includes(module.type)) {
// This takes the userRequest (which is an absolute path) and turns it into
// a relative path to the root context. This is done so that the string
// will match asset.info.sourceFilename in the emit hook.
let sourceFilename = relative(loaderContext.rootContext, module.userRequest);
// at this point, Windows paths use \ in their paths
// but in the emit hook, asset.info.sourceFilename fill have UNIX slashes
sourceFilename = sourceFilename.replace(/\\/g, '/');
Object.assign(assetTypeModuleAssets, {
[sourceFilename]: basename(module.userRequest),
});
// This takes the userRequest (which is an absolute path) and turns it into
// a relative path to the root context. This is done so that the string
// will match asset.info.sourceFilename in the emit hook.
let sourceFilename = relative(loaderContext.rootContext, module.userRequest);
// at this point, Windows paths use \ in their paths
// but in the emit hook, asset.info.sourceFilename fill have UNIX slashes
sourceFilename = sourceFilename.replace(/\\/g, '/');
Object.assign(assetTypeModuleAssets, {
[sourceFilename]: basename(module.userRequest)
});
}

// eslint-disable-next-line no-param-reassign
Expand Down

0 comments on commit 2552104

Please sign in to comment.