-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgather-polyfills-plugin.js
39 lines (33 loc) · 1.28 KB
/
gather-polyfills-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
'use strict'
const _ = require('lodash')
module.exports = class GatherPolyfillsPlugin {
constructor (debug) {
if (debug) {
this.address = 'https://cdn.polyfill.io/v2/polyfill.js?features='
} else {
this.address = 'https://cdn.polyfill.io/v2/polyfill.min.js?features='
}
}
apply (compiler) {
let polyfillValues = []
compiler.hooks.normalModuleFactory.tap('GatherPolyfillsPlugin', normalModuleFactory => {
normalModuleFactory.hooks.beforeResolve.tapAsync('GatherPolyfillsPlugin', (data, callback) => {
for (let key of [ 'request', 'userRequest', 'resource' ]) {
if (data[ key ] && data[ key ].match(/polyfill!/)) {
polyfillValues = _.union(polyfillValues, data[ key ].split('!')[ 1 ].split(','))
data[ 'request' ] = 'ignore-loader!'
data[ key ] = 'ignore-loader!'
}
}
return callback(null, data)
})
})
compiler.hooks.compilation.tap('GatherPolyfillsPlugin', compilation => {
compilation.hooks.htmlWebpackPluginBeforeHtmlGeneration.tapAsync('GatherPolyfillsPlugin', (data, callback) => {
let polyAddress = this.address + polyfillValues.join(',') + '&flags=gated'
data.assets.js.unshift(polyAddress)
callback(null, data)
})
})
}
}