-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebpack.vendorReplace.plugin.js
116 lines (105 loc) · 4.23 KB
/
webpack.vendorReplace.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
'use strict'
var _interopRequireDefault = require('@babel/runtime/helpers/interopRequireDefault')
var JavascriptModulesPlugin = require('webpack/lib/javascript/JavascriptModulesPlugin')
Object.defineProperty(exports, '__esModule', {
value: true,
})
exports.default = void 0
var _classCallCheck2 = _interopRequireDefault(
require('@babel/runtime/helpers/classCallCheck')
)
var _createClass2 = _interopRequireDefault(
require('@babel/runtime/helpers/createClass')
)
var _webpackSources = require('webpack-sources')
var VendorReplacePlugin =
/*#__PURE__*/
(function () {
function VendorReplacePlugin (entry, type = 'elements') {
;(0, _classCallCheck2.default)(this, VendorReplacePlugin)
this.entry = entry
this.type = type
}
;(0, _createClass2.default)(VendorReplacePlugin, [
{
key: 'apply',
value: function apply (compiler) {
var _this = this
compiler.hooks.afterPlugins.tap(
'VendorReplacePlugin',
function (comp) {
comp.hooks.compilation.tap(
'VendorReplacePlugin',
function (compilation) {
var handler = function handler (modules) {
compilation.chunks.forEach((chunk) => {
compilation.chunkGraph
.getChunkModules(chunk)
.forEach((module) => {
// module is available here
var id = compilation.chunkGraph.getModuleId(module)
if (typeof id === 'string') {
// Replace ../../node_modules
id = id.replace(/(\.\.\/)+(node_modules)/, './$2')
if (
_this.type === 'addons' &&
id &&
id.indexOf(_this.entry) !== -1
) {
id = './addon/' + _this.entry
}
compilation.chunkGraph.setModuleId(module, id)
}
})
})
}
compilation.hooks.moduleIds.tap(
'VendorReplacePlugin',
handler
) // Intercept default chunk template plugin
JavascriptModulesPlugin.getCompilationHooks(
compilation
).renderChunk.intercept({
register: function register (options) {
if (
options.name === 'ArrayPushCallbackChunkFormatPlugin'
) {
options.fn = (modules, renderContext) => {
const { chunk, runtimeTemplate } = renderContext
var source = new _webpackSources.ConcatSource()
const chunkLoadingGlobal =
runtimeTemplate.outputOptions.chunkLoadingGlobal
const globalObject =
runtimeTemplate.outputOptions.globalObject
source.add(
`(${globalObject}[${JSON.stringify(
chunkLoadingGlobal
)}] = ${globalObject}[${JSON.stringify(
chunkLoadingGlobal
)}] || []).push([`
)
source.add(`${JSON.stringify(chunk.ids)},`)
source.add(modules)
if (_this.type === 'addons') {
source.add(
",[['./addon/".concat(_this.entry, "']]])")
)
} else {
source.add(",[['./".concat(_this.entry, "']]])"))
}
return source
}
}
return options
},
})
}
)
}
)
},
},
])
return VendorReplacePlugin
})()
exports.default = VendorReplacePlugin