注:基于webpack3.X的版本,webpack4.X的版本不适用
参考连接:
https://segmentfault.com/a/1190000012789253?utm_source=tag-newest
个人github:
https://github.com/meilulan/webpack-vue-practicehtml
cd webpack-vue-practice npm init -y
“-y”表示将项目设置默认,之后可在package.json中更改vue
npm install --save-dev webpack@3 webpack-dev-server@2 npm i --save-dev vue
index.htmlnode
<!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>基于webpack3.X的vue开发环境</title> </head> <body> </body> </html>
webpack.config.jswebpack
const path = require('path'); const webpack = require('webpack'); module.exports = { }
到目前为止,项目目录的总体结构以下:git
util.jsgithub
export default function say() { console.log("hello webpack!!"); }
main.jsweb
import say from './util'; say();
webpack.config.jsnpm
const path = require('path'); const webpack = require('webpack'); module.exports = { entry: './src/main.js',//项目的入口文件 output: { path: path.resolve(__dirname, './dist'),//项目的打包文件目录 publicPath: '/dist/',//经过devServer访问路径 filename: 'index.js'//打包后的文件名 }, devServer: { historyApiFallback: true,//若是找不到界面就返回默认首页index.html overlay: true//能够在浏览器打开的页面显示终端编译时产生的错误 } }
package.jsonjson
"scripts": { "dev": "webpack-dev-server --open --hot", "build": "webpack --progress --hide-modules" },
index.html
<body> <script src="/dist/build.js"></script> </body>
npm run dev
能够发现浏览器自动打开了一个窗口,在浏览器的控制台里输出"hello webpack"
而且能够发现,咱们随意修改util.js的文件,浏览器会自动刷新
npm run build
能够发现,在项目中自动新建了“/dist/build.js”的路径和文件
main.js
import Vue from 'vue'; const vue = new Vue({ el: "#app", data: { message:"hello webpack-vue" } });
index.html
<div id="app">{{message}}</div>
resolve: {//帮组webpack找到bundle中须要引入的模块代码,这些代码包含在每一个require/import语句中 alias: { 'vue$': 'vue/dist/vue.esm.js' } }
webpack.config.js文件的总体配置以下:
const path = require('path'); const webpack = require('webpack'); module.exports = { entry: './src/main.js',//项目的入口文件 output: { path: path.resolve(__dirname, './dist'),//项目的打包文件目录 publicPath: '/dist/',//经过devServer访问路径 filename: 'build.js'//打包后的文件名 }, devServer: { historyApiFallback: true,//若是找不到界面就返回默认首页index.html overlay: true//能够在浏览器打开的页面显示终端编译时产生的错误 }, resolve: {//帮组webpack找到bundle中须要引入的模块代码,这些代码包含在每一个require/import语句中 alias: { 'vue$': 'vue/dist/vue.esm.js' } } }
注:webpack默认只支持js的模块化,如需其余格式的文件当成模块引入,须要用到webpack的loader解析器
终端
npm install --save-dev css-loader vue-style-loader
终端
npm install --save-dev node-sass sass-loader
module: { rules: [ //css { test: /\.css$/, use: [ "vue-style-loader", "css-loader" ] }, //scss为扩展名的sass { test: /\.scss$/, use: [ "vue-style-loader", "css-loader", "sass-loader" ] }, //sass为扩展名的sass { test: /\.sass$/, use: [ "vue-style-loader", "css-loader", "sass-loader" ] } ] }
在src目录下,新建“assets”目录,并在其下新建common.scss文件,写入如下样式
common.scss
body { color: rgb(0, 128, 0); }
启动后,能够看到浏览器中的文字颜色已改变,说明scss文件已经起做用了
有些浏览器不是很支持ES6的语法,咱们可使用babelES6转换为ES5语法。
npm install --save-dev babel-loader @babel/core @babel/preset-env
module: { rules: [ //babel { test: /\.js$/, loader: 'babel-loader', exclude: /node_modules/ } ] }
exclude:是指不须要使用的文件或目录,相对应的是include(手动指定哪些文件或目录)
.babelrc
{ "presets": [ [ "@babel/preset-env", { "modules": false, "targets": { "browsers": [ ">0.25%", "not op_mini all" ] } } ] ] }
babel的配置具体见babel官网
由于babel-core只是对新引入的语法进行转换,好比箭头函数、块级做用域等
但对新引入的对象,例如Promise、Set等这些只能使用babel的插件:@babel/plugin-transform-runtime
在项目开发环境中引入@babel/plugin-transform-runtime,同时在生产环境引入@babel/runtime
npm install --save-dev @babel/plugin-transform-runtime npm install --save @babel/runtime
在.babelrc配置文件中引入plugin
"plugins": [ [ "@babel/plugin-transform-runtime", { "absoluteRuntime": false, "corejs": false, "helpers": true, "regenerator": true, "useESModules": false } ] ]
.babelrc文件内容总体以下:
{ "presets": [ [ "@babel/preset-env", { "modules": false, "targets": { // The % refers to the global coverage of users from browserslist "browsers": [ ">0.25%", "not op_mini all" ] } } ] ], "plugins": [ [ "@babel/plugin-transform-runtime", { "absoluteRuntime": false, "corejs": false, "helpers": true, "regenerator": true, "useESModules": false } ] ] }
npm install --save-dev file-loader
module: { rules: [ //图片 { test: /\.(png|jpe?g|gif|svg)$/, loader: 'file-loader', options: { name: '[name].[ext]?[hash]' } } ] }
main.js
Vue.component('my-pic', { template: '<img :src="url" />', data() { return { url: require('./img/example.png') } } });
index.html
<div id="app"> <p>{{message}}</p> <my-pic></my-pic> </div>
运行项目就能看到该图片了。
在前面,咱们使用了Vue.component来定义全局的组件
但在实际项目中,更推荐使用单文件组件
npm install --save-dev vue-loader vue-template-compiler
webpack.config.js
const vueLoaderPlugin = require('vue-loader/lib/plugin'); module:{ rules:[ //vue单文件组件 { test:/\.vue$/, loader:'vue-loader', options:{ loaders:{ 'scss':[ 'vue-style-loader', 'css-loader', 'sass-loader' ], 'sass':[ 'vue-style-loader', 'css-loader', 'sass-loader' ] } } } ] }, plugins:[ new vueLoaderPlugin(), ]
App.vue
<template> <div id="app"> <h1>{{msg}}</h1> <img src="./img/example.png" /> <input type="text" v-model="msg" /> </div> </template> <script> import say from "./util"; export default { name: "app", data() { return { msg: "the first vue page" }; }, created() { this.getSay(); }, methods: { async getSay() { let what = await say(); this.msg = what; } } }; </script> <style lang="scss"> #app { h1 { color: red; } } </style>
main.js
import Vue from 'vue'; import App from './App.vue' import './assets/common.scss'; const vue = new Vue({ el: "#app", components: { App }, template: '<App/>' });
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>基于webpack3.X的vue开发环境</title> </head> <body> <div id="app"></div> <script src="/dist/build.js"></script> </body> </html>
运行项目,便可看到页面能正确显示。