快速开始

  • 全局安装 easywebpack-cli 插件
  1. npm i easywebpack-cli -g
  • 请通过 easy init 命令初始化骨架项目, 根据提示选择 Vue 对应的项目类型即可.
  • 开发: npm run dev
  • 构建: npm run build

从零开始

安装 easywebpack-cli 工具

  1. npm install easywebpack-cli -g

初始化项目

  • 通过 git init 初始化项目
  • 安装项目依赖
  1. npm install vue --save
  2. npm install easywebpack-cli easywebpack-vue
  • 编写 src/index.vue 组件
  1. <template>
  2. <h1>Vue 前端渲染方案</h1>
  3. </template>
  4. <style></style>
  5. <script type="text/babel">
  6. export default{}
  7. </script>
  • 添加 src/app.js 入口文件
  1. import Vue from 'vue';
  2. import Index from './index.vue'
  3. new Vue(Index).$mount('#app');
  • 添加 HTMLWebpackPlugin 模板配置 src/view/layout.html

默认 template 路径文件为 src/view/layout.html 直接存在该文件即可,无需 Webpack 配置。

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title></title>
  5. <meta http-equiv="content-type" content="text/html;charset=utf-8">
  6. <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">
  7. <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
  8. </head>
  9. <body>
  10. <div id="app"></div>
  11. </body>
  12. </html>

Vue3 支持

easywebpack-vue@^5.1.0 开始支持 Vue3 构建

  1. "devDependencies": {
  2. "@vue/compiler-sfc": "^3.1.2",
  3. "easywebpack-cli": "^5.0.0",
  4. "easywebpack-vue": "^5.2.0",
  5. "vue": "^3.0.0",
  6. "vue-loader": "^16.3.0",
  7. "vue-router": "^4.0.0",
  8. "vuex": "^4.0.0",
  9. "vuex-router-sync": "next"
  10. },

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 配置

Css Style

热更新实现

热更新

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')
  ]
};