Blame view

public/build/webpack.base.config.js 3.94 KB
沈志敏 authored
1 2 3 4 5 6 7
/**
 * webpack config
 * @author: shenzm<zhimin.shen@yoho.cn>
 * @date: 2017/3/21
 */

'use strict';
ccbikai(👎🏻🍜) authored
8 9

const os = require('os');
沈志敏 authored
10
const path = require('path');
沈志敏 authored
11
const shelljs = require('shelljs');
ccbikai(👎🏻🍜) authored
12
const _ = require('lodash');
沈志敏 authored
13
const webpack = require('webpack');
ccbikai(👎🏻🍜) authored
14
const HappyPack = require('happypack');
陈峰 authored
15
const { cssLoader, hbsLoader } = require('./utils.js');
沈志敏 authored
16
const postcssConfig = require('./postcss.config.js');
ccbikai(👎🏻🍜) authored
17
ccbikai(👎🏻🍜) authored
18 19 20 21
const happyThreadPool = HappyPack.ThreadPool({ // eslint-disable-line
    size: os.cpus().length
});
沈志敏 authored
22
ccbikai(👎🏻🍜) authored
23 24
const getEntries = () => {
    const entries = {
陈峰 authored
25
        libs: ['babel-polyfill', 'yoho-jquery', path.join(__dirname, '../js/global.js')],
ccbikai(👎🏻🍜) authored
26 27 28 29
        common: path.join(__dirname, '../scss/common.css'),
        feature: path.join(__dirname, '../scss/feature.css')
    };
30 31 32 33 34 35 36 37 38 39 40
    // 构建各模块子页面JS
    // 新的生成规则 module/page/index.js
    shelljs.ls(path.join(__dirname, '../js/**/index.js')).forEach((f) => {
        const dir = _.slice(f.split('/'), -3); // [modulename, page, index.js]

        // Important
        // 生成规则:module.page: './js/module/page/index.js'
        entries[`${dir[0]}.${dir[1]}`] = path.join(__dirname, `../js/${dir.join('/')}`);
    });

    // 老的生成规则module.page.js
ccbikai(👎🏻🍜) authored
41 42
    shelljs.ls(path.join(__dirname, '../js/**/*.page.js')).forEach((f) => {
        const dir = _.slice(f.split('/'), -2); // [modulename, xx.page.js]
沈志敏 authored
43
ccbikai(👎🏻🍜) authored
44 45 46 47 48 49 50
        // Important
        // 生成规则:module.page: './js/module/xx.page.js'
        entries[`${dir[0]}.${dir[1].match(/(.*).page.js/)[1]}`] = path.join(__dirname, `../js/${dir.join('/')}`);
    });

    return entries;
};
沈志敏 authored
51
沈志敏 authored
52 53
module.exports = (env) => {
    return {
ccbikai(👎🏻🍜) authored
54
        entry: getEntries(),
沈志敏 authored
55 56 57 58
        output: {
            path: path.join(__dirname, 'bundle'), // absolute path
            filename: '[name].js'
        },
陈峰 authored
59
        devtool: 'hidden-source-map',
沈志敏 authored
60 61 62
        module: {
            rules: [{
                test: /\.vue$/,
沈志敏 authored
63
                use: [{
沈志敏 authored
64
                    loader: 'vue-loader',
沈志敏 authored
65
                    options: {
沈志敏 authored
66 67
                        postcss: {
                            plugins: postcssConfig.postcssPlugin(env),
Targaryen authored
68
                            parser: 'postcss-scss'
沈志敏 authored
69 70 71
                        },
                        autoprefixer: false,
                        loaders: {
ccbikai(👎🏻🍜) authored
72
                            css: cssLoader(env, 'vue')
毕凯 authored
73 74
                        },
                        esModule: false
沈志敏 authored
75
                    }
沈志敏 authored
76 77 78 79
                }],
            }, {
                test: /\.js$/,
                exclude: [/node_modules/],
沈志敏 authored
80
                use: [{
沈志敏 authored
81
                    loader: 'happypack/loader?id=js'
沈志敏 authored
82 83 84
                }]
            }, {
                test: /\.css$/,
ccbikai(👎🏻🍜) authored
85
                use: cssLoader(env, 'css')
沈志敏 authored
86 87
            }, {
                test: /\.hbs$/,
沈志敏 authored
88
                use: [{
ccbikai(👎🏻🍜) authored
89
                    loader: 'happypack/loader?id=hbs'
沈志敏 authored
90
                }]
沈志敏 authored
91
            }]
沈志敏 authored
92 93
        },
        resolve: {
ccbikai(👎🏻🍜) authored
94 95
            modules: [
                path.join(__dirname, '../../node_modules'),
沈志敏 authored
96 97 98 99 100
                path.join(__dirname, '../vue'),
                path.join(__dirname, '../hbs'),
                path.join(__dirname, '../scss'),
                path.join(__dirname, '../js')
            ],
沈志敏 authored
101
            alias: {
ccbikai(👎🏻🍜) authored
102
                vue: 'vue/dist/vue.common.js'
沈志敏 authored
103 104
            }
        },
105 106 107
        stats: {
            children: false
        },
沈志敏 authored
108
        plugins: [
ccbikai(👎🏻🍜) authored
109 110 111 112 113 114 115 116 117 118
            new HappyPack({
                id: 'js',
                threadPool: happyThreadPool,
                loaders: ['babel-loader'],
            }),
            new HappyPack({
                id: 'hbs',
                threadPool: happyThreadPool,
                loaders: [hbsLoader]
            }),
沈志敏 authored
119 120 121 122 123 124 125 126 127
            new webpack.optimize.CommonsChunkPlugin({
                name: 'libs',
                filename: 'libs.js'
            }),
            new webpack.ProvidePlugin({
                $: 'yoho-jquery',
                jQuery: 'yoho-jquery',
                'window.jQuery': 'yoho-jquery'
            })
ccbikai(👎🏻🍜) authored
128
        ]
沈志敏 authored
129
    };
130
};