webpack + vue最佳實踐

以前用webpack + vue 作項目一直不是很懂,此次有空梳理下,so,讓咱們從新開始,咱們的目的是:javascript

  • 使用commonJs規範編寫面向瀏覽器端的代碼
  • 升級到可使用ES2015書寫規範
  • 使用vue來組織咱們的項目代碼

資料

webpack

經常使用命令

$ webpack --display-error-details //方便出錯時能查閱更詳盡的信息
 
$ webpack --config XXX.js //使用另外一份配置文件(好比webpack.config2.js)來打包
 
$ webpack --watch //監聽變更並自動打包
 
$ webpack -p //壓縮混淆腳本,這個很是很是重要!
 
$ webpack -d //生成map映射文件,告知哪些模塊被最終打包到哪裏了
 
$ webpack --progress //顯示進度

loaders 用於轉換應用程序的資源文件,他們是運行在nodejs下的函數 使用參數來獲取一個資源的來源而且返回一個新的來源(資源的位置)css

npm install style-loader --save-dev
npm install css-loader --save-dev
npm install less -save-dev
npm install less-loader --save-dev

樣式獨立

npm install extract-text-webpack-plugin --save-dev

confightml

var ExtractTextPlugin = require("extract-text-webpack-plugin");
plugins: [
  new ExtractTextPlugin("[name].css")
]
loaders: [
  { test: /\.css$/, loader: ExtractTextPlugin.extract("css") },
  { test: /\.less$/, loader: ExtractTextPlugin.extract("css!less") }
]  

配置外部模塊

有時咱們想引入一個庫,好比vue,若是用webpack打包的話,生成的bundle會比較大。可是經過以下的配置能夠在頁面中引入vue,可是在js文件裏仍是用require的方式聲明。vue

externals: {
  // require("jquery") 是引用自外部模塊的
  // 對應全局變量 jQuery
  vue: 'window.Vue'
}

index.jsjava

let Vue = require("vue");

咱們看下vue的這個模塊在bundle裏是怎麼表現的。node

/*!*****************************!*\
  !*** external "window.Vue" ***!
  \*****************************/
/***/ function(module, exports) {
  module.exports = window.Vue;
/***/ },

ES6

webpack + babel能夠咱們書寫ES6代碼規範的js了。可是須要加入一些babel轉換包jquery

npm install babel-loader babel-core babel-preset-es2015 --save-dev

configandroid

module: {
  //加載器配置
  loaders: [
    { test: /\.js$/, loader: "babel",query: {presets: ['es2015']} }
  ]
},

babel

.babelrc : 該文件用來設置轉碼規則和插件webpack

{
  "presets": ["es2015", "stage-0"],
  "plugins": ["transform-runtime"]
}

 

babel-preset-es2015 : 2015轉碼規則
babel-preset-stage-0/1/2/3 : ES7不一樣階段語法提案的轉碼規則(共有4個階段)
babel-core : API轉換核心文件
babel-plugin-transform-runtime : 語法轉換
babel-polyfill : api polyfill  

Vue

使用vue + webpack來整合項目。這裏須要使用vue-loader,因爲版本問題,vue version1.x請使用^8.0.0的版原本轉換。ios

這裏有一份package.json 的 devDependencies,親測ok

{
  "name": "webpack-demo",
  "version": "1.0.0",
  "description": "",
  "main": "webpack.config.js",
  "scripts": {
    "dev": "webpack --watch -d",
    "publish": "webpack -d -p --progress"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.1.2",
    "babel-loader": "^6.1.0",
    "babel-plugin-transform-runtime": "^6.1.2",
    "babel-preset-es2015": "^6.1.2",
    "babel-runtime": "^5.8.0",
    "css-loader": "^0.23.0",
    "extract-text-webpack-plugin": "^1.0.1",
    "less": "^2.7.1",
    "less-loader": "^2.2.3",
    "vue-hot-reload-api": "^1.2.0",
    "vue-html-loader": "^1.0.0",
    "vue-loader": "^8.0.0",
    "vue-style-loader": "^1.0.0",
    "webpack": "^1.12.2"
  }
}

webpack.config.js

var webpack = require('webpack');
var vue = require('vue-loader')
var commonsPlugin = new webpack.optimize.CommonsChunkPlugin('common.js');
var ExtractTextPlugin = require("extract-text-webpack-plugin");
 
module.exports = {
    //插件項
    plugins: [
        new ExtractTextPlugin("[name].css")
    ],
    //頁面入口文件配置
    entry: {
        index : './src/index.js'
    },
    //入口文件輸出配置
    output: {
        path: './dist/',
        filename: '[name].js'
    },
    module: {
        //加載器配置
        loaders: [
            { test: /\.css$/, loader: ExtractTextPlugin.extract("css") },
            { test: /\.less$/, loader: ExtractTextPlugin.extract("css!less") },
            { test: /\.js$/, loader: "babel",query: {presets: ['es2015']},exclude: /node_modules/ },
            { test: /\.vue$/, loader: 'vue'}
        ]
    },
    vue : {
      loaders: {
        css: ExtractTextPlugin.extract("css"),
        less: ExtractTextPlugin.extract("css!less")
      },
      autoprefixer: { browsers: ["ios_saf >= 7", "android >= 4"] }
    },
    externals: {
        vue: "window.Vue"
    }
};

目錄結構

src
  |____index.css
  |____index.js
  |____vue-mods
       |____index.js
       |____index.less
       |____index.vue

src/index.js

import "./index.css";
import Vue from "vue";
import App from "./vue-mods/index.vue";
addEventListener('DOMContentLoaded', function () {
    new Vue(App).$mount("app");
});
// moduleA.say();

src/vue-mods/index.vue

<style lang="less" src="./index.less"></style>
<template>
    <div class="wrap">
        {{msg}}
    </div>
</template>
<script src="./index.js"></script>

src/vue-mods/index.js

export default {
  data () {
    return {
      msg: 'Hello from Component B!'
    }
  }
}

執行 npm run build 能夠看到 dist 目錄

dist
  |____index.css
  |____index.css.map
  |____index.js
  |____index.js.map

效果圖以下

相關文章
相關標籤/搜索