-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwebpack.lodashPlugin.js
More file actions
39 lines (31 loc) · 1.39 KB
/
webpack.lodashPlugin.js
File metadata and controls
39 lines (31 loc) · 1.39 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
// Source: https://swizec.com/blog/dirty-hack-took-30percent-off-webpack-size/swizec/7657
const LodashModules = (() => {
const path = require('path'),
execSync = require('child_process').execSync;
const files = path.resolve(__dirname, 'js'),
code = execSync(`grep -r "_\\\." ${files} --exclude=mathquill.js`,
{encoding: 'utf-8'});
const matches = new Set(code.match(/_\.([a-z]+)/ig));
return Array.from(matches.values())
.map(m => {
let module = m.replace('_.', 'lodash.').toLowerCase(),
vars = [m];
if (module === 'lodash.extend') {
module = 'lodash.assign';
vars = vars.concat('_.assign');
}
if (module === 'lodash.each') {
module = 'lodash.foreach';
vars = vars.concat('_.forEach');
}
try {
require.resolve(module);
} catch(e) {
console.log(`Installing ${module}`);
execSync(`npm install --save ${module}`);
}
return new webpack.ProvidePlugin(
_.fromPairs(vars.map(v => [v, module]))
)
})
})();