webpack配置

webpack.config.js
var webpack = require("webpack");
var HtmlWebpackPlugin = require("html-webpack-plugin");
var path = require("path");
module.exports={css

entry: __dirname+"/main.js",
// {
//     index:__dirname+"/index.js",
//    main: __dirname+"/main.js"
// } ,
output:{
    path:__dirname+"/dist",
    filename:"[name].js"
},
module:{
    rules:[
        {
            test:/\.vue$/,
            loader:"vue-loader"
        },
        {
            test:/\.js$/,
            loader:"babel-loader",
            options:{
                "presets":[
                    "es2015"
                ]
            }
        },
        {
            test:/\.css$/,
            use:[
            "style-loader",
            "css-loader"
            ]
        },{
            test:/\.html$/,
            use:[
                "html-loader"
            ]
        },{
            test:/\.png|.jpg|.gif$/,
            loader:"url-loader"
        }
    ]
},
devServer:{
    //告訴服務器從哪裏提供內容。這隻有在您想要提供靜態文件時才須要。例如圖片??
                    contentBase:path.join(__dirname + 'dist'),
    // --告訴服務器觀看由devServer.contentBase選項提供的文件。文件更改將觸發整個頁面從新加載。
                    watchContentBase: true,
                    // --隨全部內容啓用gzip壓縮
                    compress: true,
                    // 模塊的熱加載,必須結合 HotModuleReplacementPlugin使用
    hot: true,  
                    port: 9999,
                    host: "localhost",
    inline:true,
                    //至關於gulp的middleware中間件攔截請求;
                    setup(app) {
                        app.get('/some/path', function(req, res) {
                            console.log(11)
                            res.json({ custom: 'response' });
                        });
                    },
                    // proxy:{
                    //         // "/api": "http://localhost:3000/"
                    //         "/api": "http://localhost:9000"
                    //             //     "/api": {
                    //             //         target: "http://localhost:3000",
                    //             //         pathRewrite: {"^/api" : ""},
                    //             //         secure: false
                    //                 //     }
                    // }
    },

plugins:[html

new webpack.HotModuleReplacementPlugin(),
    // new webpack.optimize.UglifyJsPlugin(),
    new webpack.optimize.CommonsChunkPlugin({
        name: "commons",
        // ( 公共chunk(commnons chunk) 的名稱)
        filename: "commons.js",
        // ( 公共chunk 的文件名)
    }),
    new HtmlWebpackPlugin({
        // filename: 'test.html',
        template: "./index.html"
})
    ]

}vue

package.json
{
"name": "wlr",
"version": "1.0.0",
"description": "//生成文件npm run startr //啓動服務,自動刷新npm run dev",
"main": "index.js",
"scripts": {webpack

"test": "echo \"Error: no test specified\" && exit 1",
"start":"webpack --config webpack.config.js",
"dev": "webpack-dev-server --config webpack.config.js --open"

},
"author": "",
"license": "ISC",
"devDependencies": {git

"babel": "^6.23.0",
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-preset-es2015": "^6.24.1",
"css-loader": "^0.28.7",
"html-loader": "^0.5.1",
"html-webpack-plugin": "^2.30.1",
"style-loader": "^0.19.0",
"url-loader": "^0.6.2",
"vue-loader": "^13.3.0",
"webpack": "^3.8.1",
"webpack-dev-server": "^2.9.3"

}
}
碰見的問題
1.npm run dev 啓動服務,報錯
圖片描述github

或者web

圖片描述

報錯緣由:Webpack與webpack-dev-server版本不兼容致使。webpack是1.13.1,但webpack-dev-server是2.x以上的版本。
解決辦法:將webpack-dev-server卸載掉:npm uninstall webpack-dev-server -g
而後安裝1.15.0版本的webpack-dev-server,就可解決了此問題。:npm install webpack-dev-server@1.15.0 -gnpm

相關文章
相關標籤/搜索