Skip to content

Commit 2552104

Browse files
committed
Porting over change with auxiliary files
1 parent 96dc431 commit 2552104

File tree

2 files changed

+43
-14
lines changed

2 files changed

+43
-14
lines changed

lib/webpack-manifest-plugin/helpers.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { dirname, join } = require('path');
1+
const { dirname, join, basename } = require('path');
22

33
const generateManifest = (compilation, files, { generate, seed = {} }) => {
44
let result;
@@ -62,8 +62,24 @@ const reduceAssets = (files, asset, moduleAssets, assetTypeModuleAssets) => {
6262
});
6363
};
6464

65-
const reduceChunk = (files, chunk, options) =>
66-
Array.from(chunk.files).reduce((prev, path) => {
65+
const reduceChunk = (files, chunk, options, auxiliaryFiles) => {
66+
// auxiliary files contain things like images, fonts AND, most
67+
// importantly, other files like .map sourcemap files
68+
// we modify the auxiliaryFiles so that we can add any of these
69+
// to the manifest that was not added by another method
70+
// (sourcemaps files are not added via any other method)
71+
Array.from(chunk.auxiliaryFiles || []).forEach((auxiliaryFile) => {
72+
auxiliaryFiles[auxiliaryFile] = {
73+
path: auxiliaryFile,
74+
name: basename(auxiliaryFile),
75+
isInitial: false,
76+
isChunk: false,
77+
isAsset: true,
78+
isModuleAsset: true
79+
};
80+
});
81+
82+
return Array.from(chunk.files).reduce((prev, path) => {
6783
let name = chunk.name ? chunk.name : null;
6884
// chunk name, or for nameless chunks, just map the files directly.
6985
name = name
@@ -82,6 +98,7 @@ const reduceChunk = (files, chunk, options) =>
8298
isModuleAsset: false
8399
});
84100
}, files);
101+
};
85102

86103
const standardizeFilePaths = (file) => {
87104
const result = Object.assign({}, file);

lib/webpack-manifest-plugin/hooks.js

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ const emitHook = function emit(
5151

5252
emitCountMap.set(manifestFileName, emitCount);
5353

54+
const auxiliaryFiles = {};
5455
let files = Array.from(compilation.chunks).reduce(
55-
(prev, chunk) => reduceChunk(prev, chunk, options),
56+
(prev, chunk) => reduceChunk(prev, chunk, options, auxiliaryFiles),
5657
[]
5758
);
5859

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

70+
// auxiliary files are "extra" files that are probably already included
71+
// in other ways. Loop over files and remove any from auxiliaryFiles
72+
files.forEach((file) => {
73+
delete auxiliaryFiles[file.path];
74+
});
75+
// if there are any auxiliaryFiles left, add them to the files
76+
// this handles, specifically, sourcemaps
77+
Object.keys(auxiliaryFiles).forEach((auxiliaryFile) => {
78+
files = files.concat(auxiliaryFiles[auxiliaryFile]);
79+
});
80+
6981
files = files.map((file) => {
7082
const changes = {
7183
// Append optional basepath onto all references. This allows output path to be reflected in the manifest.
@@ -119,16 +131,16 @@ const normalModuleLoaderHook = ({ moduleAssets, assetTypeModuleAssets }, loaderC
119131
// the "emitFile" callback is never called on asset modules
120132
// so, we create a different map that can be used later in the "emit" hook
121133
if (['asset', 'asset/inline', 'asset/resource', 'asset/source'].includes(module.type)) {
122-
// This takes the userRequest (which is an absolute path) and turns it into
123-
// a relative path to the root context. This is done so that the string
124-
// will match asset.info.sourceFilename in the emit hook.
125-
let sourceFilename = relative(loaderContext.rootContext, module.userRequest);
126-
// at this point, Windows paths use \ in their paths
127-
// but in the emit hook, asset.info.sourceFilename fill have UNIX slashes
128-
sourceFilename = sourceFilename.replace(/\\/g, '/');
129-
Object.assign(assetTypeModuleAssets, {
130-
[sourceFilename]: basename(module.userRequest),
131-
});
134+
// This takes the userRequest (which is an absolute path) and turns it into
135+
// a relative path to the root context. This is done so that the string
136+
// will match asset.info.sourceFilename in the emit hook.
137+
let sourceFilename = relative(loaderContext.rootContext, module.userRequest);
138+
// at this point, Windows paths use \ in their paths
139+
// but in the emit hook, asset.info.sourceFilename fill have UNIX slashes
140+
sourceFilename = sourceFilename.replace(/\\/g, '/');
141+
Object.assign(assetTypeModuleAssets, {
142+
[sourceFilename]: basename(module.userRequest)
143+
});
132144
}
133145

134146
// eslint-disable-next-line no-param-reassign

0 commit comments

Comments
 (0)