簡單配置webpack4 + vue


1.創建webpack4-vue文件夾

mkdir webpack4-vue && cd webpack4-vue

2.初始化npm

npm init -y

3.安裝相關依賴

npm i -D webpack webpack-cli webpack-dev-server vue vue-loader vue-template-compiler html-webpack-plugin

4.在根目錄下創建src文件夾,在src文件夾下創建 index.js  app.vue 文件

index.js

import Vue from 'vue'              // 引入vue
import App from './app.vue'        // 引入app組件

const root = document.createElement('div'); // 根節點
document.body.appendChild(root);

new Vue({
  render: (h) => h(App)             // 將App渲染至根節點
}).$mount(root)  

app.vue

<template>
  <div>{{message}}</div>
</template>

<script>
export default {
  data () {
    return {
      message: "2019-03-31"
    };
  }
};
</script>

5.在根目錄下創建webpack.config.js文件

const path = require('path');
const { VueLoaderPlugin } = require('vue-loader');
const webpack = require('webpack');
const htmlWebpackPlugin = require('html-webpack-plugin')

module.exports = {
  mode: 'development',
  entry: './src/index.js',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist')
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader'        // 處理vue
      }
    ]
  },
  plugins: [
    new VueLoaderPlugin(),
    new htmlWebpackPlugin(),
    new webpack.HotModuleReplacementPlugin(),
  ],
  devServer: {
    contentBase: './dist',
    host: 'localhost',
    port: 8000,
    open: true,
    hot: true
  }
}

對於vue-loader@15.x版本,需要在webpack.config.js中添加 const { VueLoaderPlugin } = require('vue-loader'),否則不起作用

6.修改package.json文件

"scripts": {
    "dev": "webpack-dev-server --config webpack.config.js",
    "build": "webpack --config webpack.config.js"
  },

7.執行 npm run dev,enjoy it

 

參考:https://segmentfault.com/a/1190000013960577

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM