"scripts": {
"dev": "webpack-dev-server --inline"
},
$ npm run dev
最好采用配置的方式,修改webpack.config.js
port: 8080,//端口
contentBase: 'build',//指定静态文件的根目录
+ inline:true,//实时刷新
$ touch index.html
$ npm install html-webpack-plugin --save-dev
plugins:[
new HtmlWebpackPlugin({
title:'hello zf', //html的title
template:'./index.html',//html引用的模板
chunks:['entry1'] //需要引入的js
})
]
npm install open-browser-webpack-plugin --save-dev
修改webpack.config.js
+ var openBrowserWebpackPlugin = require('open-browser-webpack-plugin');
+ plugin: [
+ new openBrowserWebpackPlugin({ url: 'http://localhost:8080' })
+ ]
$ npm install extract-text-webpack-plugin --save-dev
+ var ExtractTextPlugin = require("extract-text-webpack-plugin");
+ {
+ test: /\.less/,
+ loader: ExtractTextPlugin.extract("style-loader", "css-loader!less-loader")
+ },
+ {
+ test: /\.css/,
+ loader: ExtractTextPlugin.extract("style-loader", "css-loader")
+ }
plugins: [
+ new ExtractTextPlugin("bundle.css"),
]
var webpack = require('webpack');
- entry: path.resolve(__dirname, 'src/index.js'),//入口文件
+ entry: {
+ index:path.resolve(__dirname, 'src/index.js'),
+ vendor: ['jquery','react'],
+ }
output: {
path: path.resolve(__dirname, 'build'),//输出路径
- filename: 'bundle.js' //输出文件名
+ filename: '[name].js' //输出动态文件名
},
+ new webpack.optimize.CommonsChunkPlugin('vendor', '2.js'),
生成2.js将原有的vendor留下,如果直接写2.js会产生三个文件会留下一个空vendor文件,需要把vendor在塞入到2.js
- filename: '[name].js' //输出文件名
+ filename: '[name].[hash:8].js' //输出文件名
+ new webpack.optimize.UglifyJsPlugin({
+ compress: {
+ warnings: false
+ }
+ }),
+ new webpack.optimize.MinChunkSizePlugin({
+ compress: {
+ warnings: false
+ }
+ }),
+ // 查找相等或近似的模块,避免在最终生成的文件中出现重复的模块
+ new webpack.optimize.DedupePlugin(),
+ // 按引用频度来排序 ID,以便达到减少文件大小的效果
+ new webpack.optimize.OccurenceOrderPlugin(),
+ new webpack.optimize.AggressiveMergingPlugin({
+ minSizeReduce: 1.5,
+ moveToParents: true
+ })
+ function rewriteUrl(replacePath) {//重写url
+ return function (req, opt) {
+ var queryIndex = req.url.indexOf('?');//取得?所在的索引
+ var query = queryIndex >= 0 ? req.url.substr(queryIndex) : "";//取得查询字符串的内容
+ //把proxy的path替换为 '/$1\.json',$1取自path匹配到的真实路径中的第一个分组
+ req.url = req.path.replace(opt.path, replacePath) + query;
+ };
+ }
devServer: {
+ proxy: [
+ {
+ path: /^\/api\/(.*)/, //替换符合此正则的接口路径
+ target: "http://localhost:8080/", //目标域名端口
+ rewrite: rewriteUrl('/$1\.json'), //重新定向到新的地址,$1取自path正则匹配到的真实路径的第一个分组
+ changeOrigin: true //修改来源地址
+ }
+ ]
}
{"name":"zfpx","age":7}
http://localhost:8080/api/book
var definePlugin = new webpack.DefinePlugin({
__DEV__: JSON.stringify(JSON.parse(process.env.BUILD_DEV || 'true')),
__PRERELEASE__: JSON.stringify(JSON.parse(process.env.BUILD_PRERELEASE || 'false'))
});
"publish-mac": "export NODE_ENV=prod && webpack-dev-server -p --progress --colors",
"publish-win": "set NODE_ENV=prod && webpack-dev-server -p --progress --colors"
if (__DEV__) {
console.warn('Extra logging');
}
// ...
if (__PRERELEASE__) {
showSecretFeature();
}
$ npm install expose-loader --save-dev
+ {
+ test: /jquery.js$/,
+ loader: "expose?jQuery"
+ }
+ <script>
+ console.log($);
+ </script>
module.exports = {
+ devtool: 'cheap-module-source-map'