快速开始
- 全局安装
easywebpack-cli插件
npm i easywebpack-cli -g
- 请通过
easy init命令初始化骨架项目, 根据提示选择 Vue 对应的项目类型即可. - 开发:
npm run dev - 构建:
npm run build
从零开始
安装 easywebpack-cli 工具
npm install easywebpack-cli -g
初始化项目
- 通过
git init初始化项目 - 安装项目依赖
npm install vue --savenpm install easywebpack-cli easywebpack-vue
- 编写
src/index.vue组件
<template><h1>Vue 前端渲染方案</h1></template><style></style><script type="text/babel">export default{}</script>
- 添加
src/app.js入口文件
import Vue from 'vue';import Index from './index.vue'new Vue(Index).$mount('#app');
- 添加 HTMLWebpackPlugin 模板配置
src/view/layout.html
默认 template 路径文件为 src/view/layout.html 直接存在该文件即可,无需 Webpack 配置。
<!DOCTYPE html><html lang="en"><head><title></title><meta http-equiv="content-type" content="text/html;charset=utf-8"><meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui"><link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" /></head><body><div id="app"></div></body></html>
Vue3 支持
easywebpack-vue@^5.1.0 开始支持 Vue3 构建
"devDependencies": {"@vue/compiler-sfc": "^3.1.2","easywebpack-cli": "^5.0.0","easywebpack-vue": "^5.2.0","vue": "^3.0.0","vue-loader": "^16.3.0","vue-router": "^4.0.0","vuex": "^4.0.0","vuex-router-sync": "next"},
Webpack 构建
基于 webpack-cli 模式
// webpack.config.js
const easywebpack = require('easywebpack-vue');
const { webpack, merge } = easywebpack.webpack;
const env = process.env.BUILD_ENV;
const baseWebpackConfig = easywebpack.getWebpackConfig({
env, // 根据环境变量生成对应配置,可以在 npm script 里面配置,支持 dev, test, prod 模式
target : 'web', // target: web 表示只获取前端构建 Webpack 配置
entry:{
index: 'src/index.js'
}
});
// 拿到基础配置, 可以进行二次加工
const webpackConfig = merge(baseWebpackConfig, {
// 自定义配置
})
module.exports = webpackConfig;
本地开发
- 使用 webpack-dev-server :
webpack-dev-server --hot 使用 easywepback-cli:
easy dev --webpack打包编译
使用 webpack-cli :
webpack --mode production --config webpack.config.js- 使用 easywepback-cli:
easy build --webpack
基于 easywebpack-cli 构建模式
添加 webpack.config.js 配置
在项目根目录添加 webpack.config.js 文件, 添加如下配置
const path = require('path');
module.exports = {
target:'web',
framework: 'vue', // 指定用 easywebpack-vue 解决方案, 请在项目中安装该依赖
entry: {
index: 'src/index.js'
}
};
编译文件
easy build dev
easy build test
easy build prod
直接运行
easy dev
# 构建文件并启动本地静态 HTTP Server
easy build --server
运行完成自动打开编译结果页面 : http://127.0.0.1:8888/debug
特性支持
Sass/Less/Stylus 配置
热更新实现
Babel 配置
具体根据实际情况添加相关 babel 插件配置,以下仅仅是举例. 详细配置见:https://www.yuque.com/easy-team/easywebpack/babel
Babel 7 配置
${app_root}/babel.config.js
{
"presets": [
[
"@babel/preset-env",
{
"modules": false
}
]
],
"plugins": [
"@babel/plugin-syntax-dynamic-import",
"@babel/plugin-proposal-object-rest-spread",
]
}
Babel 6 配置
${app_root}/.babelrc
{
"presets": [["env",{ "modules": false }]],
"plugins": [
"transform-object-rest-spread",
"syntax-dynamic-import"
],
"comments": false
}
Autoprefixer
${app_root}/postcss.config.js 具体根据实际情况添加 postcss 配置,以下仅仅是举例:
'use strict';
module.exports = {
plugins: [
require('autoprefixer')
]
};
