UNPKG

2.18 kBJavaScriptView Raw
1'use strict';
2
3var path = require('path');
4var HappyPack = require('happypack');
5
6exports.config = function (options, cwd) {
7 var defaultQuery = {};
8 var babelPlugins = ['transform-class-properties', 'transform-decorators-legacy'];
9
10 if(options.ie8) {
11 babelPlugins.push('transform-es2015-modules-simple-commonjs');
12 }
13
14 if(this.webpack.version && this.webpack.version >= 2) {
15 defaultQuery = {
16 cacheDirectory: true,
17 presets: [
18 ['es2015', {loose: true, modules: false}],
19 'es2017',
20 'stage-0'
21 ],
22 plugins: babelPlugins
23 }
24 } else {
25 defaultQuery = {
26 cacheDirectory: true,
27 presets: [
28 ['es2015', {loose: true, modules: 'commonjs'}],
29 'es2017',
30 'stage-0'
31 ],
32 plugins: babelPlugins
33 }
34 }
35
36 var baseConfig = this.config,
37 testReg = options.test ? options.test : /\.(js|jsx)$/,
38 exclude = options.exclude ? options.exclude : /node_modules/,
39 query = options.modifyQuery ? options.modifyQuery(defaultQuery) : defaultQuery,
40 happyPackConfig = {
41 loaders: [
42 {
43 loader: 'babel-loader',
44 test: testReg,
45 exclude: exclude,
46 query: query
47 }
48 ],
49 threads: 4,
50 verbose: false,
51 cacheContext: {
52 env: process.env.NODE_ENV
53 },
54 tempDir: path.join(__dirname, '../happypack'),
55 cachePath: path.join(__dirname, '../happypack/cache--[id].json')
56 };
57
58 happyPackConfig = options.modifyHappypack ? options.modifyHappypack(happyPackConfig) : happyPackConfig;
59
60 extend(true, baseConfig, {
61 module: {
62 loaders: baseConfig.module.loaders.concat([{
63 test: testReg,
64 exclude: exclude,
65 loader: 'happypack/loader'
66 }])
67 },
68 plugins: baseConfig.plugins.concat([
69 new HappyPack(happyPackConfig)
70 ])
71 });
72};