package.json:javascript
{ "name": "leyi", "version": "1.0.0", "main": "index.js", "license": "MIT", "scripts": { "clean": "rimraf ./dist && mkdir dist", "build:dll": "npm run clean && webpack --config ./webpack.dll.config.js", "build": "webpack && webpack-dev-server --inline" }, "devDependencies": { "art-dialog": "^7.0.0", "assets-webpack-plugin": "^3.5.1", "babel-core": "^6.25.0", "babel-loader": "^7.0.0", "babel-polyfill": "^6.23.0", "babel-preset-env": "^1.5.2", "babel-preset-es2015": "^6.24.1", "css-loader": "^0.28.3", "eslint": "^4.1.0", "extract-text-webpack-plugin": "^2.1.0", "file-loader": "^0.11.2", "html-webpack-plugin": "^2.28.0", "mockjs": "^1.0.0", "style-loader": "^0.18.1", "url-loader": "^0.5.8", "webpack": "^2.6.1", "webpack-dev-server": "2.1.0-beta.10" }, "dependencies": { "d3": "^4.9.1", "jquery": "^1.11.3", "save-svg-as-png": "^1.2.0", "simple-undo": "^1.0.1", "underscore": "^1.8.3" } }
webapck.dll.config.js:css
var path = require('path'); var webpack = require('webpack'); module.exports = { entry: { dll: ['babel-polyfill','d3', 'jquery','save-svg-as-png','art-dialog'] }, output: { path:path.join(__dirname,'./dist/js'), filename: '[name].bundle.js',/* output.library 將會定義爲 window.${output.library} */ library: '[name]' }, plugins: [ new webpack.optimize.UglifyJsPlugin({ compress: { warnings: false } }), new webpack.DllPlugin({ /*path 定義 manifest文件生成的位置 [name]的部分由entry的名字替換*/ path: path.join(__dirname, '[name]-manifest.json'), name: '[name]'/*name dll bundle輸出到那個全局變量上和 output.library 同樣便可*/ }) ] };
webapck.config.js: html
'use strict'; var path=require('path'); var webpack =require('webpack'); var ExtractTextPlugin = require("extract-text-webpack-plugin"); var uglifyJsPlugin = webpack.optimize.UglifyJsPlugin; var HtmlWebpackPlugin = require('html-webpack-plugin'); module.exports={ context:path.join(__dirname,'./src'), entry:{ "home":'./pages/home/js/index.js' }, output:{ path:path.join(__dirname,'./dist'), filename:'js/[name].bundle.js' }, module:{ rules: [ { test: /\.js$/, exclude: /(node_modules|bower_components)/, use: { loader: 'babel-loader' } }, { test: /\.css$/, use: ExtractTextPlugin.extract({ fallback: "style-loader", use: "css-loader" }) }, { test: /\.(gif|jpg|png|woff|svg|eot|ttf)\??.*$/, loader: 'url-loader?limit=8192&name=images/[hash:8].[name].[ext]' } ] }, plugins: [ new webpack.DllReferencePlugin({ context: __dirname, manifest: require('./dll-manifest.json'), name: "dll" }), new webpack.ProvidePlugin({ //全局化變量 //當webpack碰到require的第三方庫中出現全局的$、jQeury和window.jQuery時,就會使用node_module下jquery包export出來的東西 $: "jquery", jQuery: "jquery", "window.jQuery": "jquery", "d3":"d3", "_":"underscore", "dialog":"art-dialog", "svg2Png":"save-svg-as-png" }), new ExtractTextPlugin("css/style.css"),//單獨使用link標籤加載css並設置路徑,相對於output配置中的publickPath new webpack.HotModuleReplacementPlugin(), //熱加載 /* new webpack.optimize.CommonsChunkPlugin({ name: "common",// 將公共模塊提取,生成名爲`common`的chunk chunks:["home"],//提取哪些模塊共有的部分,默認全部 //filename: "js/common.js", //minChunks: 2 // 提取至少2個模塊共有的部分 }),*/ //壓縮代碼 編譯的速度會變慢,生產時用 /* new uglifyJsPlugin({ compress: { warnings: false, drop_console: true //刪除console } }),*/ new HtmlWebpackPlugin({ title:'page1',//用來生成頁面的 title 元素 template:"pages/home/home.html",//自定義的html頁(默認支持ejs模板),若是不指定模板,會生成最基本的html結構 filename:'home.html',//輸出的 HTML 文件名,默認是 index.html, 也能夠直接配置帶有子目錄。 hash:true,//生成hash,對於解除 cache 頗有用 inject:'body',//script資源插入模板的位置| 'head' | 'body' | chunks: ['home']//須要引入的chunk,不配置就會引入全部頁面的資源 }) ], devServer:{ contentBase:path.join(__dirname,'./dist'), host: 'localhost', progress:true,//顯示進度 port: 3000, //默認8080 inline: true, hot: true//熱啓動 } };