-
-
Notifications
You must be signed in to change notification settings - Fork 186
Expand file tree
/
Copy pathhooks.ts
More file actions
190 lines (156 loc) · 6.12 KB
/
hooks.ts
File metadata and controls
190 lines (156 loc) · 6.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { mkdirSync, writeFileSync } from 'node:fs';
import { basename, dirname, join } from 'node:path';
import { SyncWaterfallHook } from 'tapable';
import type { Compiler, Module, Compilation, LoaderContext } from 'webpack';
// Note: This was the old delcaration. It appears to be Webpack v3 compat.
// const { RawSource } = (webpack as any).sources || require('webpack-sources');
import { RawSource } from 'webpack-sources';
import type { EmitCountMap, InternalOptions } from './index.js';
import type { CompilationAsset, FileDescriptor } from './helpers.js';
import { generateManifest, reduceAssets, reduceChunk, transformFiles } from './helpers.js';
interface BeforeRunHookArgs {
emitCountMap: EmitCountMap;
manifestFileName: string;
}
interface EmitHookArgs {
compiler: Compiler;
emitCountMap: EmitCountMap;
manifestAssetId: string;
manifestFileName: string;
moduleAssets: Record<any, any>;
options: InternalOptions;
}
interface EmitCompilation {
emitAsset: Function;
}
const compilerHookMap = new WeakMap();
const getCompilerHooks = (compiler: Compiler) => {
let hooks = compilerHookMap.get(compiler);
if (typeof hooks === 'undefined') {
hooks = {
afterEmit: new SyncWaterfallHook(['manifest']),
beforeEmit: new SyncWaterfallHook(['manifest'])
};
compilerHookMap.set(compiler, hooks);
}
return hooks;
};
const beforeRunHook = (
{ emitCountMap, manifestFileName }: BeforeRunHookArgs,
_: Compiler,
callback: Function
) => {
const emitCount = emitCountMap.get(manifestFileName) || 0;
emitCountMap.set(manifestFileName, emitCount + 1);
/* istanbul ignore next */
if (callback) {
callback();
}
};
const emitHook = function emit(
{
compiler,
emitCountMap,
manifestAssetId,
manifestFileName,
moduleAssets,
options
}: EmitHookArgs,
compilation: Compilation
) {
const emitCount = emitCountMap.get(manifestFileName) - 1;
// Disable everything we don't use, add asset info, show cached assets
const stats = compilation.getStats().toJson({
all: false,
assets: true,
cachedAssets: true,
// Note: Webpack v5 compat
ids: true,
publicPath: true
});
// Webpack v5 supports `output.publicPath: 'auto'`, which resolves the publicPath
// at runtime based on the current script/asset location. In that mode, the
// string value 'auto' should not be serialized into the manifest. Treat it as
// an "unset" publicPath so manifest values remain unprefixed and consumers can
// resolve at runtime. See https://webpack.js.org/guides/public-path/#automatic-publicpath
const resolvedPublicPath = options.publicPath !== null ? options.publicPath : stats.publicPath;
const publicPath = resolvedPublicPath === 'auto' ? '' : resolvedPublicPath;
const { basePath, removeKeyHash } = options;
emitCountMap.set(manifestFileName, emitCount);
const auxiliaryFiles: Record<any, any> = {};
let files = Array.from(compilation.chunks).reduce<FileDescriptor[]>(
(prev: FileDescriptor[], chunk: any) => reduceChunk(prev, chunk, options, auxiliaryFiles),
[] as FileDescriptor[]
);
// module assets don't show up in assetsByChunkName, we're getting them this way
files = (stats.assets! as unknown as CompilationAsset[]).reduce(
(prev, asset) => reduceAssets(prev, asset, moduleAssets),
files
);
// don't add hot updates and don't add manifests from other instances
files = files.filter(
({ name, path }: { name: string; path: string }) =>
!path.includes('hot-update') &&
typeof emitCountMap.get(join(compiler.options.output?.path || '<unknown>', 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: FileDescriptor) => {
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: FileDescriptor) => {
const normalizePath = (path: string): string => {
if (!path.endsWith('/')) {
return `${path}/`;
}
return path;
};
const changes = {
// Append optional basepath onto all references. This allows output path to be reflected in the manifest.
name: basePath ? normalizePath(basePath) + file.name : file.name,
// Similar to basePath but only affects the value (e.g. how output.publicPath turns
// require('foo/bar') into '/public/foo/bar', see https://github.com/webpack/docs/wiki/configuration#outputpublicpath
path: publicPath ? normalizePath(publicPath) + file.path : file.path
};
// Fixes #210
changes.name = removeKeyHash ? changes.name.replace(removeKeyHash, '') : changes.name;
return Object.assign(file, changes);
});
files = transformFiles(files, options);
let manifest = generateManifest(compilation, files, options);
const isLastEmit = emitCount === 0;
manifest = getCompilerHooks(compiler).beforeEmit.call(manifest);
if (isLastEmit) {
const output = options.serialize(manifest);
(compilation as unknown as EmitCompilation).emitAsset(manifestAssetId, new RawSource(output));
if (options.writeToFileEmit) {
mkdirSync(dirname(manifestFileName), { recursive: true });
writeFileSync(manifestFileName, output);
}
}
getCompilerHooks(compiler).afterEmit.call(manifest);
};
interface LegacyModule extends Module {
userRequest?: any;
}
const normalModuleLoaderHook = (
{ moduleAssets }: { moduleAssets: Record<any, any> },
context: unknown,
module: LegacyModule
) => {
const loaderContext = context as LoaderContext<any>;
const { emitFile } = loaderContext;
loaderContext.emitFile = (file: string, content: string, sourceMap: any) => {
if (module.userRequest && !moduleAssets[file]) {
Object.assign(moduleAssets, { [file]: join(dirname(file), basename(module.userRequest)) });
}
return emitFile.call(module, file, content, sourceMap);
};
};
export { beforeRunHook, emitHook, getCompilerHooks, normalModuleLoaderHook };