Node中间层实践(三)——webpack设置

版权声明:更多文章请接见我的个人站
Keyon Y,转载请说明出处。

这里没什么可说的,webpack的设置和插件实在太多了,用的时刻查文档就好了。

项目地点:https://github.com/KeyonY/NodeMiddle

这里 先贴 个我的设置,解释写的挺细致的了。

开辟环境的webpack设置:

var path = require('path');
var webpack = require('webpack');
var TransferWebpackPlugin = require('transfer-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var localOptions = require('./localOptions');

var entrys = require('./entrys.js');

module.exports = {
    entry: entrys,
    output: {
        path: path.resolve(__dirname, './dist'),
        publicPath: localOptions.host,
        filename: 'Scripts/[name].js'
    },
    devtool: 'eval-source-map',
    module: {
        rules: [
            {test: /\.js$/,loader:'babel-loader'},
            {test: /\.pug$/,loader:'pug-loader',options: {pretty: true}},
            {test: /\.scss$/,use: ExtractTextPlugin.extract({fallback: 'style-loader', use: ['css-loader',{loader: 'postcss-loader',options: {config: {path: './build/postcss.config.js'}}},'sass-loader']})},
        ]
    },
    plugins: [
        new webpack.BannerPlugin('Copyright 2017 Keyon Y'),
        //把指定文件夹下的文件复制到指定的目次
        new TransferWebpackPlugin([
                {from: '../src/assets', to: '../dist/assets'},
            ],path.resolve(__dirname)),
        // webpack就可以比对id的运用频次和散布来得出最短的id分配给运用频次高的模块
        new webpack.optimize.OccurrenceOrderPlugin(),
        new webpack.HotModuleReplacementPlugin(),
        new ExtractTextPlugin({filename:'Contents/[name].css',disable: true,allChunks: true}),
        // 许可毛病不打断顺序
        new webpack.NoErrorsPlugin()
    ]
}

临盆环境的webpack设置:

var path = require('path');
var webpack = require('webpack');
var TransferWebpackPlugin = require('transfer-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');

var entrys = require('./entrys.js');

module.exports = {
    entry: entrys,
    output: {
        path: path.resolve(__dirname, '../dist'),
        publicPath: '/',
        filename: 'Scripts/[name].js'
    },
    module: {
        rules: [
            {test: /\.js$/,loader:'babel-loader'},
            {test: /\.pug$/,loader:'pug-loader',options: {pretty: true}},
            {test: /\.scss$/,use: ExtractTextPlugin.extract({fallback: 'style-loader', use: ['css-loader',{loader: 'postcss-loader',options: {config: {path: './build/postcss.config.js'}}},'sass-loader']})}
        ]
    },
    plugins: [
        new webpack.BannerPlugin('Copyright 2017 Keyon Y'),
        //把指定文件夹下的文件复制到指定的目次
        new TransferWebpackPlugin([
                {from: '../src/assets', to: '../dist/assets'},
                {from: '../src/Views', to: '../dist/Views'},
            ],path.resolve(__dirname)),
        // webpack就可以比对id的运用频次和散布来得出最短的id分配给运用频次高的模块
        new webpack.optimize.OccurrenceOrderPlugin(),
        new ExtractTextPlugin({filename:'Contents/[name].css',disable: false,allChunks: true}),
        // 殽杂紧缩js和css
        new webpack.optimize.UglifyJsPlugin({
            compress: {
                properties: false,
                warnings: false
            },
            output: {
                beautify: false,
                quote_keys: true
            },
            mangle: {
                screw_ie8: false
            },
            sourceMap: false,
            except: ['$', 'exports', 'require']    //消除关键字
        })
    ],
    stats: 'normal'
}

entry的设置,由于有太多的组件(‘src/Components’中)了,所以为了简化webpack.config的内容,我把entry的设置写在entry.js作为一个模块导入进来。

// entry.js
var webpackHotMiddlewareScript = 'webpack-hot-middleware/client?reload=true&timeout=2000';    //reload=true的意义是,假如遇到不能hot reload的状况,就整页革新
var isDev = process.env.NODE_ENV === 'dev';

var entryJson = {
    base: './src/Components/base/base.js',
    index: './src/Components/index/index.js',   // 首页--Default 路由
    message: './src/Components/message/message.js',
    home: './src/Components/home/home.js',
    modals: './src/Components/modals/modals.js',
}

if(isDev) { // 开辟环境中运用了webpack-hot-middleware,需要将每个entry的设置中写上'webpack-hot-middleware/client?reload=true&timeout=2000'
    var transJson = {};
    for(let e in entryJson) {
        transJson[e] = [entryJson[e], webpackHotMiddlewareScript];
    }
    module.exports = transJson;
}else {
    module.exports = entryJson;
}

迎接继承关注本博的更新
Node中间层实践(一)——基于NodeJS的全栈式开辟
Node中间层实践(二)——搭建项目框架
Node中间层实践(三)——webpack设置
Node中间层实践(四)——模板引擎pug
Node中间层实践(五)——express-中间层的逻辑处置惩罚

    原文作者:KeyonY
    原文地址: https://segmentfault.com/a/1190000013349546
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞