webpack 后 webpack-dev-server ,使用的是webpack的js,不自动刷新
整整折磨了我2天,疯了一样;项目结构如下
1 2 3 4 5 6 7 |
/ --index.html --dist/ --node_modules/ --src/ --package.json --webpack.config.js |
package.json
1 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 |
{ "name": "my-webpack-simple-demo", "description": "A Vue.js project", "version": "1.0.0", "author": "wangfan <251941666@qq.com>", "private": true, "scripts": { "ser": "cross-env NODE_ENV=development webpack-dev-server --open --hot --progress", "dev": "cross-env NODE_ENV=development webpack-dev-server --hot --progress", "build": "cross-env NODE_ENV=production webpack --progress --hide-modules" }, "dependencies": { "element-ui": "^1.3.0-beta.1", "vue": "^2.2.1" }, "devDependencies": { "babel-core": "^6.0.0", "babel-loader": "^6.0.0", "babel-plugin-component": "^0.9.1", "babel-preset-latest": "^6.0.0", "cross-env": "^3.0.0", "css-loader": "^0.25.0", "echarts": "^3.5.4", "echarts-liquidfill": "^1.0.5", "file-loader": "^0.9.0", "node-sass": "^4.5.0", "raw": "^0.1.4", "raw-loader": "^0.5.1", "sass-loader": "^5.0.1", "style-loader": "^0.16.1", "stylus": "^0.54.5", "stylus-loader": "^3.0.1", "vue-echarts": "^2.3.7", "vue-loader": "^11.1.4", "vue-resource": "^1.3.1", "vue-style-loader": "^1.0.0", "vue-template-compiler": "^2.2.1", "vuex": "^2.3.1", "webpack": "^2.2.0", "webpack-dev-server": "^2.2.0" } } |
webpack.config.js
1 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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 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 |
var path = require('path') var webpack = require('webpack') module.exports = { entry: './src/main.js', output: { path: path.resolve(__dirname, './dist'), publicPath: './dist/', filename: 'build.js' }, module: { rules: [ { test: /\.vue$/, loader: 'vue-loader', options: { loaders: { // Since sass-loader (weirdly) has SCSS as its default parse mode, we map // the "scss" and "sass" values for the lang attribute to the right configs here. // other preprocessors should work out of the box, no loader config like this necessary. 'scss': 'vue-style-loader!css-loader!sass-loader', 'sass': 'vue-style-loader!css-loader!sass-loader?indentedSyntax' } // other vue-loader options go here } }, { test: /\.css$/, loader: 'style-loader!css-loader' }, { test: /\.js$/, loader: 'babel-loader', exclude: /node_modules/ }, { test: /\.(eot|svg|ttf|woff|woff2)(\?\S*)?$/, loader: 'file-loader' }, { //test: /\.(png|jpg|gif|svg)$/, test: /\.(png|jpe?g|gif|svg)(\?\S*)?$/, loader: 'file-loader', options: { name: '[name].[ext]?[hash]' } } ] }, resolve: { alias: { 'vue$': 'vue/dist/vue.esm.js' } }, devServer: { historyApiFallback: true, noInfo: true, hot: true, inline: true, proxy:{ '/report': { target: 'http://www.vking.com/livezilla/report.php', //secure: false } } }, performance: { hints: false }, devtool: '#eval-source-map' } if (process.env.NODE_ENV === 'production') { module.exports.devtool = '#source-map' // http://vue-loader.vuejs.org/en/workflow/production.html module.exports.plugins = (module.exports.plugins || []).concat([ new webpack.DefinePlugin({ 'process.env': { NODE_ENV: '"production"' } }), new webpack.optimize.UglifyJsPlugin({ sourceMap: true, compress: { warnings: false } }), new webpack.LoaderOptionsPlugin({ minimize: true }) ]) } |
刚开始使用这份配置,一切顺利,dev开发好后build,上传服务器,一切正常;
当我在回到run dev后,发现它一直再使用了dist下的js,于是折腾了2天,改js名字、改dist目录名、各种找、各种改,结果都不行;
最后机缘巧合下改了webpack.config.js 第八行,将publicPath: ‘./dist/’ 改为了publicPath: ‘dist/’ ;
npm run dev 时,竟然神奇的正常了,真是撞大运编程啊
此时npm run build将找不见dist下的资源,使用以下方式判断环境
publicPath: process.env.NODE_ENV === ‘production’?’./dist/’:’/dist/’
npm run build 时必须把 npm run dev停掉