Skip to content

Commit

Permalink
refactor: add vite-plugin-html. Delete updateHtml related logic
Browse files Browse the repository at this point in the history
  • Loading branch information
anncwb committed Oct 27, 2020
1 parent 7bd0b8e commit 173d402
Show file tree
Hide file tree
Showing 19 changed files with 224 additions and 286 deletions.
15 changes: 14 additions & 1 deletion CHANGELOG.zh_CN.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
## # 2.0.0-rc.5 (2020-10-26)
## Wip

### 🎫 Chores

- 升级 vite 版本为`v1.0.0.rc8`
- vite.config.ts 内部 plugins 抽取
- build 目录结构调整
- 依赖更新

### ✨ Refactor

- 独立出`vite-plugin-html`,并修改相关插入 html 的逻辑

## 2.0.0-rc.5 (2020-10-26)

### ✨ Features

Expand Down
8 changes: 8 additions & 0 deletions README.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
- [Finished features](#finished-features)
- [Developing features](#developing-features)
- [Browser support](#browser-support)
- [Plugins](#plugins)

## Introduction

Expand Down Expand Up @@ -250,3 +251,10 @@ Support modern browsers, Not currently supported ie11,Follow-up consideration
| not support | last 2 versions | last 2 versions | last 2 versions | last 2 versions |

More browsers can view [Can I Use Es Module](https://caniuse.com/?search=ES%20Module)

## Plugins

If these plugins are helpful to you, you can give a star

- [vite-plugin-mock](https://github.com/anncwb/vite-plugin-mock)
- [vite-plugin-html](https://github.com/anncwb/vite-plugin-html)
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
- [已完成功能](#已完成功能)
- [正在开发的功能](#正在开发的功能)
- [浏览器支持](#浏览器支持)
- [插件](#插件-1)
- [加入我们](#加入我们)

## 介绍
Expand Down Expand Up @@ -249,6 +250,13 @@ yarn clean:lib # 删除node_modules,兼容window系统

更多浏览器可以查看 [Can I Use Es Module](https://caniuse.com/?search=ES%20Module)

## 插件

如果这些插件对你有帮助,可以给一个 star 支持下

- [vite-plugin-mock](https://github.com/anncwb/vite-plugin-mock)
- [vite-plugin-html](https://github.com/anncwb/vite-plugin-html)

## 加入我们

`Vue-Vben-Aadmin` 是完全开源免费的项目,在帮助开发者更方便地进行中大型管理系统开发,同时也提供 QQ 交流群(项目刚起步,人数较少,有兴趣的可以加群一起讨论),使用问题欢迎在群内提问。
Expand Down
File renamed without changes.
8 changes: 4 additions & 4 deletions build/script/postBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import { sh } from 'tasksfile';

import { argv } from 'yargs';
import { runBuildConfig } from './buildConf';
import { runUpdateHtml } from './updateHtml';
// import { runUpdateHtml } from './updateHtml';
import { errorConsole, successConsole } from '../utils';
import { startGzipStyle } from '../plugin/gzip/compress';
import { startGzipStyle } from '../vite/plugin/gzip/compress';

export const runBuild = async (preview = false) => {
try {
const argvList = argv._;
if (preview) {
let cmd = `npm run build`;
let cmd = `cross-env NODE_ENV=production vite build`;
await sh(cmd, {
async: true,
nopipe: true,
Expand All @@ -23,7 +23,7 @@ export const runBuild = async (preview = false) => {
if (!argvList.includes('no-conf')) {
await runBuildConfig();
}
await runUpdateHtml();
// await runUpdateHtml();
if (!preview) {
await startGzipStyle();
}
Expand Down
103 changes: 0 additions & 103 deletions build/script/updateHtml.ts

This file was deleted.

File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// https://github.com/luxueyan/vite-transform-globby-import/blob/master/src/index.ts

// TODO 目前还不能监听文件新增及删除 内容已经改变,缓存问题?
// 可以使用,先不打算集成
import { join } from 'path';
import { lstatSync } from 'fs';
import glob from 'glob';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { gzip } from 'zlib';
import { readFileSync, writeFileSync } from 'fs';
import { GzipPluginOptions } from './types';
import viteConfig from '../../../vite.config';
import { readAllFile, getCwdPath, isBuildGzip, isSiteMode } from '../../utils';
import viteConfig from '../../../../vite.config';
import { readAllFile, getCwdPath, isBuildGzip, isSiteMode } from '../../../utils';

export function startGzip(
fileContent: string | Buffer,
Expand Down
File renamed without changes.
File renamed without changes.
76 changes: 76 additions & 0 deletions build/vite/plugin/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import type { Plugin as VitePlugin } from 'vite';
import type { Plugin as rollupPlugin } from 'rollup';

import { createMockServer } from 'vite-plugin-mock';
import ViteHtmlPlugin from 'vite-plugin-html';
import PurgeIcons from 'vite-plugin-purge-icons';

import visualizer from 'rollup-plugin-visualizer';
import gzipPlugin from './gzip/index';

import { hmScript } from '../hm';

const pkg = require('../../../package.json');

import { isDevFn, isProdFn, isSiteMode, ViteEnv, isReportMode, isBuildGzip } from '../../utils';
import { GLOB_CONFIG_FILE_NAME } from '../../constant';

// gen vite plugins
export function createVitePlugins(viteEnv: ViteEnv) {
const { VITE_USE_MOCK, VITE_GLOB_APP_TITLE, VITE_PUBLIC_PATH } = viteEnv;

const vitePlugins: VitePlugin[] = [];

// vite-plugin-html
vitePlugins.push(
ViteHtmlPlugin({
// html title
title: VITE_GLOB_APP_TITLE,
minify: isProdFn(),
options: {
// Package and insert additional configuration files
injectConfig: isProdFn()
? `<script src='${VITE_PUBLIC_PATH || './'}${GLOB_CONFIG_FILE_NAME}?v=${
pkg.version
}-${new Date().getTime()}'></script>`
: '',
// Insert Baidu statistics code
hmScript: isSiteMode() ? hmScript : '',
},
})
);

// vite-plugin-purge-icons
vitePlugins.push(PurgeIcons());

// vite-plugin-mock
if (isDevFn() && VITE_USE_MOCK) {
// open mock
vitePlugins.push(
createMockServer({
ignore: /^\_/,
mockPath: 'mock',
})
);
}
return vitePlugins;
}

// gen rollup plugins
export function createRollupPlugin() {
const rollupPlugins: rollupPlugin[] = [];

if (isProdFn()) {
if (isReportMode()) {
// rollup-plugin-visualizer
rollupPlugins.push(
visualizer({ filename: './build/.cache/stats.html', open: true }) as Plugin
);
}
if (isBuildGzip() || isSiteMode()) {
// rollup-plugin-gizp
rollupPlugins.push(gzipPlugin());
}
}
return rollupPlugins;
}
File renamed without changes.
2 changes: 2 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<!DOCTYPE html>
<html lang="en">
<head>
<%= viteHtmlPluginOptions.hmScript %>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="renderer" content="webkit" />
Expand All @@ -10,6 +11,7 @@
/>
<title></title>
<link rel="icon" href="/favicon.ico" />
<%= viteHtmlPluginOptions.injectConfig %>
</head>
<body>
<div id="app">
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"scripts": {
"bootstrap": "yarn install",
"serve": "esno ./build/script/preserve.ts && cross-env NODE_ENV=development vite",
"build": "cross-env NODE_ENV=production vite build && esno ./build/script/postBuild.ts",
"build": "rimraf dist && cross-env NODE_ENV=production vite build && esno ./build/script/postBuild.ts",
"build:site": "cross-env SITE=true npm run build ",
"build:no-cache": "yarn clean:cache && npm run build",
"report": "cross-env REPORT=true npm run build ",
Expand Down Expand Up @@ -48,7 +48,6 @@
"@purge-icons/generated": "^0.4.1",
"@types/echarts": "^4.8.3",
"@types/fs-extra": "^9.0.2",
"@types/html-minifier": "^4.0.0",
"@types/koa-static": "^4.0.1",
"@types/lodash-es": "^4.17.3",
"@types/mockjs": "^1.0.3",
Expand All @@ -72,7 +71,6 @@
"eslint-plugin-vue": "^7.1.0",
"esno": "^0.2.4",
"fs-extra": "^9.0.1",
"html-minifier": "^4.0.0",
"husky": "^4.3.0",
"koa-static": "^5.0.0",
"less": "^3.12.2",
Expand All @@ -90,6 +88,7 @@
"tasksfile": "^5.1.1",
"ts-node": "^9.0.0",
"typescript": "^4.0.5",
"vite-plugin-html": "^1.0.0-beta.2",
"vite-plugin-mock": "^1.0.4",
"vite-plugin-purge-icons": "^0.4.4",
"vue-eslint-parser": "^7.1.1",
Expand Down
2 changes: 1 addition & 1 deletion src/settings/projectSetting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { ProjectConfig } from '/@/types/config';

import { MenuTypeEnum, MenuThemeEnum, MenuModeEnum } from '/@/enums/menuEnum';
import { ContentEnum, PermissionModeEnum, RouterTransitionEnum } from '/@/enums/appEnum';
import { primaryColor } from '../../build/config/glob/lessModifyVars';
import { primaryColor } from '../../build/config/lessModifyVars';
import { isProdMode } from '/@/utils/env';
// ! 改动后需要清空浏览器缓存
const setting: ProjectConfig = {
Expand Down
Loading

0 comments on commit 173d402

Please sign in to comment.