forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdynamic-entry-plugin.js
68 lines (57 loc) · 1.81 KB
/
dynamic-entry-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
import SingleEntryPlugin from 'webpack/lib/SingleEntryPlugin'
import MultiEntryPlugin from 'webpack/lib/MultiEntryPlugin'
import { detachable } from './detach-plugin'
detachable(SingleEntryPlugin)
detachable(MultiEntryPlugin)
export default class DynamicEntryPlugin {
apply (compiler) {
compiler.entryNames = getInitialEntryNames(compiler)
compiler.addEntry = addEntry
compiler.removeEntry = removeEntry
compiler.hasEntry = hasEntry
compiler.plugin('emit', (compilation, callback) => {
compiler.cache = compilation.cache
callback()
})
}
}
function getInitialEntryNames (compiler) {
const entryNames = new Set()
const { entry } = compiler.options
if (typeof entry === 'string' || Array.isArray(entry)) {
entryNames.add('main')
} else if (typeof entry === 'object') {
Object.keys(entry).forEach((name) => {
entryNames.add(name)
})
}
return entryNames
}
function addEntry (entry, name = 'main') {
const { context } = this.options
const Plugin = Array.isArray(entry) ? MultiEntryPlugin : SingleEntryPlugin
this.apply(new Plugin(context, entry, name))
this.entryNames.add(name)
}
function removeEntry (name = 'main') {
for (const p of this.getDetachablePlugins()) {
if (!(p instanceof SingleEntryPlugin || p instanceof MultiEntryPlugin)) continue
if (p.name !== name) continue
if (this.cache) {
for (const id of Object.keys(this.cache)) {
const m = this.cache[id]
if (m.name === name) {
// cache of `MultiModule` is based on `name`,
// so delete it here for the case
// a new entry is added with the same name later
delete this.cache[id]
}
}
}
this.detach(p)
}
this.entryNames.delete(name)
}
function hasEntry (name = 'main') {
return this.entryNames.has(name)
}