2016-10-23 16:42:13 +00:00
|
|
|
import SingleEntryPlugin from 'webpack/lib/SingleEntryPlugin'
|
|
|
|
import MultiEntryPlugin from 'webpack/lib/MultiEntryPlugin'
|
|
|
|
|
|
|
|
export default class DynamicEntryPlugin {
|
|
|
|
apply (compiler) {
|
|
|
|
compiler.entryNames = getInitialEntryNames(compiler)
|
|
|
|
compiler.addEntry = addEntry
|
|
|
|
compiler.removeEntry = removeEntry
|
|
|
|
compiler.hasEntry = hasEntry
|
2016-10-24 02:38:55 +00:00
|
|
|
|
|
|
|
compiler.plugin('compilation', (compilation) => {
|
|
|
|
compilation.addEntry = compilationAddEntry(compilation.addEntry)
|
|
|
|
})
|
2016-10-23 16:42:13 +00: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') {
|
|
|
|
this.entryNames.delete(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
function hasEntry (name = 'main') {
|
2016-10-24 01:55:25 +00:00
|
|
|
return this.entryNames.has(name)
|
2016-10-23 16:42:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function compilationAddEntry (original) {
|
|
|
|
return function (context, entry, name, callback) {
|
|
|
|
if (!this.compiler.entryNames.has(name)) {
|
|
|
|
// skip removed entry
|
|
|
|
callback()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return original.call(this, context, entry, name, callback)
|
|
|
|
}
|
|
|
|
}
|