From 710b36437237c80fff0b16fc69124c8aa296119a Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Mon, 8 Feb 2021 09:35:26 -0500 Subject: [PATCH] embedding fix: https://github.com/shellscape/webpack-manifest-plugin/pull/249 --- lib/webpack-manifest-plugin/helpers.js | 53 +++++++++++++++----------- lib/webpack-manifest-plugin/hooks.js | 18 +++++++-- lib/webpack-manifest-plugin/index.js | 4 +- 3 files changed, 48 insertions(+), 27 deletions(-) diff --git a/lib/webpack-manifest-plugin/helpers.js b/lib/webpack-manifest-plugin/helpers.js index aa12d19f..3309d705 100644 --- a/lib/webpack-manifest-plugin/helpers.js +++ b/lib/webpack-manifest-plugin/helpers.js @@ -1,3 +1,5 @@ +const { dirname, join } = require('path'); + const generateManifest = (compilation, files, { generate, seed = {} }) => { let result; if (generate) { @@ -24,8 +26,16 @@ const getFileType = (fileName, { transformExtensions }) => { return transformExtensions.test(extension) ? `${split.pop()}.${extension}` : extension; }; -const reduceAssets = (files, asset, moduleAssets) => { - const name = moduleAssets[asset.name] ? moduleAssets[asset.name] : asset.info.sourceFilename; +const reduceAssets = (files, asset, moduleAssets, assetTypeModuleAssets) => { + let name; + if (moduleAssets[asset.name]) { + name = moduleAssets[asset.name]; + } else if (assetTypeModuleAssets[asset.info.sourceFilename]) { + name = join(dirname(asset.name), assetTypeModuleAssets[asset.info.sourceFilename]); + } else { + name = asset.info.sourceFilename; + } + if (name) { return files.concat({ path: asset.name, @@ -53,28 +63,25 @@ const reduceAssets = (files, asset, moduleAssets) => { }; const reduceChunk = (files, chunk, options) => - Array.of(...Array.from(chunk.files), ...Array.from(chunk.auxiliaryFiles || [])).reduce( - (prev, path) => { - let name = chunk.name ? chunk.name : null; - // chunk name, or for nameless chunks, just map the files directly. - name = name - ? options.useEntryKeys && !path.endsWith('.map') - ? name - : `${name}.${getFileType(path, options)}` - : path; + 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 + ? options.useEntryKeys && !path.endsWith('.map') + ? name + : `${name}.${getFileType(path, options)}` + : path; - return prev.concat({ - path, - chunk, - name, - isInitial: chunk.isOnlyInitial(), - isChunk: true, - isAsset: false, - isModuleAsset: false - }); - }, - files - ); + return prev.concat({ + path, + chunk, + name, + isInitial: chunk.isOnlyInitial(), + isChunk: true, + isAsset: false, + isModuleAsset: false + }); + }, files); const standardizeFilePaths = (file) => { const result = Object.assign({}, file); diff --git a/lib/webpack-manifest-plugin/hooks.js b/lib/webpack-manifest-plugin/hooks.js index 3e32bf35..05da9c91 100644 --- a/lib/webpack-manifest-plugin/hooks.js +++ b/lib/webpack-manifest-plugin/hooks.js @@ -33,7 +33,7 @@ const beforeRunHook = ({ emitCountMap, manifestFileName }, compiler, callback) = }; const emitHook = function emit( - { compiler, emitCountMap, manifestAssetId, manifestFileName, moduleAssets, options }, + { compiler, emitCountMap, manifestAssetId, manifestFileName, moduleAssets, assetTypeModuleAssets, options }, compilation ) { const emitCount = emitCountMap.get(manifestFileName) - 1; @@ -57,7 +57,7 @@ const emitHook = function emit( ); // module assets don't show up in assetsByChunkName, we're getting them this way - files = stats.assets.reduce((prev, asset) => reduceAssets(prev, asset, moduleAssets), files); + files = stats.assets.reduce((prev, asset) => reduceAssets(prev, asset, moduleAssets, assetTypeModuleAssets), files); // don't add hot updates and don't add manifests from other instances files = files.filter( @@ -113,9 +113,21 @@ const emitHook = function emit( getCompilerHooks(compiler).afterEmit.call(manifest); }; -const normalModuleLoaderHook = ({ moduleAssets }, loaderContext, module) => { +const normalModuleLoaderHook = ({ moduleAssets, assetTypeModuleAssets }, loaderContext, module) => { const { emitFile } = loaderContext; + // 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)) { + Object.assign(assetTypeModuleAssets, { + // this takes the userRequest (which is an absolute path) and removes the rootContext + // this is done so that the string will match asset.info.sourceFilename in the emit hook + [module.userRequest.replace(loaderContext.rootContext, '').replace(/^(\/|\\)/, '')]: basename( + module.userRequest + ) + }); + } + // eslint-disable-next-line no-param-reassign loaderContext.emitFile = (file, content, sourceMap) => { if (module.userRequest && !moduleAssets[file]) { diff --git a/lib/webpack-manifest-plugin/index.js b/lib/webpack-manifest-plugin/index.js index 189266b1..4c7a0dd3 100644 --- a/lib/webpack-manifest-plugin/index.js +++ b/lib/webpack-manifest-plugin/index.js @@ -33,6 +33,7 @@ class WebpackManifestPlugin { apply(compiler) { const moduleAssets = {}; + const assetTypeModuleAssets = {}; const manifestFileName = resolve(compiler.options.output.path, this.options.fileName); const manifestAssetId = relative(compiler.options.output.path, manifestFileName); const beforeRun = beforeRunHook.bind(this, { emitCountMap, manifestFileName }); @@ -42,9 +43,10 @@ class WebpackManifestPlugin { manifestAssetId, manifestFileName, moduleAssets, + assetTypeModuleAssets, options: this.options }); - const normalModuleLoader = normalModuleLoaderHook.bind(this, { moduleAssets }); + const normalModuleLoader = normalModuleLoaderHook.bind(this, { moduleAssets, assetTypeModuleAssets }); const hookOptions = { name: 'WebpackManifestPlugin', stage: Infinity