使用 webpack 手动搭建 vue 环境
准备
全局安装
webpack webpack-cli
项目安装
yarn add webpack webpack-cli webpack-dev-server -D
yarn add vue@2.6 -S
文件创建
webpack.config.js
webpack 配置文件index.html
模板挂载页面1
<div id="app"></div>
src/index.js
入口文件1
2
3
4
5
6
7import Vue from 'vue'
import App from './App.js'
new Vue({
el: '#app',
render: h => h(App)
})src/App.vue
根组件
webpack.config.js
- 安装
vue-loader
(必须)、html-webpack-plugin
(必须) 、vue-template-compiler
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24const VueLoaderPlugin = require('vue-loader/lib/plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports = {
entry: './src/index.js',
mode: 'product',
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader'
}
]
},
plugins: [
new VueLoaderPlugin(),
new HtmlWebpackPlugin({
template: './index.html'
})
],
devServer: {
port: 3000
}
}
配置 package.json -> script
1 | "scripts": { |
yarn start 启动项目自动打开浏览器
配置 webpack.config.js
1 | const path = require('path') |
其他功能
sass
sass-loader node-sass css-loader style-loader vue-style-loader
webpack.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16module: {
rules: [
{
test: /\.css$/,
use: ['vue-style-loader', 'css-loader']
},
{
test: /\.scss$/,
use: ['vue-style-loader', 'css-loader', 'sass-loader']
},
{
test: /\.sass$/,
use: ['vue-style-loader', 'css-loader', 'sass-loader?indentedSyntax']
}
]
}
可以使用 sass 语法了
1 | @import './valiable.scss'; |
引入图片 file-loader / url-loader
1 | { |
报错问题
[webpack-cli] Invalid configuration object. Webpack has been initialized using a configuration object that does not match the API schema…. configuration.module.rules[1].loader should be a non-empty string.
loader 改为 use
1 | module: { |
[webpack-cli] Error: Cannot find module ‘webpack/lib/RuleSet’
** 可能是版本问题,node 版本、vue-cli 版本、webpack 版本 。。。
1 | "devDependencies": { |
vue中图片引入为[object Module]
vue
使用的是commonjs
语法规范,而file-loader/url-loader
使用的es module
语法规范,解决方法是处理图片时不适用es module
的语法
1 | { |