搭建Vue2+Vuex+Webpack+Pug(jade)+Stylus环境

 

 

 1、开发环境配置css

开始以前,假设你已经安装了最新版本的 node 和 npm。html

全局安装 vue-cli 和 webpack :vue

npm install vue-cli webpack -g

建立工程;工程名字不能用中文,英文也建议小写。node

vue init webpack 项目名

按照步骤一步一步执行,须要你添加 Project nameProject descriptionAuthor.webpack

而后使用 npm install 安装官方库。web

而后使用npm run dev运行咱们的项目后浏览器会自动弹出,并展现如下页面。vue-router

能够根据页面给咱们的这八个连接得到咱们想要的学习资源。vuex

接下来安装Vue全家桶。vue-cli

npm install vue-router vue-resource vuex --save

以下修改src/main.js :npm

import Vue from 'vue'
import VueResource from 'vue-resource'
import VueRouter from 'vue-router'
import Vuex from 'vuex'

import App from './App.vue'

Vue.use(VueResource)
Vue.use(VueRouter)
Vue.use(Vuex)

new Vue({
  el: '#app',
  render: h => h(App)
})

安装html模板语言pug(jade)

npm install pug pug-loader pug-filters --save

安装css 预编译语言stylus

npm install style-loader stylus stylus-loader --save
# 或者你习惯用sass或less
# npm install style-loader node-sass sass-loader --save
# npm install style-loader less less-loader --save

打开webpack.config.js,配置loader:

 

module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: {
          loaders: {
          }
          // other vue-loader options go here
        }
      },
      {
        test: /\.pug$/,
        loader: 'vue-html!pug-html'
      },
      {
        test: /\.styl/,
        loader: "style-loader!css-loader!stylus-loader"
      },
...

生成html文件,安装插件` html-webpack-plugin  `。

npm install html-webpack-plugin --save
var path = require('path')
var webpack = require('webpack')
var HtmlWebpackPlugin = require('html-webpack-plugin')

module.exports = {
  entry: './src/main.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'js/build.js',
  },

...

  plugins: [
    new HtmlWebpackPlugin({
      title: 'Charles 的我的主页',
      filename: 'index.html', //生成后的文件名,默认为 index.html.
      template: 'index.html',  //自定义的模板文件
      inject:'body' //script标签位于html文件的 body 底部
    }),
  ]
}

提取CSS,提取CSS的插件叫`extract-text-webpack-plugin`,这个不是webpack自带的,须要本身安装。

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

原来的`style-loader`就要修改了,毕竟咱们的目标是将CSS提取出来而不是放在head中, 而且vue文件中<style>里的样式也要提取出来。

var HtmlWebpackPlugin = require('html-webpack-plugin')
var ExtractTextPlugin = require("extract-text-webpack-plugin")
module.exports = {
...

  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: {
          loaders: {
            stylus: ExtractTextPlugin.extract("css-loader!stylus-loader")
          }
        }
      },
      {
        test: /\.pug$/,
        loader: 'vue-html!pug-html'
      },
      {
        test: /\.styl/,
        loader: ExtractTextPlugin.extract("css-loader!stylus-loader")
      },
...

  plugins: [
    new HtmlWebpackPlugin(),
    new ExtractTextPlugin("css/style.css")
  ]