refactor: perf-loader and test

This commit is contained in:
Clark Du 2018-04-02 20:08:31 +08:00
parent e2e124b8e4
commit 89bdd71591
No known key found for this signature in database
GPG Key ID: D0E5986AF78B86D9
4 changed files with 12 additions and 13 deletions

View File

@ -466,7 +466,7 @@ export default class Builder {
// Warmup perfLoader before build
if (this.options.build.parallel) {
consola.start('Warming up worker pools')
this.perfLoader.warmup()
this.perfLoader.warmupAll()
consola.success('Worker pools ready')
}

View File

@ -1,6 +1,6 @@
import path from 'path'
import threadLoader from 'thread-loader'
import { warmup } from 'thread-loader'
// https://github.com/webpack-contrib/thread-loader
// https://github.com/webpack-contrib/cache-loader
@ -8,12 +8,11 @@ import threadLoader from 'thread-loader'
export default class PerfLoader {
constructor(options) {
this.options = options
this.threadLoader = threadLoader
this.warmup = warmup
this.workerPools = {
js: {
name: 'js',
poolTimeout: this.options.dev ? Infinity : 2000
},
css: {
name: 'css',
@ -22,9 +21,9 @@ export default class PerfLoader {
}
}
warmup() {
this.threadLoader.warmup(this.workerPools.js, ['babel-loader', 'babel-preset-env'])
this.threadLoader.warmup(this.workerPools.css, ['css-loader'])
warmupAll() {
this.warmup(this.workerPools.js, ['babel-loader', 'babel-preset-env'])
this.warmup(this.workerPools.css, ['css-loader'])
}
pool(poolName, _loaders) {

View File

@ -48,6 +48,7 @@ export default {
publicPath: '/orion/',
maxChunkSize: 300000,
cssSourceMap: true,
parallel: true,
analyze: {
analyzerMode: 'disabled',
generateStatsFile: true,

View File

@ -15,12 +15,11 @@ describe('webpack configuration', () => {
const css = { name: 'css', poolTimeout: Infinity }
expect(perfLoader.workerPools).toMatchObject({ js, css })
const warmup = jest.fn()
perfLoader.threadLoader = { warmup }
perfLoader.warmup()
expect(warmup).toHaveBeenCalledTimes(2)
expect(warmup).toHaveBeenCalledWith(js, ['babel-loader', 'babel-preset-env'])
expect(warmup).toHaveBeenCalledWith(css, ['css-loader'])
perfLoader.warmup = jest.fn()
perfLoader.warmupAll()
expect(perfLoader.warmup).toHaveBeenCalledTimes(2)
expect(perfLoader.warmup).toHaveBeenCalledWith(js, ['babel-loader', 'babel-preset-env'])
expect(perfLoader.warmup).toHaveBeenCalledWith(css, ['css-loader'])
const loaders = perfLoader.pool('js', { loader: 'test-perf-loader' })
const cacheDirectory = path.resolve('node_modules/.cache/cache-loader')