Skip to content

Commit 37f0fcf

Browse files
committed
fix: support multiple page close #4
1 parent ef86cbc commit 37f0fcf

File tree

7 files changed

+117
-98
lines changed

7 files changed

+117
-98
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ A Vite plugin for index.html that provides minify and EJS template-based functio
1010

1111
**node version:** >=12.0.0
1212

13-
**vite version:** >=2.0.0-beta.64
13+
**vite version:** >=2.0.0
1414

1515
```
1616
yarn add vite-plugin-html -D

README.zh_CN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
**node version:** >=12.0.0
1212

13-
**vite version:** >=2.0.0-beta.64
13+
**vite version:** >=2.0.0
1414

1515
```
1616
yarn add vite-plugin-html -D

example/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"@vitejs/plugin-vue": "^1.1.4",
1313
"@vue/compiler-sfc": "^3.0.5",
1414
"cross-env": "^7.0.3",
15-
"typescript": "^4.1.3",
16-
"vite": "^2.0.0-beta.64"
15+
"typescript": "^4.1.5",
16+
"vite": "^2.0.1"
1717
}
1818
}

example/yarn.lock

+19-19
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,10 @@ estree-walker@^2.0.1:
200200
resolved "https://registry.npmjs.org/estree-walker/-/estree-walker-2.0.2.tgz#52f010178c2a4c117a7757cfe942adb7d2da4cac"
201201
integrity sha512-Rfkk/Mp/DL7JVje3u18FxFujQlTNR2q6QfMSMB7AvCBx91NGj/ba3kCfza0f6dVDbw7YlRf/nDrn7pQrCCyQ/w==
202202

203-
fsevents@~2.1.2:
204-
version "2.1.3"
205-
resolved "https://registry.npmjs.org/fsevents/-/fsevents-2.1.3.tgz#fb738703ae8d2f9fe900c33836ddebee8b97f23e"
206-
integrity sha512-Auw9a4AxqWpa9GUfj370BMPzzyncfBABW8Mab7BGWBYDj4Isgq+cDKtx0i6u9jcX9pQDnswsaaOTgTmA5pEjuQ==
203+
fsevents@~2.3.1:
204+
version "2.3.2"
205+
resolved "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz#8a526f78b8fdf4623b709e0b975c52c24c02fd1a"
206+
integrity sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==
207207

208208
function-bind@^1.1.1:
209209
version "1.1.1"
@@ -419,12 +419,12 @@ resolve@^1.19.0:
419419
is-core-module "^2.1.0"
420420
path-parse "^1.0.6"
421421

422-
rollup@^2.35.1:
423-
version "2.36.1"
424-
resolved "https://registry.npmjs.org/rollup/-/rollup-2.36.1.tgz#2174f0c25c7b400d57b05628d0e732c7ae8d2178"
425-
integrity sha512-eAfqho8dyzuVvrGqpR0ITgEdq0zG2QJeWYh+HeuTbpcaXk8vNFc48B7bJa1xYosTCKx0CuW+447oQOW8HgBIZQ==
422+
rollup@^2.38.5:
423+
version "2.39.0"
424+
resolved "https://registry.npmjs.org/rollup/-/rollup-2.39.0.tgz#be4f98c9e421793a8fec82c854fb567c35e22ab6"
425+
integrity sha512-+WR3bttcq7zE+BntH09UxaW3bQo3vItuYeLsyk4dL2tuwbeSKJuvwiawyhEnvRdRgrII0Uzk00FpctHO/zB1kw==
426426
optionalDependencies:
427-
fsevents "~2.1.2"
427+
fsevents "~2.3.1"
428428

429429
shebang-command@^2.0.0:
430430
version "2.0.0"
@@ -472,10 +472,10 @@ to-fast-properties@^2.0.0:
472472
resolved "https://registry.npmjs.org/to-fast-properties/-/to-fast-properties-2.0.0.tgz#dc5e698cbd079265bc73e0377681a4e4e83f616e"
473473
integrity sha1-3F5pjL0HkmW8c+A3doGk5Og/YW4=
474474

475-
typescript@^4.1.3:
476-
version "4.1.3"
477-
resolved "https://registry.npmjs.org/typescript/-/typescript-4.1.3.tgz#519d582bd94cba0cf8934c7d8e8467e473f53bb7"
478-
integrity sha512-B3ZIOf1IKeH2ixgHhj6la6xdwR9QrLC5d1VKeCSY4tvkqhF2eqd9O7txNlS0PO3GrBAFIdr3L1ndNwteUbZLYg==
475+
typescript@^4.1.5:
476+
version "4.1.5"
477+
resolved "https://registry.npmjs.org/typescript/-/typescript-4.1.5.tgz#123a3b214aaff3be32926f0d8f1f6e704eb89a72"
478+
integrity sha512-6OSu9PTIzmn9TCDiovULTnET6BgXtDYL4Gg4szY+cGsc3JP1dQL8qvE8kShTRx1NIw4Q9IBHlwODjkjWEtMUyA==
479479

480480
uniq@^1.0.1:
481481
version "1.0.1"
@@ -487,17 +487,17 @@ util-deprecate@^1.0.2:
487487
resolved "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"
488488
integrity sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=
489489

490-
vite@^2.0.0-beta.64:
491-
version "2.0.0-beta.64"
492-
resolved "https://registry.npmjs.org/vite/-/vite-2.0.0-beta.64.tgz#245cabc72335911d4cb704545f713d47e880d84f"
493-
integrity sha512-/dR5rOXohhdXfkAIBAjmwX7JmoMWhQc7+VI6/EjyqXQCQzREmfBztLgTk/rPkmsNY87Cd71u9b+nlbcYK4TDCA==
490+
vite@^2.0.1:
491+
version "2.0.1"
492+
resolved "https://registry.npmjs.org/vite/-/vite-2.0.1.tgz#348fc5c0de510aa90bd01ecf87df210ce741b38e"
493+
integrity sha512-x7ZfikjNs+6n4cdvwb9L5r5xBCdjmtmHFHaI4JVR3nAkJbMCK/dynfDWky8/NseZ9Ncz1jVxTQ/Bcf+n1ps1Ww==
494494
dependencies:
495495
esbuild "^0.8.34"
496496
postcss "^8.2.1"
497497
resolve "^1.19.0"
498-
rollup "^2.35.1"
498+
rollup "^2.38.5"
499499
optionalDependencies:
500-
fsevents "~2.1.2"
500+
fsevents "~2.3.1"
501501

502502
vue@^3.0.5:
503503
version "3.0.5"

package.json

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vite-plugin-html",
3-
"version": "2.0.0",
3+
"version": "2.0.1",
44
"description": "A plugin for vite to Minimize index.html and use lodash.template template syntax in index.html",
55
"main": "dist/index.js",
66
"module": "dist/index.mjs",
@@ -36,24 +36,24 @@
3636
},
3737
"homepage": "https://github.com/vbenjs/vite-plugin-html/tree/master/#readme",
3838
"dependencies": {
39-
"ejs": "^3.1.5",
39+
"ejs": "^3.1.6",
4040
"fs-extra": "^9.1.0",
4141
"html-minifier-terser": "^5.1.1"
4242
},
4343
"peerDependencies": {
44-
"vite": ">=2.0.0-beta.32"
44+
"vite": ">=2.0.0"
4545
},
4646
"devDependencies": {
47-
"@types/ejs": "^3.0.5",
48-
"@types/fs-extra": "^9.0.6",
47+
"@types/ejs": "^3.0.6",
48+
"@types/fs-extra": "^9.0.7",
4949
"@types/html-minifier-terser": "^5.1.1",
5050
"@types/jsdom": "^16.2.6",
51-
"@types/node": "^14.14.24",
51+
"@types/node": "^14.14.31",
5252
"conventional-changelog-cli": "^2.1.1",
5353
"prettier": "^2.2.1",
5454
"rimraf": "^3.0.2",
5555
"tsup": "^3.12.1",
56-
"typescript": "^4.1.3",
57-
"vite": "^2.0.0-beta.64"
56+
"typescript": "^4.1.5",
57+
"vite": "^2.0.1"
5858
}
5959
}

src/minifyHtml.ts

+53-34
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,64 @@
11
import { minify, Options as MinifyOptions } from 'html-minifier-terser';
2-
import type { Plugin, ResolvedConfig } from 'vite';
3-
import fs from 'fs-extra';
4-
import path from 'path';
2+
import type { Plugin } from 'vite';
3+
// import fs from 'fs-extra';
4+
// import path from 'path';
55

66
export function minifyHtml(minifyOptions: MinifyOptions | boolean = true): Plugin {
7-
let config: ResolvedConfig;
7+
// let config: ResolvedConfig;
88
return {
99
name: 'vite:minifyHtml',
10-
11-
configResolved(resolvedConfig: ResolvedConfig) {
12-
config = resolvedConfig;
10+
// configResolved(resolvedConfig: ResolvedConfig) {
11+
// config = resolvedConfig;
12+
// },
13+
transformIndexHtml: {
14+
enforce: 'post',
15+
transform(html: string) {
16+
if (!minifyOptions) {
17+
return html;
18+
}
19+
const defaultMinifyOptions = {
20+
collapseBooleanAttributes: true,
21+
collapseWhitespace: true,
22+
minifyCSS: true,
23+
minifyJS: true,
24+
minifyURLs: true,
25+
removeAttributeQuotes: true,
26+
removeComments: true,
27+
removeEmptyAttributes: true,
28+
...(typeof minifyOptions === 'boolean' ? {} : minifyOptions),
29+
};
30+
return minify(html, defaultMinifyOptions);
31+
},
1332
},
33+
// async closeBundle() {
34+
// if (!minifyOptions) {
35+
// return;
36+
// }
1437

15-
async closeBundle() {
16-
if (!minifyOptions) {
17-
return;
18-
}
38+
// // Since the html conversion can also be maintained inside the plug-in, it cannot be guaranteed that the subsequent plug-in will compress the html. So it is changed to compress after packaging
39+
// const defaultMinifyOptions = {
40+
// collapseBooleanAttributes: true,
41+
// collapseWhitespace: true,
42+
// minifyCSS: true,
43+
// minifyJS: true,
44+
// minifyURLs: true,
45+
// removeAttributeQuotes: true,
46+
// removeComments: true,
47+
// removeEmptyAttributes: true,
48+
// ...(typeof minifyOptions === 'boolean' ? {} : minifyOptions),
49+
// };
1950

20-
// Since the html conversion can also be maintained inside the plug-in, it cannot be guaranteed that the subsequent plug-in will compress the html. So it is changed to compress after packaging
21-
const defaultMinifyOptions = {
22-
collapseBooleanAttributes: true,
23-
collapseWhitespace: true,
24-
minifyCSS: true,
25-
minifyJS: true,
26-
minifyURLs: true,
27-
removeAttributeQuotes: true,
28-
removeComments: true,
29-
removeEmptyAttributes: true,
30-
...(typeof minifyOptions === 'boolean' ? {} : minifyOptions),
31-
};
51+
// const { root, build } = config;
52+
// const { outDir, rollupOptions: { input } = {} } = build;
3253

33-
const { root, build } = config;
34-
const { outDir } = build;
35-
const indexHtmlPath = path.resolve(root, outDir, 'index.html');
36-
if (!fs.existsSync(indexHtmlPath)) {
37-
console.log('no such file: ' + indexHtmlPath);
38-
return;
39-
}
40-
let processHtml = fs.readFileSync(indexHtmlPath, 'utf-8');
41-
processHtml = minify(processHtml, defaultMinifyOptions);
42-
fs.writeFile(indexHtmlPath, processHtml);
43-
},
54+
// const indexHtmlPath = path.resolve(root, outDir, 'index.html');
55+
// if (!fs.existsSync(indexHtmlPath)) {
56+
// console.log('no such file: ' + indexHtmlPath);
57+
// return;
58+
// }
59+
// let processHtml = fs.readFileSync(indexHtmlPath, 'utf-8');
60+
// processHtml = minify(processHtml, defaultMinifyOptions);
61+
// fs.writeFile(indexHtmlPath, processHtml);
62+
// },
4463
};
4564
}

yarn.lock

+33-33
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,20 @@
6565
estree-walker "^1.0.1"
6666
picomatch "^2.2.2"
6767

68-
"@types/ejs@^3.0.5":
69-
version "3.0.5"
70-
resolved "https://registry.npmjs.org/@types/ejs/-/ejs-3.0.5.tgz#95a3a1c3d9603eba80fe67ff56da1ba275ef2eda"
71-
integrity sha512-k4ef69sS4sIqAPW9GoBnN+URAON2LeL1H0duQvL4RgdEBna19/WattYSA1qYqvbVEDRTSWzOw56tCLhC/m/IOw==
68+
"@types/ejs@^3.0.6":
69+
version "3.0.6"
70+
resolved "https://registry.npmjs.org/@types/ejs/-/ejs-3.0.6.tgz#aca442289df623bfa8e47c23961f0357847b83fe"
71+
integrity sha512-fj1hi+ZSW0xPLrJJD+YNwIh9GZbyaIepG26E/gXvp8nCa2pYokxUYO1sK9qjGxp2g8ryZYuon7wmjpwE2cyASQ==
7272

7373
7474
version "0.0.39"
7575
resolved "https://registry.npmjs.org/@types/estree/-/estree-0.0.39.tgz#e177e699ee1b8c22d23174caaa7422644389509f"
7676
integrity sha512-EYNwp3bU+98cpU4lAWYYL7Zz+2gryWH1qbdDTidVd6hkiR6weksdbMadyXKXNPEkQFhXM+hVO9ZygomHXp+AIw==
7777

78-
"@types/fs-extra@^9.0.6":
79-
version "9.0.6"
80-
resolved "https://registry.npmjs.org/@types/fs-extra/-/fs-extra-9.0.6.tgz#488e56b77299899a608b8269719c1d133027a6ab"
81-
integrity sha512-ecNRHw4clCkowNOBJH1e77nvbPxHYnWIXMv1IAoG/9+MYGkgoyr3Ppxr7XYFNL41V422EDhyV4/4SSK8L2mlig==
78+
"@types/fs-extra@^9.0.7":
79+
version "9.0.7"
80+
resolved "https://registry.npmjs.org/@types/fs-extra/-/fs-extra-9.0.7.tgz#a9ef2ffdab043def080c5bec94c03402f793577f"
81+
integrity sha512-YGq2A6Yc3bldrLUlm17VNWOnUbnEzJ9CMgOeLFtQF3HOCN5lQBO8VyjG00a5acA5NNSM30kHVGp1trZgnVgi1Q==
8282
dependencies:
8383
"@types/node" "*"
8484

@@ -106,10 +106,10 @@
106106
resolved "https://registry.npmjs.org/@types/node/-/node-14.14.22.tgz#0d29f382472c4ccf3bd96ff0ce47daf5b7b84b18"
107107
integrity sha512-g+f/qj/cNcqKkc3tFqlXOYjrmZA+jNBiDzbP3kH+B+otKFqAdPgVTGP1IeKRdMml/aE69as5S4FqtxAbl+LaMw==
108108

109-
"@types/node@^14.14.24":
110-
version "14.14.24"
111-
resolved "https://registry.npmjs.org/@types/node/-/node-14.14.24.tgz#541c00e4f7c41c8f108bd5b045248b5224c62946"
112-
integrity sha512-6BAlkS4seVjszhrwN0W1WabqWwuJwcYF36Z1rPPyQm80LGRKsIeUPdzI51TezXenjetFNy1gRTpuDn1NBg33LA==
109+
"@types/node@^14.14.31":
110+
version "14.14.31"
111+
resolved "https://registry.npmjs.org/@types/node/-/node-14.14.31.tgz#72286bd33d137aa0d152d47ec7c1762563d34055"
112+
integrity sha512-vFHy/ezP5qI0rFgJ7aQnjDXwAMrG0KqqIH7tQG5PPv3BWBayOPIQNBjVc/P6hhdZfMx51REc6tfDNXHUio893g==
113113

114114
"@types/normalize-package-data@^2.4.0":
115115
version "2.4.0"
@@ -610,10 +610,10 @@ dot-prop@^5.1.0:
610610
dependencies:
611611
is-obj "^2.0.0"
612612

613-
ejs@^3.1.5:
614-
version "3.1.5"
615-
resolved "https://registry.npmjs.org/ejs/-/ejs-3.1.5.tgz#aed723844dc20acb4b170cd9ab1017e476a0d93b"
616-
integrity sha512-dldq3ZfFtgVTJMLjOe+/3sROTzALlL9E34V4/sDtUd/KlBSS0s6U1/+WPE1B4sj9CXHJpL1M6rhNJnc9Wbal9w==
613+
ejs@^3.1.6:
614+
version "3.1.6"
615+
resolved "https://registry.npmjs.org/ejs/-/ejs-3.1.6.tgz#5bfd0a0689743bb5268b3550cceeebbc1702822a"
616+
integrity sha512-9lt9Zse4hPucPkoP7FHDF0LQAlGyF9JVpnClFLFH3aSSbxmyoqINRpp/9wePWJTUl4KOQwRL72Iw3InHPDkoGw==
617617
dependencies:
618618
jake "^10.6.1"
619619

@@ -1686,20 +1686,20 @@ rollup-plugin-dts@^2.0.1:
16861686
optionalDependencies:
16871687
"@babel/code-frame" "^7.10.4"
16881688

1689-
rollup@^2.35.1:
1690-
version "2.36.1"
1691-
resolved "https://registry.npmjs.org/rollup/-/rollup-2.36.1.tgz#2174f0c25c7b400d57b05628d0e732c7ae8d2178"
1692-
integrity sha512-eAfqho8dyzuVvrGqpR0ITgEdq0zG2QJeWYh+HeuTbpcaXk8vNFc48B7bJa1xYosTCKx0CuW+447oQOW8HgBIZQ==
1693-
optionalDependencies:
1694-
fsevents "~2.1.2"
1695-
16961689
rollup@^2.38.0:
16971690
version "2.38.1"
16981691
resolved "https://registry.npmjs.org/rollup/-/rollup-2.38.1.tgz#ecea0f7ce6ef2c1f023fdb79524eb7aeb670ea79"
16991692
integrity sha512-q07T6vU/V1kqM8rGRRyCgEvIQcIAXoKIE5CpkYAlHhfiWM1Iuh4dIPWpIbqFngCK6lwAB2aYHiUVhIbSWHQWhw==
17001693
optionalDependencies:
17011694
fsevents "~2.1.2"
17021695

1696+
rollup@^2.38.5:
1697+
version "2.39.0"
1698+
resolved "https://registry.npmjs.org/rollup/-/rollup-2.39.0.tgz#be4f98c9e421793a8fec82c854fb567c35e22ab6"
1699+
integrity sha512-+WR3bttcq7zE+BntH09UxaW3bQo3vItuYeLsyk4dL2tuwbeSKJuvwiawyhEnvRdRgrII0Uzk00FpctHO/zB1kw==
1700+
optionalDependencies:
1701+
fsevents "~2.3.1"
1702+
17031703
run-parallel@^1.1.9:
17041704
version "1.1.10"
17051705
resolved "https://registry.npmjs.org/run-parallel/-/run-parallel-1.1.10.tgz#60a51b2ae836636c81377df16cb107351bcd13ef"
@@ -2008,10 +2008,10 @@ type-fest@^0.8.1:
20082008
resolved "https://registry.npmjs.org/type-fest/-/type-fest-0.8.1.tgz#09e249ebde851d3b1e48d27c105444667f17b83d"
20092009
integrity sha512-4dbzIzqvjtgiM5rw1k5rEHtBANKmdudhGyBEajN01fEyhaAIhsoKNy6y7+IN93IfpFtwY9iqi7kD+xwKhQsNJA==
20102010

2011-
typescript@^4.1.3:
2012-
version "4.1.3"
2013-
resolved "https://registry.npmjs.org/typescript/-/typescript-4.1.3.tgz#519d582bd94cba0cf8934c7d8e8467e473f53bb7"
2014-
integrity sha512-B3ZIOf1IKeH2ixgHhj6la6xdwR9QrLC5d1VKeCSY4tvkqhF2eqd9O7txNlS0PO3GrBAFIdr3L1ndNwteUbZLYg==
2011+
typescript@^4.1.5:
2012+
version "4.1.5"
2013+
resolved "https://registry.npmjs.org/typescript/-/typescript-4.1.5.tgz#123a3b214aaff3be32926f0d8f1f6e704eb89a72"
2014+
integrity sha512-6OSu9PTIzmn9TCDiovULTnET6BgXtDYL4Gg4szY+cGsc3JP1dQL8qvE8kShTRx1NIw4Q9IBHlwODjkjWEtMUyA==
20152015

20162016
uglify-js@^3.1.4:
20172017
version "3.12.4"
@@ -2041,17 +2041,17 @@ validate-npm-package-license@^3.0.1:
20412041
spdx-correct "^3.0.0"
20422042
spdx-expression-parse "^3.0.0"
20432043

2044-
vite@^2.0.0-beta.64:
2045-
version "2.0.0-beta.64"
2046-
resolved "https://registry.npmjs.org/vite/-/vite-2.0.0-beta.64.tgz#245cabc72335911d4cb704545f713d47e880d84f"
2047-
integrity sha512-/dR5rOXohhdXfkAIBAjmwX7JmoMWhQc7+VI6/EjyqXQCQzREmfBztLgTk/rPkmsNY87Cd71u9b+nlbcYK4TDCA==
2044+
vite@^2.0.1:
2045+
version "2.0.1"
2046+
resolved "https://registry.npmjs.org/vite/-/vite-2.0.1.tgz#348fc5c0de510aa90bd01ecf87df210ce741b38e"
2047+
integrity sha512-x7ZfikjNs+6n4cdvwb9L5r5xBCdjmtmHFHaI4JVR3nAkJbMCK/dynfDWky8/NseZ9Ncz1jVxTQ/Bcf+n1ps1Ww==
20482048
dependencies:
20492049
esbuild "^0.8.34"
20502050
postcss "^8.2.1"
20512051
resolve "^1.19.0"
2052-
rollup "^2.35.1"
2052+
rollup "^2.38.5"
20532053
optionalDependencies:
2054-
fsevents "~2.1.2"
2054+
fsevents "~2.3.1"
20552055

20562056
wordwrap@^1.0.0:
20572057
version "1.0.0"

0 commit comments

Comments
 (0)