博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Node中间层实践(三)——webpack配置
阅读量:6974 次
发布时间:2019-06-27

本文共 4115 字,大约阅读时间需要 13 分钟。

版权声明:更多文章请访问我的个人站 ,转载请注明出处。

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

项目地址:

这里 先贴 个我的配置,注释写的挺详细的了。

开发环境的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.jsvar 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;}

欢迎继续关注本博的更新

你可能感兴趣的文章
浏览器自动化测试解決方案 Geb
查看>>
《C程序员从校园到职场》一导读
查看>>
我希望一年前就知道 MongoDB 的那些事儿
查看>>
《Spark 官方文档》Spark独立模式
查看>>
《树莓派Python编程入门与实战(第2版)》——1.5 决定如何购买外围设备
查看>>
完全指南之在 Ubuntu 操作系统中安装及卸载软件
查看>>
《Spark 官方文档》在YARN上运行Spark
查看>>
《C++面向对象高效编程(第2版)》——2.5 数据封装的优点
查看>>
判断email格式的正则表达式
查看>>
HTTP Referer 二三事
查看>>
《策略驱动型数据中心——ACI技术详解》——导读
查看>>
SPDY 是什么?如何部署 SPDY?
查看>>
WebSocket实现网页聊天室
查看>>
《无人机DIY》——3.2 大疆Phantom 2 Vision+
查看>>
《Flink官方文档》Python 编程指南测试版(二)
查看>>
Linux有问必答:如何在VMware ESXi虚拟机上设置静态MAC地址
查看>>
《Unity 游戏案例开发大全》一6.1 背景以及功能概述
查看>>
《C++代码设计与重用》——2.6 接口一致性
查看>>
《AngularJS高级程序设计》——2.4 小结
查看>>
Spark Streaming + Spark SQL 实现配置化ETL流程
查看>>