webpack.config.js 3.2 KB
Newer Older
B
BingBlog 已提交
1
'use strict';
B
BingBlog 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
const webpack = require('webpack');
const path = require('path');
const projectPath = path.resolve(__dirname, '..');
const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin');
const argv = require('yargs').argv;
const isDev = process.env.NODE_ENV === 'dev';
const entry = require('./entry');
const ExtractTextPlugin = require('extract-text-webpack-plugin');

function getLoaders(isDev, ext) {
    let arr = ['css-loader'];
    if (ext) {
        arr.push(ext + '-loader');
    }
    if (isDev) {
        arr.unshift('style-loader');
        return arr;
    }

    return ExtractTextPlugin.extract({
        use: arr,
        fallback: 'style-loader'
    });

}

/**
 * entry config
 *
 * @type {Object}
 */

const ENTR_CONFIG = entry.get(argv.app, argv.template);
/**
 * webpack config
 *
 * @type {Object}
 */
const config = {
    entry: ENTR_CONFIG.module,
    output: {
        path: path.resolve(projectPath, 'dist'),
        filename: '[name].[hash].js'
    },
    resolve: {

        alias: {
J
Jeff Wang 已提交
49 50 51 52
            axios: 'axios/dist/axios.min.js',
            'vue$': 'vue/dist/vue.esm.js',
            '@': path.resolve(projectPath, 'src'),
            style: path.resolve(__dirname, '../src/style')
B
BingBlog 已提交
53 54
        },

J
Jeff Wang 已提交
55
        extensions: ['.js', '.json', '.styl', '.css', '.html', '.vue']
B
BingBlog 已提交
56 57 58 59
    },

    module: {
        noParse: [
J
Jeff Wang 已提交
60
            /node_modules\/(axios)\//
B
BingBlog 已提交
61 62 63
        ],
        rules: [
            {
J
Jeff Wang 已提交
64 65
                test: /\.vue$/,
                loader: 'vue-loader',
B
BingBlog 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
            },
            {
                test: /\.js$/,
                exclude: /node_modules/,
                include: [
                    path.resolve(projectPath, 'src')
                ],
                loader: 'babel-loader'
            },
            {
                test: /\.json$/,
                loader: 'json-loader'
            },
            {
                test: /\.html/,
                loader: 'html-loader',
                options: {
                    minimize: false
                }
            },
            {
                test: /\.css$/,
                use: getLoaders(isDev)
            },
            {
                test: /\.styl$/,
                use: getLoaders(isDev, 'stylus')
            },
            {
95
                test: /\.(gif|png|jpe?g|svg)$/i,
B
BingBlog 已提交
96 97
                loader: 'file-loader',
                options: {
98
                    name: 'assets/[name].[hash].[ext]'
B
BingBlog 已提交
99 100 101 102 103 104 105 106 107 108 109 110
                }
            },
            {
                test: /\.woff2?$/,
                loader: 'url-loader',
                options: {
                    name: 'fonts/[name].[hash].[ext]',
                    limit: '10000',
                    mimetype: 'application/font-woff'
                }
            },
            {
111
                test: /\.(ttf|eot)$/,
B
BingBlog 已提交
112 113 114 115 116 117 118 119 120 121 122
                loader: 'file-loader',
                options: {
                    name: 'fonts/[name].[hash].[ext]'
                }
            }
        ]
    },
    plugins: [

        new CaseSensitivePathsPlugin(),
        new webpack.LoaderOptionsPlugin({
J
Jeff Wang 已提交
123
            test: /\.(styl)$/
124 125
        }),
        new ExtractTextPlugin({filename: '[name].css'})
B
BingBlog 已提交
126 127 128 129 130 131 132
    ]
};

// template config
config.plugins = config.plugins.concat(ENTR_CONFIG.template);

module.exports = config;