Skip to content

Commit

Permalink
chore: minify json and html assets
Browse files Browse the repository at this point in the history
  • Loading branch information
simonwep committed Nov 1, 2024
1 parent f300dc6 commit a7f52cc
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 0 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@eslint/js": "9.13.0",
"@intlify/eslint-plugin-vue-i18n": "^3.0.0",
"@types/ackee-tracker": "5.0.4",
"@types/html-minifier-terser": "7.0.2",
"@types/node": "22.8.1",
"@types/papaparse": "5.3.15",
"@typescript-eslint/parser": "8.11.0",
Expand All @@ -45,6 +46,7 @@
"eslint-plugin-import-x": "4.3.1",
"eslint-plugin-prettier": "5.2.1",
"eslint-plugin-vue": "9.29.1",
"html-minifier-terser": "7.2.0",
"prettier": "3.3.3",
"sass": "1.79.6",
"typescript": "5.6.3",
Expand Down
22 changes: 22 additions & 0 deletions plugins/vite-plugin-minify-html/minifyHtmlPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Plugin } from 'vite';
import { minify } from 'html-minifier-terser';

export const minifyHtmlPlugin = (): Plugin => ({
name: 'vite-plugin-minify',
apply: 'build',
transformIndexHtml: {
order: 'post',
handler(html) {
return minify(html, {
removeComments: true,
collapseWhitespace: true,
collapseBooleanAttributes: true,
removeAttributeQuotes: false,
removeEmptyAttributes: true,
minifyCSS: true,
minifyJS: true,
minifyURLs: true
});
}
}
});
24 changes: 24 additions & 0 deletions plugins/vite-plugin-minify-json/minifyJsonPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Plugin } from 'vite';

export const minifyJsonPlugin = (): Plugin => ({
name: 'vite-plugin-minify-json',
apply: 'build',
enforce: 'pre',
async generateBundle(_, bundle) {
for (const fileName of Object.keys(bundle)) {
if (fileName.endsWith('.json')) {
const asset = bundle[fileName];

if (asset.type !== 'asset') {
continue;
}

if (typeof asset.source === 'string') {
asset.source = JSON.stringify(JSON.parse(asset.source));
} else {
asset.source = JSON.stringify(JSON.parse(Buffer.from(asset.source).toString('utf8')));
}
}
}
}
});
93 changes: 93 additions & 0 deletions pnpm-lock.yaml

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

4 changes: 4 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { defineConfig } from 'vite';
import { optimizeCssModules } from 'vite-plugin-optimize-css-modules';
import { VitePWA } from 'vite-plugin-pwa';
import tsconfigPaths from 'vite-tsconfig-paths';
import { minifyJsonPlugin } from './plugins/vite-plugin-minify-json/minifyJsonPlugin.ts';
import { minifyHtmlPlugin } from './plugins/vite-plugin-minify-html/minifyHtmlPlugin.ts';

export default defineConfig({
envPrefix: ['OCULAR'],
Expand Down Expand Up @@ -36,6 +38,8 @@ export default defineConfig({
plugins: [
tsconfigPaths({ loose: true }),
optimizeCssModules(),
minifyJsonPlugin(),
minifyHtmlPlugin(),
vue({
script: {
defineModel: true
Expand Down

0 comments on commit a7f52cc

Please sign in to comment.