From 0ea0e8352c39bbb8a354a874a08e4fa1284bceab Mon Sep 17 00:00:00 2001 From: Jixun Wu Date: Thu, 18 Jan 2024 00:59:11 +0000 Subject: [PATCH] build: minify final mjs --- package.json | 4 +++- pnpm-lock.yaml | 45 ++++++++++++++++++++++++++++-------------- scripts/minify-mjs.mjs | 19 ++++++++++++++++++ 3 files changed, 52 insertions(+), 16 deletions(-) create mode 100644 scripts/minify-mjs.mjs diff --git a/package.json b/package.json index 3e9d62d..801e45e 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,8 @@ "type": "module", "scripts": { "start": "vite", - "build": "tsc -p tsconfig.prod.json && vite build && node scripts/write-version.mjs", + "build": "tsc -p tsconfig.prod.json && vite build && pnpm build:finalize", + "build:finalize": "node scripts/write-version.mjs && node scripts/minify-mjs.mjs", "lint": "eslint src --ext ts,tsx --report-unused-disable-directives --max-warnings 0", "format": "prettier -w .", "test": "vitest run", @@ -59,6 +60,7 @@ "jsdom": "^23.0.1", "lint-staged": "^15.2.0", "prettier": "^3.1.1", + "terser": "^5.27.0", "typescript": "^5.3.3", "vite": "^5.0.10", "vite-plugin-pwa": "^0.17.4", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a38f9a3..6393456 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -142,12 +142,15 @@ devDependencies: prettier: specifier: ^3.1.1 version: 3.1.1 + terser: + specifier: ^5.27.0 + version: 5.27.0 typescript: specifier: ^5.3.3 version: 5.3.3 vite: specifier: ^5.0.10 - version: 5.0.10(@types/node@20.10.5)(sass@1.69.5) + version: 5.0.10(@types/node@20.10.5)(sass@1.69.5)(terser@5.27.0) vite-plugin-pwa: specifier: ^0.17.4 version: 0.17.4(vite@5.0.10)(workbox-build@7.0.0)(workbox-window@7.0.0) @@ -159,7 +162,7 @@ devDependencies: version: 3.3.0(vite@5.0.10) vitest: specifier: ^1.1.0 - version: 1.1.0(@types/node@20.10.5)(@vitest/ui@1.1.0)(jsdom@23.0.1)(sass@1.69.5) + version: 1.1.0(@types/node@20.10.5)(@vitest/ui@1.1.0)(jsdom@23.0.1)(sass@1.69.5)(terser@5.27.0) workbox-window: specifier: ^7.0.0 version: 7.0.0 @@ -3362,7 +3365,7 @@ packages: dom-accessibility-api: 0.5.16 lodash: 4.17.21 redent: 3.0.0 - vitest: 1.1.0(@types/node@20.10.5)(@vitest/ui@1.1.0)(jsdom@23.0.1)(sass@1.69.5) + vitest: 1.1.0(@types/node@20.10.5)(@vitest/ui@1.1.0)(jsdom@23.0.1)(sass@1.69.5)(terser@5.27.0) dev: true /@testing-library/react@14.1.2(react-dom@18.2.0)(react@18.2.0): @@ -3667,7 +3670,7 @@ packages: '@babel/plugin-transform-react-jsx-source': 7.23.3(@babel/core@7.23.6) '@types/babel__core': 7.20.5 react-refresh: 0.14.0 - vite: 5.0.10(@types/node@20.10.5)(sass@1.69.5) + vite: 5.0.10(@types/node@20.10.5)(sass@1.69.5)(terser@5.27.0) transitivePeerDependencies: - supports-color dev: true @@ -3690,7 +3693,7 @@ packages: std-env: 3.6.0 test-exclude: 6.0.0 v8-to-istanbul: 9.2.0 - vitest: 1.1.0(@types/node@20.10.5)(@vitest/ui@1.1.0)(jsdom@23.0.1)(sass@1.69.5) + vitest: 1.1.0(@types/node@20.10.5)(@vitest/ui@1.1.0)(jsdom@23.0.1)(sass@1.69.5)(terser@5.27.0) transitivePeerDependencies: - supports-color dev: true @@ -3737,7 +3740,7 @@ packages: pathe: 1.1.1 picocolors: 1.0.0 sirv: 2.0.4 - vitest: 1.1.0(@types/node@20.10.5)(@vitest/ui@1.1.0)(jsdom@23.0.1)(sass@1.69.5) + vitest: 1.1.0(@types/node@20.10.5)(@vitest/ui@1.1.0)(jsdom@23.0.1)(sass@1.69.5)(terser@5.27.0) dev: true /@vitest/utils@1.1.0: @@ -6765,6 +6768,17 @@ packages: source-map-support: 0.5.21 dev: true + /terser@5.27.0: + resolution: {integrity: sha512-bi1HRwVRskAjheeYl291n3JC4GgO/Ty4z1nVs5AAsmonJulGxpSektecnNedrwK9C7vpvVtcX3cw00VSLt7U2A==} + engines: {node: '>=10'} + hasBin: true + dependencies: + '@jridgewell/source-map': 0.3.5 + acorn: 8.11.2 + commander: 2.20.3 + source-map-support: 0.5.21 + dev: true + /test-exclude@6.0.0: resolution: {integrity: sha512-cAGWPIyOHU6zlmg88jwm7VRyXnMN7iV68OGAbYDk/Mh/xC/pzVPlQtY6ngoIH/5/tciuhGfvESU8GrHrcxD56w==} engines: {node: '>=8'} @@ -7065,7 +7079,7 @@ packages: convert-source-map: 2.0.0 dev: true - /vite-node@1.1.0(@types/node@20.10.5)(sass@1.69.5): + /vite-node@1.1.0(@types/node@20.10.5)(sass@1.69.5)(terser@5.27.0): resolution: {integrity: sha512-jV48DDUxGLEBdHCQvxL1mEh7+naVy+nhUUUaPAZLd3FJgXuxQiewHcfeZebbJ6onDqNGkP4r3MhQ342PRlG81Q==} engines: {node: ^18.0.0 || >=20.0.0} hasBin: true @@ -7074,7 +7088,7 @@ packages: debug: 4.3.4 pathe: 1.1.1 picocolors: 1.0.0 - vite: 5.0.10(@types/node@20.10.5)(sass@1.69.5) + vite: 5.0.10(@types/node@20.10.5)(sass@1.69.5)(terser@5.27.0) transitivePeerDependencies: - '@types/node' - less @@ -7097,7 +7111,7 @@ packages: debug: 4.3.4 fast-glob: 3.3.2 pretty-bytes: 6.1.1 - vite: 5.0.10(@types/node@20.10.5)(sass@1.69.5) + vite: 5.0.10(@types/node@20.10.5)(sass@1.69.5)(terser@5.27.0) workbox-build: 7.0.0 workbox-window: 7.0.0 transitivePeerDependencies: @@ -7112,7 +7126,7 @@ packages: '@rollup/plugin-virtual': 3.0.2(rollup@2.79.1) '@swc/core': 1.3.101 uuid: 9.0.1 - vite: 5.0.10(@types/node@20.10.5)(sass@1.69.5) + vite: 5.0.10(@types/node@20.10.5)(sass@1.69.5)(terser@5.27.0) transitivePeerDependencies: - '@swc/helpers' - rollup @@ -7123,10 +7137,10 @@ packages: peerDependencies: vite: ^2 || ^3 || ^4 || ^5 dependencies: - vite: 5.0.10(@types/node@20.10.5)(sass@1.69.5) + vite: 5.0.10(@types/node@20.10.5)(sass@1.69.5)(terser@5.27.0) dev: true - /vite@5.0.10(@types/node@20.10.5)(sass@1.69.5): + /vite@5.0.10(@types/node@20.10.5)(sass@1.69.5)(terser@5.27.0): resolution: {integrity: sha512-2P8J7WWgmc355HUMlFrwofacvr98DAjoE52BfdbwQtyLH06XKwaL/FMnmKM2crF0iX4MpmMKoDlNCB1ok7zHCw==} engines: {node: ^18.0.0 || >=20.0.0} hasBin: true @@ -7159,11 +7173,12 @@ packages: postcss: 8.4.32 rollup: 4.9.1 sass: 1.69.5 + terser: 5.27.0 optionalDependencies: fsevents: 2.3.3 dev: true - /vitest@1.1.0(@types/node@20.10.5)(@vitest/ui@1.1.0)(jsdom@23.0.1)(sass@1.69.5): + /vitest@1.1.0(@types/node@20.10.5)(@vitest/ui@1.1.0)(jsdom@23.0.1)(sass@1.69.5)(terser@5.27.0): resolution: {integrity: sha512-oDFiCrw7dd3Jf06HoMtSRARivvyjHJaTxikFxuqJjO76U436PqlVw1uLn7a8OSPrhSfMGVaRakKpA2lePdw79A==} engines: {node: ^18.0.0 || >=20.0.0} hasBin: true @@ -7209,8 +7224,8 @@ packages: strip-literal: 1.3.0 tinybench: 2.5.1 tinypool: 0.8.1 - vite: 5.0.10(@types/node@20.10.5)(sass@1.69.5) - vite-node: 1.1.0(@types/node@20.10.5)(sass@1.69.5) + vite: 5.0.10(@types/node@20.10.5)(sass@1.69.5)(terser@5.27.0) + vite-node: 1.1.0(@types/node@20.10.5)(sass@1.69.5)(terser@5.27.0) why-is-node-running: 2.2.2 transitivePeerDependencies: - less diff --git a/scripts/minify-mjs.mjs b/scripts/minify-mjs.mjs new file mode 100644 index 0000000..2f513ef --- /dev/null +++ b/scripts/minify-mjs.mjs @@ -0,0 +1,19 @@ +import { minify } from 'terser'; +import { readFileSync, writeFileSync, readdirSync } from 'fs'; + +for (const file of readdirSync('dist/assets')) { + if (!/\.(mjs|js)$/.test(file)) { + continue; + } + + console.log(`minifying ${file}...`); + const isModule = /\.mjs$/.test(file); + + const output = await minify(readFileSync(`dist/assets/${file}`, 'utf-8'), { + compress: true, + mangle: true, + module: isModule, + }); + + writeFileSync(`dist/assets/${file}`, output.code); +}