-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
170 lines (154 loc) · 5.34 KB
/
index.js
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
const { getFilenameFromUrl, handleRequest } = require('webpack-dev-middleware/lib/util');
class LazyBuild {
constructor() {
this.requestedFiles = new Set();
this.neededModules = new Set();
}
get plugin() {
if (!this._plugin) {
console.warn('LazyBuild#plugin is deprecated. Please use #createPlugin() instead');
this._plugin = new WebpackLazyDevBuildPlugin(this);
}
return this._plugin;
}
createPlugin() {
return new WebpackLazyDevBuildPlugin(this);
}
createMiddleware(devMiddleware) {
return (req, res, next) => {
if (req.method !== 'GET') {
return next();
}
const { context } = devMiddleware;
const reqFilename = getFilenameFromUrl(context.options.publicPath, context.compiler, req.url);
if (reqFilename === false) {
return next();
}
const processFile = (filename) => {
if (context.webpackStats && !this.requestedFiles.has(filename)) {
this.requestedFiles.add(filename);
const stats = context.webpackStats.stats || [context.webpackStats];
const modifiedStats = stats.find(({ compilation }) => {
let { outputPath } = compilation.compiler;
if (!outputPath.endsWith('/')) outputPath += '/';
if (!filename.startsWith(outputPath)) return;
const chunkFilename = filename.slice(outputPath.length);
const filteredChunks = compilation.chunks.filter((chunk) => {
return chunk.files.includes(chunkFilename);
});
if (!filteredChunks.length) return;
filteredChunks.forEach((chunk) => {
for (const module of chunk.modulesIterable) {
this.neededModules.add(module.resource || module.debugId);
}
});
return true;
});
if (modifiedStats) {
this._recompile(context, modifiedStats.compilation.compiler);
}
}
};
const assetUrl = this.getAssetUrl(req.url);
if (assetUrl && assetUrl !== req.url) {
const assetFilename = getFilenameFromUrl(context.options.publicPath, context.compiler, assetUrl);
if (assetFilename !== false) {
const assetReq = Object.assign({}, req);
assetReq.url = assetUrl;
processFile(assetFilename);
return handleRequest(context, assetFilename, () => {
this.requestedFiles.add(reqFilename);
return devMiddleware(req, res, next);
}, assetReq);
}
}
processFile(reqFilename);
return devMiddleware(req, res, next);
}
}
_recompile(context, compiler) {
context.state = false;
const watchings = context.watching.watchings || [context.watching];
const watching = watchings.find(w => w.compiler === compiler);
watching.pausedWatcher = watching.watcher;
watching.watcher = null;
let timestamps = compiler.watchFileSystem.watcher.getTimes();
if (compiler.hooks) {
timestamps = objectToMap(timestamps);
}
compiler.fileTimestamps = timestamps;
compiler.contextTimestamps = timestamps;
watching.invalidate();
}
getAssetUrl(url) {
if (url.endsWith('.css')) {
url = url.slice(0, -4) + '.js';
}
return url;
}
}
class WebpackLazyDevBuildPlugin {
constructor(lazyBuild) {
this.lazyBuild = lazyBuild;
}
apply(compiler) {
const hasHooks = Boolean(compiler.hooks);
const compilationCallback = (compilation) => {
const buildModuleCallback = (module) => {
if (this.lazyBuild.neededModules.has(module.resource || module.debugId)) return;
const isLazy = module.reasons.every((reason) => {
const { type } = reason.dependency;
return type === 'import()' || type === 'single entry';
});
if (isLazy) {
if (hasHooks) {
const buildCopy = module.build;
module.build = () => {
module.buildMeta = {};
module.buildInfo = {
builtTime: Date.now(),
contextDependencies: module._contextDependencies,
};
};
setTimeout(() => {
const callbackList = compilation._buildingModules.get(module);
compilation._buildingModules.delete(module);
for (const cb of callbackList) cb();
module.build = buildCopy;
});
} else {
module.building = [];
setTimeout(() => {
const { building } = module;
module.building = undefined;
building.forEach(cb => cb());
});
}
} else {
this.lazyBuild.neededModules.add(module.resource || module.debugId);
}
};
if (hasHooks) {
compilation.hooks.buildModule.tap('WebpackLazyDevBuildPlugin', buildModuleCallback);
} else {
compilation.plugin('build-module', buildModuleCallback);
}
};
if (hasHooks) {
compiler.hooks.compilation.tap('WebpackLazyDevBuildPlugin', compilationCallback);
} else {
compiler.plugin('compilation', compilationCallback);
}
}
}
module.exports = LazyBuild;
/** Copied from webpack/lib/util/objectToMap.js (v4.6.0) */
function objectToMap(obj) {
return new Map(
Object.keys(obj).map(key => {
/** @type {[string, string]} */
const pair = [key, obj[key]];
return pair;
})
);
};