rsnext/server/build/plugins/dynamic-entry-plugin.js

69 lines
1.8 KiB
JavaScript
Raw Normal View History

2016-10-23 18:42:13 +02:00
import SingleEntryPlugin from 'webpack/lib/SingleEntryPlugin'
import MultiEntryPlugin from 'webpack/lib/MultiEntryPlugin'
import { detachable } from './detach-plugin'
detachable(SingleEntryPlugin)
detachable(MultiEntryPlugin)
2016-10-23 18:42:13 +02:00
export default class DynamicEntryPlugin {
apply (compiler) {
compiler.entryNames = getInitialEntryNames(compiler)
compiler.addEntry = addEntry
compiler.removeEntry = removeEntry
compiler.hasEntry = hasEntry
2016-10-24 04:38:55 +02:00
compiler.plugin('emit', (compilation, callback) => {
compiler.cache = compilation.cache
callback()
2016-10-24 04:38:55 +02:00
})
2016-10-23 18:42:13 +02:00
}
}
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)
}
2016-10-23 18:42:13 +02:00
this.entryNames.delete(name)
}
function hasEntry (name = 'main') {
2016-10-24 03:55:25 +02:00
return this.entryNames.has(name)
2016-10-23 18:42:13 +02:00
}