1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/server/build/plugins/dynamic-entry-plugin.js

58 lines
1.5 KiB
JavaScript
Raw Normal View History

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)
}
}