wuweijia / work-life-balance

记录工作与生活
https://github.com/wuweijia/wuweijia.github.io/issues
26 stars 2 forks source link

vue-webpack-101 #37

Open wuweijia opened 6 years ago

wuweijia commented 6 years ago

vue webpack 打包

初始化

  1. mkdir vue-webpack-101
  2. cd vue-webpack-101
  3. npm init (都懂 就不介绍了)
  4. touch .gitignore (都懂 就不介绍了)

装包

目录

构建

app.vue

<template>
  <div id="test">{{test}}</div>
</template>

<script>
export default {
  data() {
    return {
      text: 'abc',
    }
  }
}
</script>

<style>
 #test {
   color: brown;
 }
</style>

index.js (入口文件)

import Vue from "vue";
import App from "./app.vue";

const root = document.createElement('div');
document.body.appendChild(root);

new Vue({
  render: (h) => h(App)
}).$mount(root)

webpack.config.js

const path = require('path');
const { VueLoaderPlugin } = require('vue-loader');  //*1 这里

module.exports = {
  entry: path.join(__dirname, 'src/index.js'),
  output: {
    filename: 'bundle.js',
    path: path.join(__dirname, 'dist')
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader'
      }
    ]
  },
  plugins: [
    new VueLoaderPlugin() //*1 这里
  ]
}

一步一步的解决报错

错误1

1 引入VueLoaderPlugin解决了问题如图 image vue-loader

错误2

image

webpack.config.js 添加如下规则

// this will apply to both plain .css files
// AND <style> blocks in vue files
      {
        test: /\.css$/,
        use: [
          'vue-style-loader',
          'css-loader'
        ]
      }

warning 1

image

webpack.config.js

entry: path.join(__dirname, 'src/index.js'),
  output: {
    filename: 'bundle.js',
    path: path.join(__dirname, 'dist')
  },
  _**mode: 'development',**_

至此,npm run build 终于跑起来了

结果图

image

wuweijia commented 6 years ago

配置开发环境运行项目

安装依赖

npm install --save-dev webpack-dev-server

配置 webpack.config.js

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

module.exports = {
  entry: path.join(__dirname, 'src/index.js'),
  output: {
    filename: 'bundle.js',
    path: path.join(__dirname, 'dist')
  },
  resolve: {
    extensions: [".js", ".json", ".css", '.vue']
  },
  devServer: {
    contentBase: path.join(__dirname, "dist"),
    compress: true,
    port: 9000
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader'
      },
      // this will apply to both plain .js files
      // AND <script> blocks in vue files
      {
        test: /\.js$/,
        loader: 'babel-loader'
      },
      // this will apply to both plain .css files
      // AND <style> blocks in vue files
      {
        test: /\.css$/,
        use: [
          'vue-style-loader',
          'css-loader'
        ]
      }
    ]
  },
  plugins: [
    new VueLoaderPlugin(),
    new HtmlWebpackPlugin({
      template: path.join(__dirname, 'index.html'), // 模板文件
      inject: 'body' // js的script注入到body底部
    })
  ]
}

配置script

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

执行npm run dev

wuweijia commented 6 years ago

image