webpack 與 熱編譯webpack-dev-server

來源:互聯網
上載者:User

標籤:exports   hash   inject   conf   需要   url   1.0   run   class   

為啥我發現每次搭建熱編譯的情況都不同,但這次應該算是最科學的方式了。

  

webpack.config.js 只需要注意加大加粗的地方。這是核心,其他地方只是為了示範實戰環境而已

var webpack = require("webpack");var HtmlWebpackPlugin = require(‘html-webpack-plugin‘);module.exports = {    entry:{        jq  : [                               __dirname + ‘/src/js/jq/jquery-1.9.1.js‘,                              __dirname + ‘/src/js/jq/jquery.uriAnchor-1.1.3.js‘                  ],              app : [               __dirname + ‘/src/js/spa.js‘,               __dirname + ‘/src/js/spa.shell.js‘,               __dirname + ‘/src/js/spa.util.js‘,               __dirname + ‘/src/js/spa.chat.js‘,               __dirname + ‘/src/js/spa.model.js‘,               ‘webpack-dev-server/client?http://127.0.0.1:8896‘                     ]            },    output:{        publicPath : "http://127.0.0.1:8896/",        path       : __dirname + ‘/build/js‘,        filename   : ‘[name].js‘    },    resolve: {        alias: {            css: __dirname + ‘/src/css/‘        }    },    module : {        loaders : [           {test:/\.(eot|woff|woff2|svg|ttf)([\?]?.*)$/,loader:"file" },           {test: /\.css$/,loader: ‘style-loader!css-loader‘},           {test: /\.(png|jpe?g|gif|svg)(\?\S*)?$/,loader: ‘file-loader‘,query: {name: ‘[name].[ext]?[hash]‘}}        ]    },    plugins:[         new HtmlWebpackPlugin({             // filename : __dirname + "/build/spa.html",//使用者後台首頁             filename : "spa.html", // 配合熱編譯故意這樣編寫,這樣才可以訪問localhost:port/spa.html            template : __dirname + ‘/src/spa.html‘,//模板檔案            inject   : ‘head‘,            hash     : true,             chunks   : ["jq","app",‘common‘]         }),         //提取出公用的代碼        new webpack.optimize.CommonsChunkPlugin({            name:"common",              chunks:["jq","app"]        })    ]}

 

package.json 同樣只需要注意加粗加大的地方

{  "name": "test_spa",  "version": "1.0.0",  "description": "",  "main": "index.js",  "scripts": {    "dev": "webpack-dev-server --host 127.0.0.1  --port 8896"  },  "repository": {    "type": "git",    "url": "git+https://github.com/dragon8github/spa.git"  },  "author": "",  "license": "ISC",  "bugs": {    "url": "https://github.com/dragon8github/spa/issues"  },  "homepage": "https://github.com/dragon8github/spa#readme",  "devDependencies": {    "babel-cli": "^6.23.0",    "babel-loader": "^6.3.1",    "babel-preset-es2015": "^6.22.0",    "css-loader": "^0.26.1",    "html-webpack-plugin": "^2.28.0",    "jquery": "^1.9.1",    "style-loader": "^0.13.1",    "url-loader": "^0.5.7",    "webpack": "^2.2.1",    "webpack-dev-server": "^2.3.0"  }}

 

命令列執行 npm run dev 測試

webpack 與 熱編譯webpack-dev-server

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.