-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathplugin.js
123 lines (101 loc) · 3.42 KB
/
plugin.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
import webpack, { DllReferencePlugin } from 'webpack';
import flatMap from 'lodash/flatMap';
import isEmpty from 'lodash/isEmpty';
import { RawSource } from 'webpack-sources';
import path from 'path';
import { cacheDir, getManifestPath } from './paths';
import { concat, merge, keys } from './utils/index.js';
import createCompileIfNeeded from './createCompileIfNeeded';
import createConfig from './createConfig';
import createMemory from './createMemory';
import createSettings from './createSettings';
import getInstanceIndex from './getInstanceIndex';
import createHandleStats from './createHandleStats';
import createLogger from './createLogger';
class AutoDLLPlugin {
constructor(settings) {
this._originalSettings = settings;
}
apply(compiler) {
const settings = createSettings({
originalSettings: this._originalSettings,
index: getInstanceIndex(compiler.options.plugins, this),
parentConfig: compiler.options
});
const log = createLogger(settings.debug);
const dllConfig = createConfig(settings, compiler.options);
const compileIfNeeded = createCompileIfNeeded(log, settings);
const memory = createMemory();
const handleStats = createHandleStats(log, settings.hash, memory);
if (isEmpty(dllConfig.entry)) {
// there's nothing to do.
return;
}
const { context, inject } = settings;
keys(dllConfig.entry)
.map(getManifestPath(settings.hash))
.forEach(manifestPath => {
new DllReferencePlugin({
context: context,
manifest: manifestPath
}).apply(compiler);
});
compiler.plugin('before-compile', (params, callback) => {
params.compilationDependencies = params.compilationDependencies.filter(
path => !path.startsWith(cacheDir)
);
callback();
});
compiler.plugin(['run', 'watch-run'], (_compiler, callback) => {
compileIfNeeded(() => webpack(dllConfig))
.then((a) => {
return a;
})
.then(handleStats)
.then(({ source, stats }) => {
compiler.applyPlugins('autodll-stats-retrieved', stats, source);
if (source === 'memory') return;
return memory.sync(settings.hash, stats);
})
.then(() => callback())
.catch(console.error);
});
compiler.plugin('emit', (compilation, callback) => {
const dllAssets = memory
.getAssets()
.reduce((assets, { filename, buffer }) => {
const assetPath = path.join(settings.path, filename);
return {
...assets,
[assetPath]: new RawSource(buffer)
};
}, {});
compilation.assets = merge(
compilation.assets,
dllAssets
);
callback();
});
if (inject) {
compiler.plugin('compilation', compilation => {
compilation.plugin(
'html-webpack-plugin-before-html-generation',
(htmlPluginData, callback) => {
const dllEntriesPaths = flatMap(
memory.getStats().entrypoints,
'assets'
).map((filename) => {
return path.join(settings.publicPath, settings.path, filename);
});
htmlPluginData.assets.js = concat(
dllEntriesPaths,
htmlPluginData.assets.js
);
callback(null, htmlPluginData);
}
);
});
}
}
}
export default AutoDLLPlugin;