|
| 1 | +const { mkdirSync, writeFileSync } = require('fs'); |
| 2 | +const { basename, dirname, join } = require('path'); |
| 3 | + |
| 4 | +const { SyncWaterfallHook } = require('tapable'); |
| 5 | +const webpack = require('webpack'); |
| 6 | +// eslint-disable-next-line global-require |
| 7 | +const { RawSource } = webpack.sources || require('webpack-sources'); |
| 8 | + |
| 9 | +const { generateManifest, reduceAssets, reduceChunk, transformFiles } = require('./helpers'); |
| 10 | + |
| 11 | +const compilerHookMap = new WeakMap(); |
| 12 | + |
| 13 | +const getCompilerHooks = (compiler) => { |
| 14 | + let hooks = compilerHookMap.get(compiler); |
| 15 | + if (typeof hooks === 'undefined') { |
| 16 | + hooks = { |
| 17 | + afterEmit: new SyncWaterfallHook(['manifest']), |
| 18 | + beforeEmit: new SyncWaterfallHook(['manifest']) |
| 19 | + }; |
| 20 | + compilerHookMap.set(compiler, hooks); |
| 21 | + } |
| 22 | + return hooks; |
| 23 | +}; |
| 24 | + |
| 25 | +const beforeRunHook = ({ emitCountMap, manifestFileName }, compiler, callback) => { |
| 26 | + const emitCount = emitCountMap.get(manifestFileName) || 0; |
| 27 | + emitCountMap.set(manifestFileName, emitCount + 1); |
| 28 | + |
| 29 | + /* istanbul ignore next */ |
| 30 | + if (callback) { |
| 31 | + callback(); |
| 32 | + } |
| 33 | +}; |
| 34 | + |
| 35 | +const emitHook = function emit( |
| 36 | + { compiler, emitCountMap, manifestAssetId, manifestFileName, moduleAssets, options }, |
| 37 | + compilation |
| 38 | +) { |
| 39 | + const emitCount = emitCountMap.get(manifestFileName) - 1; |
| 40 | + // Disable everything we don't use, add asset info, show cached assets |
| 41 | + const stats = compilation.getStats().toJson({ |
| 42 | + all: false, |
| 43 | + assets: true, |
| 44 | + cachedAssets: true, |
| 45 | + ids: true, |
| 46 | + publicPath: true |
| 47 | + }); |
| 48 | + |
| 49 | + const publicPath = options.publicPath !== null ? options.publicPath : stats.publicPath; |
| 50 | + const { basePath, removeKeyHash } = options; |
| 51 | + |
| 52 | + emitCountMap.set(manifestFileName, emitCount); |
| 53 | + |
| 54 | + const auxiliaryFiles = {}; |
| 55 | + let files = Array.from(compilation.chunks).reduce( |
| 56 | + (prev, chunk) => reduceChunk(prev, chunk, options, auxiliaryFiles), |
| 57 | + [] |
| 58 | + ); |
| 59 | + |
| 60 | + // module assets don't show up in assetsByChunkName, we're getting them this way |
| 61 | + files = stats.assets.reduce((prev, asset) => reduceAssets(prev, asset, moduleAssets), files); |
| 62 | + |
| 63 | + // don't add hot updates and don't add manifests from other instances |
| 64 | + files = files.filter( |
| 65 | + ({ name, path }) => |
| 66 | + !path.includes('hot-update') && |
| 67 | + typeof emitCountMap.get(join(compiler.options.output.path, name)) === 'undefined' |
| 68 | + ); |
| 69 | + |
| 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 | + |
| 81 | + files = files.map((file) => { |
| 82 | + const changes = { |
| 83 | + // Append optional basepath onto all references. This allows output path to be reflected in the manifest. |
| 84 | + name: basePath ? basePath + file.name : file.name, |
| 85 | + // Similar to basePath but only affects the value (e.g. how output.publicPath turns |
| 86 | + // require('foo/bar') into '/public/foo/bar', see https://github.com/webpack/docs/wiki/configuration#outputpublicpath |
| 87 | + path: publicPath ? publicPath + file.path : file.path |
| 88 | + }; |
| 89 | + |
| 90 | + // Fixes #210 |
| 91 | + changes.name = removeKeyHash ? changes.name.replace(removeKeyHash, '') : changes.name; |
| 92 | + |
| 93 | + return Object.assign(file, changes); |
| 94 | + }); |
| 95 | + |
| 96 | + files = transformFiles(files, options); |
| 97 | + |
| 98 | + let manifest = generateManifest(compilation, files, options); |
| 99 | + const isLastEmit = emitCount === 0; |
| 100 | + |
| 101 | + manifest = getCompilerHooks(compiler).beforeEmit.call(manifest); |
| 102 | + |
| 103 | + if (isLastEmit) { |
| 104 | + const output = options.serialize(manifest); |
| 105 | + // |
| 106 | + // Object.assign(compilation.assets, { |
| 107 | + // [manifestAssetId]: { |
| 108 | + // source() { |
| 109 | + // return output; |
| 110 | + // }, |
| 111 | + // size() { |
| 112 | + // return output.length; |
| 113 | + // } |
| 114 | + // } |
| 115 | + // }); |
| 116 | + // |
| 117 | + compilation.emitAsset(manifestAssetId, new RawSource(output)); |
| 118 | + |
| 119 | + if (options.writeToFileEmit) { |
| 120 | + mkdirSync(dirname(manifestFileName), { recursive: true }); |
| 121 | + writeFileSync(manifestFileName, output); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + getCompilerHooks(compiler).afterEmit.call(manifest); |
| 126 | +}; |
| 127 | + |
| 128 | +const normalModuleLoaderHook = ({ moduleAssets }, loaderContext, module) => { |
| 129 | + const { emitFile } = loaderContext; |
| 130 | + |
| 131 | + // eslint-disable-next-line no-param-reassign |
| 132 | + loaderContext.emitFile = (file, content, sourceMap) => { |
| 133 | + if (module.userRequest && !moduleAssets[file]) { |
| 134 | + Object.assign(moduleAssets, { [file]: join(dirname(file), basename(module.userRequest)) }); |
| 135 | + } |
| 136 | + |
| 137 | + return emitFile.call(module, file, content, sourceMap); |
| 138 | + }; |
| 139 | +}; |
| 140 | + |
| 141 | +module.exports = { beforeRunHook, emitHook, getCompilerHooks, normalModuleLoaderHook }; |
0 commit comments