让前端飞Web前端之路vue

17、webpack从0到1-构建vue项目

2020-03-25  本文已影响0人  ComfyUI

讲下webpack中的对于vue配置,其实vue官方就提供了一套模板vue-webpack-template,我们学习学习,然后基于当前自己的项目来配置下。
git仓库:webpack-demo

1、处理vue

$ npm install vue-loader vue-template-compiler --save-dev
// webpack.common.js
const VueLoaderPlugin = require('vue-loader/lib/plugin')

module.exports = {
  module: {
    rules: [
      // ... 其它规则
      {
        test: /\.vue$/,
        loader: 'vue-loader'
      }
    ]
  },
  plugins: [
    // 请确保引入这个插件!
    new VueLoaderPlugin()
  ]
}

2、写点vue

$ cd chapter17
$ npm install vue --save
  webpack-demo/chapter17
  |- /build
  |- src
-   |- assets
-   |- styles
-   |- content.js
-   |- footer.js
-   |- header.js
-   |- logo.js
+   |- App.vue
    |- index.js
  |- index.html
  |- package.json
  |- postcss.config.js
  |- README.md
<template>
  <div id="app">
    hello world
  </div>
</template>

<style lang="scss">
#app {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}
</style>
import Vue from "vue";
import App from "./App.vue";

Vue.config.productionTip = false;

new Vue({
  render: h => h(App)
}).$mount("#app");
<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title><%= htmlWebpackPlugin.options.title %></title>
</head>

<body>
+    <div id="app"></div>
</body>

</html>

3、关于路由

$ npm install vue-router --save
上一章的src目录 现在的src目录
import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "home",
    component: () => import("../views/home.vue")
  },
  {
    path: "/about",
    name: "about",
    component: () => import("../views/about.vue")
  }
];

const router = new VueRouter({
  routes
});

export default router;

4、问题

 // ...
 {
    test: /\.(png|jpg|gif)$/i,
    use: [
      {
        loader: "url-loader",
        options: {
          limit: 4096,
+         esModule: false
        }
      }
    ]
  }

5、小结

上一篇下一篇

猜你喜欢

热点阅读