Skip to content

Commit

Permalink
feat(projects): add compress script [添加压缩命令]
Browse files Browse the repository at this point in the history
  • Loading branch information
honghuangdc committed Dec 6, 2022
1 parent 61a43b8 commit be6d431
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .env.production
Expand Up @@ -4,3 +4,5 @@ VITE_COMPRESS=N

# gzip | brotliCompress | deflate | deflateRaw
VITE_COMPRESS_TYPE=gzip

VITE_PWA=N
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -11,6 +11,7 @@ node_modules
.DS_Store
dist
dist-ssr
dist.zip
coverage
*.local
stats.html
Expand Down
15 changes: 4 additions & 11 deletions build/plugins/index.ts
Expand Up @@ -16,24 +16,17 @@ import pwa from './pwa';
* @param viteEnv - 环境变量配置
*/
export function setupVitePlugins(viteEnv: ImportMetaEnv): (PluginOption | PluginOption[])[] {
const plugins = [
vue(),
vueJsx(),
html(viteEnv),
...unplugin(viteEnv),
unocss(),
mock,
progress(),
routerPage(),
pwa()
];
const plugins = [vue(), vueJsx(), html(viteEnv), ...unplugin(viteEnv), unocss(), mock, progress(), routerPage()];

if (viteEnv.VITE_VISUALIZER === 'Y') {
plugins.push(visualizer as PluginOption);
}
if (viteEnv.VITE_COMPRESS === 'Y') {
plugins.push(compress(viteEnv));
}
if (viteEnv.VITE_PWA === 'Y' || viteEnv.VITE_VERCEL === 'Y') {
plugins.push(pwa());
}

return plugins;
}
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -46,9 +46,9 @@
"typecheck": "vue-tsc --noEmit --skipLibCheck",
"lint": "eslint . --fix",
"commit": "czg",
"cz": "czg",
"esno": "esno",
"cleanup": "esno ./scripts/cleanup.ts",
"compress": "esno ./scripts/compress.ts",
"update-pkg": "ncu --deep -u",
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
"release": "standard-version",
Expand Down Expand Up @@ -98,6 +98,7 @@
"@vitejs/plugin-vue": "^3.2.0",
"@vitejs/plugin-vue-jsx": "^2.1.1",
"commitlint": "^17.3.0",
"compressing": "^1.6.2",
"conventional-changelog": "^3.1.25",
"cross-env": "^7.0.3",
"cz-git": "^1.4.0",
Expand Down
120 changes: 120 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions scripts/compress.ts
@@ -0,0 +1,21 @@
import { createWriteStream } from 'fs';
import { zip } from 'compressing';

/**
* 压缩目录或者文件
* @param paths 目录或者文件的路径
* @param destinationName 压缩产物的名称
*/
function zipDirsOrFile(paths: string[], destinationName = 'dist.zip') {
const stream = new zip.Stream();

paths.forEach(path => {
stream.addEntry(path);
});

const destination = createWriteStream(destinationName);

stream.pipe(destination);
}

zipDirsOrFile(['dist']);
2 changes: 2 additions & 0 deletions src/typings/env.d.ts
Expand Up @@ -53,6 +53,8 @@ interface ImportMetaEnv {
readonly VITE_COMPRESS?: 'Y' | 'N';
/** 压缩算法类型 */
readonly VITE_COMPRESS_TYPE?: 'gzip' | 'brotliCompress' | 'deflate' | 'deflateRaw';
/** 是否应用pwa */
readonly VITE_PWA?: 'Y' | 'N';
/** hash路由模式 */
readonly VITE_HASH_ROUTE?: 'Y' | 'N';
/** 是否是部署的vercel */
Expand Down

1 comment on commit be6d431

@vercel
Copy link

@vercel vercel bot commented on be6d431 Dec 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.