2018-03-23 07:34:55 +00:00
|
|
|
import path from 'path'
|
|
|
|
|
2018-04-02 12:08:31 +00:00
|
|
|
import { warmup } from 'thread-loader'
|
2018-03-23 07:34:55 +00:00
|
|
|
|
|
|
|
// https://github.com/webpack-contrib/thread-loader
|
|
|
|
// https://github.com/webpack-contrib/cache-loader
|
|
|
|
|
|
|
|
export default class PerfLoader {
|
2018-03-30 01:57:30 +00:00
|
|
|
constructor(options) {
|
|
|
|
this.options = options
|
2018-04-02 12:08:31 +00:00
|
|
|
this.warmup = warmup
|
2018-03-23 07:34:55 +00:00
|
|
|
this.workerPools = {
|
|
|
|
js: {
|
|
|
|
name: 'js',
|
|
|
|
poolTimeout: this.options.dev ? Infinity : 2000
|
|
|
|
},
|
|
|
|
css: {
|
|
|
|
name: 'css',
|
|
|
|
poolTimeout: this.options.dev ? Infinity : 2000
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-02 12:08:31 +00:00
|
|
|
warmupAll() {
|
|
|
|
this.warmup(this.workerPools.js, ['babel-loader', 'babel-preset-env'])
|
|
|
|
this.warmup(this.workerPools.css, ['css-loader'])
|
2018-03-23 07:34:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pool(poolName, _loaders) {
|
|
|
|
const loaders = [].concat(_loaders)
|
|
|
|
|
|
|
|
if (this.options.build.parallel) {
|
|
|
|
const pool = this.workerPools[poolName]
|
|
|
|
|
|
|
|
if (pool) {
|
|
|
|
loaders.unshift({
|
|
|
|
loader: 'thread-loader',
|
|
|
|
options: pool
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.options.build.cache) {
|
|
|
|
loaders.unshift({
|
|
|
|
loader: 'cache-loader',
|
|
|
|
options: {
|
|
|
|
cacheDirectory: path.resolve('node_modules/.cache/cache-loader')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return loaders
|
|
|
|
}
|
2018-03-29 19:26:42 +00:00
|
|
|
|
|
|
|
poolOneOf(poolName, oneOfRules) {
|
|
|
|
return oneOfRules.map(rule => Object.assign({}, rule, {
|
|
|
|
use: this.pool(poolName, rule.use)
|
|
|
|
}))
|
|
|
|
}
|
2018-03-23 07:34:55 +00:00
|
|
|
}
|