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

Commit

Permalink
rename getWebpackConfig to createWebpackConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed Dec 13, 2017
1 parent e3a5a03 commit fc4f5ab
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 66 deletions.
6 changes: 2 additions & 4 deletions packages/poi/bin/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ module.exports = function (cliOptions) {

const app = poi(merge(config, cliOptions))

yield app.prepare()

console.log(`> Bundling with Webpack ${require('webpack/package.json').version}`)

const { options } = app
Expand Down Expand Up @@ -105,10 +103,10 @@ module.exports = function (cliOptions) {

watchFiles({ server })
} else if (typeof options.mode === 'string') {
yield app.prepare()
yield app.runMiddlewares()
if (app.middlewares.length === 0) {
console.log('> Please use this command with Poi presets')
} else {
app.runMiddlewares().catch(handleError)
}
}
})
Expand Down
101 changes: 58 additions & 43 deletions packages/poi/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,45 +27,54 @@ function runWebpack(compiler) {
class Poi extends EventEmitter {
constructor(options) {
super()
this.options = Object.assign({
cwd: '.',
argv: yargs.argv,
// Required for cloud IDE like cloud9
host: process.env.HOST || '0.0.0.0',
port: process.env.PORT || 4000
}, options)
this.options = Object.assign(
{
cwd: '.',
argv: yargs.argv,
// Required for cloud IDE like cloud9
host: process.env.HOST || '0.0.0.0',
port: process.env.PORT || 4000
},
options
)

if (!process.env.NODE_ENV) {
// env could be `production` `development` `test`
process.env.NODE_ENV = this.options.mode === 'watch' ? 'development' : this.options.mode
process.env.NODE_ENV =
this.options.mode === 'watch' ? 'development' : this.options.mode
}

this.manifest = readPkg()
this.middlewares = []
this.webpackFlows = []
}

/*
Load everything that's required for creating our webpack config
*/
prepare() {
return handleOptions(this.options)
.then(options => {
this.options = options
this.manifest = readPkg()
this.middlewares = []
this.webpackFlows = []

this.usePresets()
this.addWebpackFlow(config => {
config.plugin('compile-notifier')
.use(PostCompilePlugin, [stats => {
this.emit('compile-done', stats)
}])
})
if (this.options.extendWebpack) {
this.addWebpackFlow(this.options.extendWebpack)
}

this.webpackConfig = createConfig(this.options)
this.webpackFlows.forEach(flow => flow(this.webpackConfig))
})
return handleOptions(this.options).then(options => {
this.options = options
})
}

getWebpackConfig() {
createWebpackConfig() {
if (this.webpackConfig) return this.webpackConfig.toConfig()

this.usePresets()
this.addWebpackFlow(config => {
config.plugin('compile-notifier').use(PostCompilePlugin, [
stats => {
this.emit('compile-done', stats)
}
])
})
if (this.options.extendWebpack) {
this.addWebpackFlow(this.options.extendWebpack)
}

this.webpackConfig = createConfig(this.options)
this.webpackFlows.forEach(flow => flow(this.webpackConfig))
const config = this.webpackConfig.toConfig()
if (this.options.webpack) {
return typeof this.options.webpack === 'function' ?
Expand All @@ -76,12 +85,15 @@ class Poi extends EventEmitter {
}

build() {
return this.runMiddlewares()
.then(() => {
this.createCompiler()
return this.prepare()
.then(() => this.runMiddlewares())
.then(webpackConfig => {
this.createCompiler(webpackConfig)
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)
const implicitlyRemoveDist =
this.options.removeDist !== false &&
/\[(chunk)?hash:?\d?\]/.test(filename)
if (this.options.removeDist === true || implicitlyRemoveDist) {
return promisify(rm)(path.join(outputPath, '*'))
}
Expand All @@ -90,22 +102,24 @@ class Poi extends EventEmitter {
}

watch() {
return this.runMiddlewares()
.then(() => {
this.createCompiler()
return this.prepare()
.then(() => this.runMiddlewares())
.then(webpackConfig => {
this.createCompiler(webpackConfig)
return this.compiler.watch({}, () => {})
})
}

dev() {
return this.runMiddlewares()
.then(() => {
this.createCompiler()
return this.prepare()
.then(() => this.runMiddlewares())
.then(webpackConfig => {
this.createCompiler(webpackConfig)
return createServer(this.compiler, this.options)
})
}

createCompiler(webpackConfig = this.getWebpackConfig()) {
createCompiler(webpackConfig) {
this.compiler = webpack(webpackConfig)
if (this.options.inMemory) {
this.compiler.outputFileSystem = new MemoryFS()
Expand Down Expand Up @@ -170,11 +184,12 @@ class Poi extends EventEmitter {

runMiddlewares() {
return new Promise((resolve, reject) => {
const webpackConfig = this.createWebpackConfig()
ware()
.use(this.middlewares)
.run(this.getWebpackConfig(), err => {
.run(webpackConfig, err => {
if (err) return reject(err)
resolve()
resolve(webpackConfig)
})
})
}
Expand Down
38 changes: 19 additions & 19 deletions packages/poi/test/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('get webpack config', () => {
const app = poi()

await app.prepare()
const config = app.getWebpackConfig()
const config = app.createWebpackConfig()

expect(config.entry)
.toEqual({
Expand All @@ -37,7 +37,7 @@ describe('get webpack config', () => {

const [a, b, c, d] = await Promise.all(entries.map(entry => {
const app = poi({ entry })
return app.prepare().then(() => app.getWebpackConfig())
return app.prepare().then(() => app.createWebpackConfig())
}))

expect(a.entry.client)
Expand All @@ -59,7 +59,7 @@ describe('get webpack config', () => {
mode: 'development'
})
await app.prepare()
const config = app.getWebpackConfig()
const config = app.createWebpackConfig()

expect(config.entry.client)
.toEqual([
Expand All @@ -78,7 +78,7 @@ describe('get webpack config', () => {
hotEntry: ['foo', 'bar']
})
await app.prepare()
const config = app.getWebpackConfig()
const config = app.createWebpackConfig()

expect(config.entry.foo)
.toEqual([
Expand All @@ -98,15 +98,15 @@ describe('get webpack config', () => {
it('default dir', async () => {
const app = poi()
await app.prepare()
const config = app.getWebpackConfig()
const config = app.createWebpackConfig()

expect(config.output.path).toBe(path.resolve('dist'))
})

it('custom dir', async () => {
const app = poi({ dist: 'foo/bar' })
await app.prepare()
const config = app.getWebpackConfig()
const config = app.createWebpackConfig()

expect(config.output.path).toBe(path.resolve('foo/bar'))
})
Expand All @@ -116,10 +116,10 @@ describe('get webpack config', () => {
it('copy existing static folder', async () => {
const app = poi()
await app.prepare()
const config = app.webpackConfig
app.createWebpackConfig()

expect(config.plugins.has('copy-static-files')).toBe(true)
expect(config.plugins.get('copy-static-files').get('args')[0].length)
expect(app.webpackConfig.plugins.has('copy-static-files')).toBe(true)
expect(app.webpackConfig.plugins.get('copy-static-files').get('args')[0].length)
.toBe(1)
})

Expand All @@ -128,9 +128,9 @@ describe('get webpack config', () => {
copy: {}
})
await app.prepare()
const config = app.webpackConfig
app.createWebpackConfig()

expect(config.plugins.get('copy-static-files').get('args')[0].length)
expect(app.webpackConfig.plugins.get('copy-static-files').get('args')[0].length)
.toBe(2)
})

Expand All @@ -139,9 +139,9 @@ describe('get webpack config', () => {
copy: [{}, {}]
})
await app.prepare()
const config = app.webpackConfig
app.createWebpackConfig()

expect(config.plugins.get('copy-static-files').get('args')[0].length)
expect(app.webpackConfig.plugins.get('copy-static-files').get('args')[0].length)
.toBe(3)
})

Expand All @@ -150,9 +150,9 @@ describe('get webpack config', () => {
copy: false
})
await app.prepare()
const config = app.webpackConfig
app.createWebpackConfig()

expect(config.plugins.has('copy-static-files'))
expect(app.webpackConfig.plugins.has('copy-static-files'))
.toBe(false)
})
})
Expand All @@ -170,7 +170,7 @@ describe('get webpack config', () => {
presets: preset
})
await app.prepare()
const config = app.getWebpackConfig()
const config = app.createWebpackConfig()

expect(config.entry.foo).toEqual([path.resolve('foo', 'haha.js')])
})
Expand All @@ -194,7 +194,7 @@ describe('get webpack config', () => {
presets
})
await app.prepare()
const config = app.getWebpackConfig()
const config = app.createWebpackConfig()

expect(config.entry.foo).toEqual(['foo', 'bar'])
})
Expand All @@ -209,7 +209,7 @@ describe('get webpack config', () => {
})

await app.prepare()
const config = app.getWebpackConfig()
const config = app.createWebpackConfig()

expect(config.output.libraryTarget).toBe('commonjs2')
expect(config.externals).toEqual([['externals'], 'vue', 'babel-runtime'])
Expand All @@ -223,7 +223,7 @@ describe('get webpack config', () => {
})

await app.prepare()
const config = app.getWebpackConfig()
const config = app.createWebpackConfig()

expect(config.output.libraryTarget).toBe('umd')
expect(config.output.library).toBe('LibraryName')
Expand Down

0 comments on commit fc4f5ab

Please sign in to comment.