1. 程式人生 > >webpack安裝以及使用

webpack安裝以及使用

webpack安裝

  • 注意:請先安裝node環境

建議大家這樣操作(可以隨時切換映象源)

  • npm install nrm -g // 安裝nrm

  • nrm ls // 檢視映象源

  • nrm use taobao // 選擇淘寶映象,也可以選擇cnpm

  • npm install [email protected] -g

  • 安裝路由
    npm install vue-router --save

  • 安裝css
    npm install css-loader style-loader --save-dev

  • 安裝less和scss (node-sass很難裝,可能要按很多遍)


    npm install less less-loader sass-loader node-sass --save-dev**

  • 圖片&字型安裝
    npm install file-loader url-loader --save-dev

  • HTML安裝
    npm install html-webpack-plugin --save-dev

  • babel安裝
    npm install babel-core babel-loader babel-preset-env --save-dev

  • 單檔案元件vue安裝
    npm install vue --save
    npm install vue-loader vue-template-compiler --save-dev

webpack使用

根據上面的安裝,所需要了解的都在下面webpack.config.js程式碼

  • 在webpack.config.js檔案中配置,程式碼如下
let path = require('path')
let HtmlWebpackPlugin = require('html-webpack-plugin')
const VueLoaderPlugin = require('vue-loader/lib/plugin')
module.exports = {
  // 配置入口檔案
  entry: './app.js',
  // 配置輸出檔案
  output:
{ // 輸出路徑 path: path.join(__dirname, 'dist'), // publicPath表示靜態資源在伺服器上存放的路徑 // publicPath: '/dist', // 輸出檔案的名字 filename: 'bundle.js' }, // module欄位專門用來配置各種載入器的 module: { rules: [ // 配置的是用來解析.css檔案的loader(style-loader和css-loader) { // 1.0 用正則匹配當前訪問的檔案的字尾名是 .css test: /\.css$/, // css-loader用於處理字尾名是.css的檔案 // style-loader用來將處理完的樣式新增到style標籤中去 use: ['style-loader', 'css-loader'] //webpack底層呼叫這些包的順序是從右到左 }, // 處理less { test: /\.less$/, use: [{ loader: 'style-loader' }, { loader: 'css-loader' }, { loader: 'less-loader' }] }, // 處理scss { test: /\.scss$/, use: [{ loader: 'style-loader' }, { loader: 'css-loader' }, { loader: 'sass-loader' }] }, { // 處理圖片 和 字型圖示 test: /\.(png|jpg|gif|eot|svg|ttf|woff)/, use: [{ loader: 'url-loader', options: { // limit引數用來控制圖片展示的形式,如果圖片大於limit的值,就以路徑形式展示,如果小於limit的值,就以base64展示 limit的單位是b limit: 5000 } }] }, // 處理es6語法 { test: /\.js$/, // Webpack2建議儘量避免exclude,更傾向於使用include // exclude: /(node_modules)/, // node_modules下面的.js檔案會被排除 include: [path.resolve(__dirname, 'src')], use: { loader: 'babel-loader' // options裡面的東西可以放到.babelrc檔案中去 // options: { // "presets":["env"] // } } }, // 處理vue單檔案元件 { test: /\.vue$/, loader: 'vue-loader' } ] }, plugins: [ new HtmlWebpackPlugin({ filename: 'index.html', template: 'template.html', title: 'webpack外掛學習' }), new VueLoaderPlugin() ] }
  • 使用vue檔案建立vue元件
<template>
  <div>
    測試頁面
    <p class="myp">{{msg}}</p>
    <router-view></router-view>
  </div>
</template>
<script>
export default {
  data () {
    return {
      msg: 'hello itcast!'
    }
  }
}
</script>
<style>

</style>


  • app.js 引入元件,並將元件渲染到頁面,and路由配置
import Vue from 'vue'
// 1.1 引入vue-router
import VueRouter from 'vue-router'
import App from './App.vue'
import Product from './Product.vue'

// 1.2 呼叫Vue.use()方法啟用路由外掛
Vue.use(VueRouter)

// 1.3 建立路由規則
let router = new VueRouter({
  routes: [
  // 1.4 建立product元件並引入元件
    {name: 'product', path: '/product', component: Product}
  ]
})

new Vue({
  el: '#app',
  // 1.5 注入路由
  router,
  // 1.6 去App.vue單檔案元件中挖坑
  // render函式的作用,用來將指定的元件渲染到#app內部去
  // 引數h是一個函式,它有一個引數,引數就是元件
  render: h => h(App)
})
  • package.json檔案配置
  • “scripts”: {
    “dev”: “webpack-dev-server --inline --hot --open --port 8090”
    }
    設定這個找到檔案位置直接執行 npm run dev 就可以啦
{
  "name": "01-webpack-cli",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "webpack-dev-server --inline --hot --open --port 8090"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

  • options裡面的東西可以放到.babelrc檔案中去,裡面可以配置其他語法,具體是啥我也搞不懂 0.0 (檔名只能是這個 .babelrc )
    { “presets”:[“env”] }