項目名稱: vue-initcss
npm i -D webpack
webpack.config.js
基礎配置html
進階配置vue
module.exports = { enter: {}, module: {}, plugins: [], output: {} }
enter: { app: './app/js/main.js' }
module: { rules: [ { test: /\.html$/, use: [ { loader: 'html-loader' } ] }, { test: /\.vue$/, use: [ { loader: 'vue-loader' } ] }, { test: /\.scss$/, use: [ { loader: 'style-loader' }, { loader: 'css-loader', options: { module: true } }, { loader: 'sass-loader' }, ] }, ] },
options: { module: true }
開啓css module//在webpack.config.js頂部引入path const path = require('path');
output: { filename: '[name].min.js', path: path.resolve(_dirname, 'dist') } }
配置好了 咱們開始安裝loadersnode
npm i -D html-loader vue-loader style-loader css-loader sass-loader
若是有loader安裝不成功請再單個安裝它,或者換用cnpmwebpack
到這一步咱們的基礎配置已經作好,代碼以下:git
module.exports = { enter: { app: './app/js/main.js' }, module: { rules: [ { test: /\.html$/, use: [ { loader: 'html-loader' } ] }, { test: /\.vue$/, use: [ { loader: 'vue-loader' } ] }, { test: /\.scss$/, use: [ { loader: 'style-loader' }, { loader: 'css-loader', options: { module: true } }, { loader: 'sass-loader' }, ] }, ] }, plugins: [], output: { filename: '[name].min.js', path: path.resolve(_dirname, 'dist') } }
devServer: { contentBase: path.join(__dirname, 'dist'), compress: true, port: 9000 }
配置好了 咱們開始安裝它github
npm i -D webpack-dev-server
home/index.vueweb
<template> <div id="home"> <h1>首頁</h1> <p>123123<p> </div> </template> <script> export default {} </script> <style lang="scss" scoped> .home { color: red; font-size: 80px; p { color: blue } } </style>
router/index.jsvue-router
import Vue from "vue" import Router from "vue-router" import Home from "../home/index.vue" Vue.use(Router); export default new Router({ routes: [{ path: '/', name: 'home', component: Home }] })
App.vuenpm
<template> <div id="app"> <router-view></router-view> </div> </template> <script> export default { name: 'app' }; </script> <style lang="scss" scoped> </style>
main.js
import Vue from 'vue' import App from './App.vue' import router from './router' Vue.config.productionTip = false; new Vue({ router, render: h => h(App) }).$mount("#app")
咱們還須要安裝 vue 和vue router
npm i vue vue-router
還須要安裝兩個依賴
npm i -D html-webpack-plugin clean-webpack-plugin
webpack.config.js頂部加入以下代碼
const HtmlWebpackPlugin = require('html-webpack-plugin'); const { CleanWebpackPlugin } = require('clean-webpack-plugin'); // 注意這裏的寫法, 這樣寫 const CleanWebpackPlugin 會報錯
官網文檔解釋:HtmlWebpackPlugin簡化了HTML文件的建立,以便爲你的webpack包提供服務。這對於在文件名中包含每次會隨着編譯而發生變化哈希的 webpack bundle 尤爲有用。 你可讓插件爲你生成一個HTML文件,使用lodash模板提供你本身的模板,或使用你本身的loader。另外你能夠在github查看這個項目的詳細配置。
配置plugins
plugins: [ new CleanWebpackPlugin(), new HtmlWebpackPlugin({ template: './views/index.html' }) ],
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <div id="app"></div> </body> </html>
package.json 加入 "start": "webpack-dev-server --open"
"scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start": "webpack-dev-server --open" },
跑完發現一大堆報錯
npm i vue-loader-plugin -S
webpack.config.js
const VueLoaderPlugin = require('vue-loader/lib/plugin') module.exports = { // ... plugins: [ new VueLoaderPlugin() ] }
npm install -D px2rem-loader
module: { rules: [ { test: /\.html$/, use: 'html-loader' }, { test: /\.vue$/, use: 'vue-loader' }, { test: /\.scss$/, use: [ 'vue-style-loader', 'css-loader', { loader: 'px2rem-loader', options: { remUnit: 75, remPrecision: 6 } }, 'sass-loader' ] }, ] },
這部分爲何這麼配置,參考了Vue官方文檔 -> 單文件組建 -> 針對高級用戶 -> VueLoader
原來的webpack3.x須要在vue-loader 下配置css 和 sass 並配置 px2rem。
// ... { test: /\.scss$/, use: [ 'vue-style-loader', + { + loader: 'css-loader', + options: { + modules: true, + localIdentName: '[local]_[hash:base64:8]' + } + }, { loader: 'px2rem-loader', options: { remUnit: 75, remPrecision: 6 } }, 'sass-loader' ] },
若是你不知道如何使用css module 請參閱Vue官方文檔 -> 單文件組建 -> 針對高級用戶 -> VueLoader -> css module
npm install -D mini-css-extract-plugin
{ test: /\.scss$/, use: [ MiniCssExtractPlugin.loader, { loader: 'css-loader', options: { modules: true } }, { loader: 'px2rem-loader', options: { remUnit: 75, remPrecision: 6 } }, 'sass-loader' ] },
plugins: [ // ... new MiniCssExtractPlugin({ filename: 'style.css' }) ]
咱們須要使用webpack的DefinePlugin建立一個在編譯時能夠配置的全局常量。在webpack.config.js頭部引入webpack
const webpack = require('webpack');
接下來咱們把module.exports的值改成箭頭函數,並傳入一個參數env
module.exports = env => { if (!env) { env = {} } return { // 原來的配置 } }
咱們先來作一個示例,例如咱們在開發環境不須要css提取
module.exports = env => { if (!env) { env = {} } let plugins = [ new CleanWebpackPlugin(), new HtmlWebpackPlugin({ template: './views/index.html' }), new VueLoaderPlugin(), ]; if (env.production) { plugins.push( new webpack.DefinePlugin({ 'process.env': { NODE_ENV: 'production' } }), new MiniCssExtractPlugin({ filename: 'style.css' }) ) }
對應的咱們還有修改部分原來的代碼
{ test: /\.scss$/, use: [ * env.production?MiniCssExtractPlugin.loader:'vue-style-loader', { loader: 'css-loader', options: { modules: true } }, { loader: 'px2rem-loader', options: { remUnit: 75, remPrecision: 6 } }, 'sass-loader' ] },
以及原來的plugins配置咱們直接將它的值變爲咱們上面定義的plugins。
package.json中咱們須要添加命令
"scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start": "webpack-dev-server --open", "watch": "webpack --watch", "build" : "webpack --env.production" },
注意咱們給webpack 傳遞了參數,咱們就是利用這個參數來區分環境。
npm start
控制檯咱們能夠看到
css樣式以style標籤插入,並無被提取,說明MiniCssExtractPlugin插件沒有運行
npm run build
運行打包後的index.html,css樣式以link標籤插入,說明css被提取合併爲一個文件,說明生產環境下MiniCssExtractPlugin插件運行了
上面是的作法看起來更好理解,webpack4中咱們能夠直接利用mode來區分開發環境和生產環境。頭部咱們不須要引入webpack了, 由於咱們不須要依賴 DefinePlugin。
配置中新增:
mode: 'development' //默認是 development
module.exports = (env, argv) => { if (argv.mode === 'production') { //... } return config; };
npm i eslint -D
eslint支持多種格式的配置文件,同時支持把配置直接寫在package.json中,咱們直接在寫在package.json中,如何配置呢?
vue項目能夠直接使用vue官方推薦的插件
npm i eslint-plugin-vue -D
package.json添加以下:
{ // 其餘配置 "eslintConfig": { "root": true, "parserOptions": { "ecmaVersion": 2017 }, "extends": [ "mysticatea", "mysticatea/modules", "plugin:vue/recommended" ], "plugins": [ "node" ], "env": { "browser": false }, "globals": { "applicationCache": false, "atob": false, "btoa": false, "console": false, "document": false, "location": false, "window": false }, "rules": { "node/no-extraneous-import": "error", "node/no-missing-import": "error", "node/no-unpublished-import": "error", "vue/html-indent": [ "error", 4 ], "vue/max-attributes-per-line": "off" } }, "eslintIgnore": [ "node_modules", "webpack.config.js" ] }