这不算是初学者的入门文章,也不能算是高端用户的进阶。这只是我本身在配置Webpack过程当中收集整理的一些资料,以及本身经常使用的整个配置流程。由于有时候总是忘了某个东西是怎么配置的,因此记录下来用于速查和备忘。css
$ npm init $ npm install webpack --save-dev # 全局安装依赖 # or $ npm install webpack-dev-server --save-dev # 安装webpack调试工具
// config/webpack.config.js const webpack = require('webpack'); // 配置目录 // 由于咱们的webpack.config.js文件不在项目根目录下,因此须要一个路径的配置 const path = require('path'); const CURRENT_PATH = path.resolve(__dirname); // 获取到当前目录 const ROOT_PATH = path.join(__dirname, '../'); // 项目根目录 const MODULES_PATH = path.join(ROOT_PATH, './node_modules'); // node包目录 const BUILD_PATH = path.join(ROOT_PATH, './public/assets'); // 最后输出放置公共资源的目录 module.exports = { context: path.join(__dirname, '../'), // 设置webpack配置中指向的默认目录为项目根目录 entry: { index: './public/pages/index.js', public: './public/pages/public.js' }, output: { path: BUILD_PATH, // 设置输出目录 filename: '[name].bundle.js', // 输出文件名 }, resolve: { extensions: ['', '.js', '.jsx', '.coffee'] // 配置简写,配置事后,书写该文件路径的时候能够省略文件后缀 }, module: { loaders: [ // loader 扔在这里 ] }, plugins: [ // 插件扔在这里 ] }
没有loader怎么活! ̄へ ̄node
scss-loader的配置同理less,我的比较经常使用lessreact
$ npm install less --save-dev # install less $ npm install css-loader style-loader --save-dev # install style-loader, css-loader $ npm install less less-loader --save-dev # 基于style-loader,css-loader
用来处理图片和字体文件jquery
$ npm install file-loader --save-dev $ npm install url-loader --save-dev
不能写ES6的js不叫jswebpack
$ npm install babel-loader babel-core babel-preset-es2015 --save-dev
// config/webpack.config.js module.exports = { module: { loaders: [ // style & css & less loader { test: /\.css$/, loader: "style-loader!css-loader"}, { test: /\.less$/, loader: "style-loader!css-loader!less-loader")}, // babel loader { test: /\.jsx?$/, exclude: /(node_modules|bower_components)/, loader: ['babel-loader'], query: { presets: ['es2015'] // 若是安装了React的话 // presets: ['react', 'es2015'] } }, // image & font { test: /\.(woff|woff2|eot|ttf|otf)$/i, loader: 'url-loader?limit=8192&name=[name].[ext]'}, { test: /\.(jpe?g|png|gif|svg)$/i, loader: 'url-loader?limit=8192&name=[name].[ext]'}, ] } }
ExtractTextPlugin
分离CSS行内插入一坨CSS是万恶之源 git
-- 我瞎扯的github
# install ExtractTextPlugin $ npm install extract-text-webpack-plugin --save-dev
// config/webpack.config.js const ExtractTextPlugin = require("extract-text-webpack-plugin"); module: { loaders: [ // 把以前的style&css&less loader改成 { test: /\.css$/, loader: ExtractTextPlugin.extract('style-loader', 'css-loader')}, { test: /\.less$/, loader: ExtractTextPlugin.extract('style', 'css!less') }, ] }, plugins: [ // 分离css new ExtractTextPlugin('[name].bundle.css', { allChunks: true }), ]
jQuery
全局变量jQuery
很老土?好吧我还真有点赞成你。。但无疑在必定程度上它仍是很方便的。我把jQuery
设置成全局变量,这样的话有时候就能偷懒用下它了。web
$ npm install jquery --save-dev # 安装 expose-loader $ sudo npm install expose-loader --save
// config/webpack.config.js module: { loaders: [ // expose-loader将须要的变量从依赖包中暴露出来 { test: require.resolve("jquery"), loader: "expose?$! expose?jQuery" } ] }, plugins: [ // 把jquery做为全局变量插入到全部的代码中 // 而后就能够直接在页面中使用jQuery了 new webpack.ProvidePlugin({ $: 'jquery', jQuery: 'jquery', 'window.jQuery': 'jquery' }), ]
CommonsChunkPlugin
抽取公共资源// config/webpack.config.js entry: { jquery: ['jquery'] }, plugins: [ // public sources new webpack.optimize.CommonsChunkPlugin({ // 与 entry 中的 jquery 对应 name: 'jquery', // 输出的公共资源名称 filename: 'common.bundle.js', // 对全部entry实行这个规则 minChunks: Infinity }), ]
UglifyJsPlugin
代码压缩混淆// config/webpack.config.js plugins: [ new webpack.optimize.UglifyJsPlugin({ mangle: { except: ['$super', '$', 'exports', 'require'] //以上变量‘$super’, ‘$’, ‘exports’ or ‘require’,不会被混淆 }, compress: { warnings: false } }) ]
React + Webpack在我内心是个标配,本身也很喜欢React+Redux+Webpack那一套,因此怎么少得了它。npm
# react $ npm install react --save $ npm install react-dom --save # 喜欢redux? $ npm install --save redux # redux $ npm install --save react-redux # 和react配合 $ npm install --save redux-thunk # middleware # 若是已经装了babel能够忽略下面这条 $ npm install babel-loader babel-core babel-preset-es2015 --save-dev # 可是要用React的话必定记得安装下面这个 $ npm install babel-preset-react --save-dev
loaders: [ { test: /\.jsx?$/, exclude: /(node_modules|bower_components)/, loader: ['babel-loader'], query: { presets: ['react', 'es2015'] } } ]
在最新的React(V15)里,若是你按照上面的配置正常使用的话,应该会出现以下的警告:redux
Warning: It looks like you're using a minified copy of the development build of React. When deploying React apps to production, make sure to use the production build which skips development warnings and is faster. See https://fb.me/react-minification for more details.
我记得之前的版本没有这个警告啊?我在开发环境压缩它了?那把UglifyJsPlugin
拿走试试。。结果仍是同样。
最后在github React-issue找到了目前的解决方案:
在Webpack的plugins里添加:
new webpack.DefinePlugin({ "process.env": { NODE_ENV: JSON.stringify("production") } })
而后就没问题了==
若是真的要玩的话,webpack能够有很是多的玩法(看看它插件就知道了)。但webpack终究是一个工具,因此也就没有特别深刻探究它,知道怎么用,够用就行了。