| 1 | "use strict";
|
|---|
| 2 | Object.defineProperty(exports, "__esModule", { value: true });
|
|---|
| 3 | exports.normalModuleLoaderHook = exports.getCompilerHooks = exports.emitHook = exports.beforeRunHook = void 0;
|
|---|
| 4 | const fs_1 = require("fs");
|
|---|
| 5 | const path_1 = require("path");
|
|---|
| 6 | const tapable_1 = require("tapable");
|
|---|
| 7 | const webpack_sources_1 = require("webpack-sources");
|
|---|
| 8 | const helpers_1 = require("./helpers");
|
|---|
| 9 | const compilerHookMap = new WeakMap();
|
|---|
| 10 | const getCompilerHooks = (compiler) => {
|
|---|
| 11 | let hooks = compilerHookMap.get(compiler);
|
|---|
| 12 | if (typeof hooks === 'undefined') {
|
|---|
| 13 | hooks = {
|
|---|
| 14 | afterEmit: new tapable_1.SyncWaterfallHook(['manifest']),
|
|---|
| 15 | beforeEmit: new tapable_1.SyncWaterfallHook(['manifest'])
|
|---|
| 16 | };
|
|---|
| 17 | compilerHookMap.set(compiler, hooks);
|
|---|
| 18 | }
|
|---|
| 19 | return hooks;
|
|---|
| 20 | };
|
|---|
| 21 | exports.getCompilerHooks = getCompilerHooks;
|
|---|
| 22 | const beforeRunHook = ({ emitCountMap, manifestFileName }, _, callback) => {
|
|---|
| 23 | const emitCount = emitCountMap.get(manifestFileName) || 0;
|
|---|
| 24 | emitCountMap.set(manifestFileName, emitCount + 1);
|
|---|
| 25 | if (callback) {
|
|---|
| 26 | callback();
|
|---|
| 27 | }
|
|---|
| 28 | };
|
|---|
| 29 | exports.beforeRunHook = beforeRunHook;
|
|---|
| 30 | const emitHook = function emit({ compiler, emitCountMap, manifestAssetId, manifestFileName, moduleAssets, options }, compilation) {
|
|---|
| 31 | const emitCount = emitCountMap.get(manifestFileName) - 1;
|
|---|
| 32 | const stats = compilation.getStats().toJson({
|
|---|
| 33 | all: false,
|
|---|
| 34 | assets: true,
|
|---|
| 35 | cachedAssets: true,
|
|---|
| 36 | ids: true,
|
|---|
| 37 | publicPath: true
|
|---|
| 38 | });
|
|---|
| 39 | const publicPath = options.publicPath !== null ? options.publicPath : stats.publicPath;
|
|---|
| 40 | const { basePath, removeKeyHash } = options;
|
|---|
| 41 | emitCountMap.set(manifestFileName, emitCount);
|
|---|
| 42 | const auxiliaryFiles = {};
|
|---|
| 43 | let files = Array.from(compilation.chunks).reduce((prev, chunk) => helpers_1.reduceChunk(prev, chunk, options, auxiliaryFiles), []);
|
|---|
| 44 | files = stats.assets.reduce((prev, asset) => helpers_1.reduceAssets(prev, asset, moduleAssets), files);
|
|---|
| 45 | files = files.filter(({ name, path }) => {
|
|---|
| 46 | var _a;
|
|---|
| 47 | return !path.includes('hot-update') &&
|
|---|
| 48 | typeof emitCountMap.get(path_1.join(((_a = compiler.options.output) === null || _a === void 0 ? void 0 : _a.path) || '<unknown>', name)) ===
|
|---|
| 49 | 'undefined';
|
|---|
| 50 | });
|
|---|
| 51 | files.forEach((file) => {
|
|---|
| 52 | delete auxiliaryFiles[file.path];
|
|---|
| 53 | });
|
|---|
| 54 | Object.keys(auxiliaryFiles).forEach((auxiliaryFile) => {
|
|---|
| 55 | files = files.concat(auxiliaryFiles[auxiliaryFile]);
|
|---|
| 56 | });
|
|---|
| 57 | files = files.map((file) => {
|
|---|
| 58 | const normalizePath = (path) => {
|
|---|
| 59 | if (!path.endsWith('/')) {
|
|---|
| 60 | return `${path}/`;
|
|---|
| 61 | }
|
|---|
| 62 | return path;
|
|---|
| 63 | };
|
|---|
| 64 | const changes = {
|
|---|
| 65 | name: basePath ? normalizePath(basePath) + file.name : file.name,
|
|---|
| 66 | path: publicPath ? normalizePath(publicPath) + file.path : file.path
|
|---|
| 67 | };
|
|---|
| 68 | changes.name = removeKeyHash ? changes.name.replace(removeKeyHash, '') : changes.name;
|
|---|
| 69 | return Object.assign(file, changes);
|
|---|
| 70 | });
|
|---|
| 71 | files = helpers_1.transformFiles(files, options);
|
|---|
| 72 | let manifest = helpers_1.generateManifest(compilation, files, options);
|
|---|
| 73 | const isLastEmit = emitCount === 0;
|
|---|
| 74 | manifest = getCompilerHooks(compiler).beforeEmit.call(manifest);
|
|---|
| 75 | if (isLastEmit) {
|
|---|
| 76 | const output = options.serialize(manifest);
|
|---|
| 77 | compilation.emitAsset(manifestAssetId, new webpack_sources_1.RawSource(output));
|
|---|
| 78 | if (options.writeToFileEmit) {
|
|---|
| 79 | fs_1.mkdirSync(path_1.dirname(manifestFileName), { recursive: true });
|
|---|
| 80 | fs_1.writeFileSync(manifestFileName, output);
|
|---|
| 81 | }
|
|---|
| 82 | }
|
|---|
| 83 | getCompilerHooks(compiler).afterEmit.call(manifest);
|
|---|
| 84 | };
|
|---|
| 85 | exports.emitHook = emitHook;
|
|---|
| 86 | const normalModuleLoaderHook = ({ moduleAssets }, loaderContext, module) => {
|
|---|
| 87 | const { emitFile } = loaderContext;
|
|---|
| 88 | loaderContext.emitFile = (file, content, sourceMap) => {
|
|---|
| 89 | if (module.userRequest && !moduleAssets[file]) {
|
|---|
| 90 | Object.assign(moduleAssets, { [file]: path_1.join(path_1.dirname(file), path_1.basename(module.userRequest)) });
|
|---|
| 91 | }
|
|---|
| 92 | return emitFile.call(module, file, content, sourceMap);
|
|---|
| 93 | };
|
|---|
| 94 | };
|
|---|
| 95 | exports.normalModuleLoaderHook = normalModuleLoaderHook;
|
|---|
| 96 | //# sourceMappingURL=hooks.js.map |
|---|