Skip to content
This repository has been archived by the owner on Dec 6, 2021. It is now read-only.

Support multi-compiler #241

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions packages/poi/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,30 @@ class Poi extends EventEmitter {
return config
}

getCompiler() {
return new Promise((resolve, reject) => {
if (this.compiler.compilers) {
this.compiler.compilers.forEach(compiler => resolve(compiler))
} else {
resolve(this.compiler)
}
reject()
})
}

build() {
return this.runMiddlewares()
.then(() => {
this.createCompiler()
const { filename, path: outputPath } = this.compiler.options.output
// Only remove dist file when name contains hash
const implicitlyRemoveDist = this.options.removeDist !== false && /\[(chunk)?hash:?\d?\]/.test(filename)
if (this.options.removeDist === true || implicitlyRemoveDist) {
return promisify(rm)(path.join(outputPath, '*'))
}

return this.getCompiler().then(compiler => {
const { filename, path: outputPath } = compiler.options.output
// Only remove dist file when name contains hash
const implicitlyRemoveDist = this.options.removeDist !== false && /\[(chunk)?hash:?\d?\]/.test(filename)
if (this.options.removeDist === true || implicitlyRemoveDist) {
return promisify(rm)(path.join(outputPath, '*'))
}
})
})
.then(() => runWebpack(this.compiler))
}
Expand All @@ -91,7 +105,7 @@ class Poi extends EventEmitter {
return this.runMiddlewares()
.then(() => {
this.createCompiler()
return createServer(this.compiler, this.options)
return this.getCompiler().then(compiler => createServer(compiler, this.options))
})
}

Expand All @@ -102,7 +116,9 @@ class Poi extends EventEmitter {
createCompiler(webpackConfig = this.getWebpackConfig()) {
this.compiler = webpack(webpackConfig)
if (this.options.inMemory) {
this.compiler.outputFileSystem = new MemoryFS()
this.getCompiler().then(compiler => {
compiler.outputFileSystem = new MemoryFS()
})
}
return this
}
Expand Down