This repository has been archived by the owner on Apr 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
HtmlWebpackExternalsPlugin.js
130 lines (111 loc) · 3.6 KB
/
HtmlWebpackExternalsPlugin.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
import CopyWebpackPlugin from 'copy-webpack-plugin'
import HtmlWebpackIncludeAssetsPlugin from 'html-webpack-include-assets-plugin'
import Ajv from 'ajv'
import configSchema from './configSchema.json'
export default class HtmlWebpackExternalsPlugin {
static validateArguments = (() => {
const ajv = new Ajv({ useDefaults: true })
const validateConfig = ajv.compile(configSchema)
return config => {
if (!validateConfig(config)) {
throw new TypeError(ajv.errorsText(validateConfig.errors))
}
}
})()
static URL_ENTRY = /^(http:|https:)?\/\//
constructor(config) {
HtmlWebpackExternalsPlugin.validateArguments(config)
this.assetsToPrepend = []
this.assetsToAppend = []
this.assetsToCopy = []
this.externals = {}
const { externals, hash, outputPath, publicPath, files, enabled } = config
this.hash = hash
this.outputPath = outputPath
this.publicPath = publicPath
this.files = files
this.enabled = enabled
externals.forEach(({ module, entry, global, supplements, append }) => {
this.externals[module] = global
const localEntries = []
const entries = (Array.isArray(entry) ? entry : [entry]).map(entry => {
if (typeof entry === 'string') {
entry = { path: entry, type: undefined }
}
if (HtmlWebpackExternalsPlugin.URL_ENTRY.test(entry.path)) {
return entry
}
const localEntry = `${module}/${entry.path}`
localEntries.push(localEntry)
return { ...entry, path: localEntry }
})
if (append) {
this.assetsToAppend = [...this.assetsToAppend, ...entries]
} else {
this.assetsToPrepend = [...this.assetsToPrepend, ...entries]
}
this.assetsToCopy = [
...this.assetsToCopy,
...localEntries,
...supplements.map(asset => `${module}/${asset}`),
]
})
}
apply(compiler) {
if (!this.enabled) {
return
}
if (!compiler.options.externals) {
compiler.options.externals = this.externals
} else if (Array.isArray(compiler.options.externals)) {
compiler.options.externals.push(this.externals)
} else if (typeof compiler.options.externals === 'object') {
compiler.options.externals = {
...compiler.options.externals,
...this.externals,
}
}
const publicPath = (() => {
if (this.publicPath != null) {
return this.publicPath
} else if (compiler.options.output.publicPath != null) {
return compiler.options.output.publicPath
} else {
return ''
}
})()
const pluginsToApply = []
pluginsToApply.push(
new CopyWebpackPlugin(
this.assetsToCopy.map(asset => ({
from: `node_modules/${asset}`,
to: `${this.outputPath}/${asset}`,
}))
)
)
const createAssetsPlugin = (assets, append) => {
if (assets.length) {
pluginsToApply.push(
new HtmlWebpackIncludeAssetsPlugin({
assets: assets.map(
asset =>
HtmlWebpackExternalsPlugin.URL_ENTRY.test(asset.path)
? asset
: {
...asset,
path: `${publicPath}${this.outputPath}/${asset.path}`,
}
),
append,
hash: this.hash,
files: this.files == null ? undefined : this.files,
publicPath: '',
})
)
}
}
createAssetsPlugin(this.assetsToPrepend, false)
createAssetsPlugin(this.assetsToAppend, true)
pluginsToApply.forEach(plugin => plugin.apply(compiler))
}
}