Skip to content

Commit 1158532

Browse files
committed
2.0.5 更改script注入位置,没有script的情况下会自动注入script,允许配置config.base,优化日志打印顺序,优化template,纠正tsconfig.json
1 parent 9836ecb commit 1158532

15 files changed

Lines changed: 179 additions & 186 deletions

LICENSE.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
The MIT License (MIT)
2-
Copyright © 2024-present bddjr ; Adapted from https://www.npmjs.com/package/vite-plugin-singlefile
2+
Copyright © 2024 bddjr
33

44
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
55

README.md

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,19 @@ export interface Options {
105105
https://bddjr.github.io/vite-plugin-singlefile-compression/
106106

107107
```
108-
vite v6.0.11 building for production...
108+
vite v6.2.6 building for production...
109109
✓ 45 modules transformed.
110110
rendering chunks (1)...
111111
112-
vite-plugin-singlefile-compression 2.0.1 building...
112+
vite-plugin-singlefile-compression 2.0.5 building...
113113
114-
file:///D:/bddjr/Desktop/code/js/vite-plugin-singlefile-compression/test/dist/index.html
115-
101.6 KiB -> 46.39 KiB
114+
file:///D:/code/js/vite-plugin-singlefile-compression/test/dist/index.html
115+
101.63 KiB -> 46.42 KiB
116116
117117
Finish.
118118
119-
dist/index.html 47.50 kB
120-
✓ built in 732ms
119+
dist/index.html 47.53 kB
120+
✓ built in 770ms
121121
```
122122

123123
![](effect.jpg)
@@ -133,10 +133,3 @@ npm i
133133
cd ..
134134
npm run build
135135
```
136-
137-
## License
138-
139-
Using [MIT License](LICENSE.txt).
140-
[src/template](src/template) using [Unlicense](src/template/LICENSE.txt).
141-
142-
Adapted from [vite-plugin-singlefile](https://www.npmjs.com/package/vite-plugin-singlefile).

build.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,3 @@ if (!fs.existsSync(outdir))
2626
for (const i of result.outputFiles) {
2727
fs.writeFileSync(i.path, i.text.replace(/;?\n?$/, ''))
2828
}
29-
30-
fs.copyFileSync(
31-
path.join(inDir, "LICENSE.txt"),
32-
path.join(outdir, "LICENSE.txt")
33-
)

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
{
22
"name": "vite-plugin-singlefile-compression",
3-
"version": "2.0.4",
3+
"version": "2.0.5",
4+
"author": "bddjr",
5+
"license": "MIT",
6+
"description": "",
47
"main": "dist/index.js",
5-
"typings": "dist/index.d.ts",
8+
"types": "dist/index.d.ts",
69
"files": [
710
"dist"
811
],
@@ -11,9 +14,6 @@
1114
"build": "rimraf dist && tsc && node build.js && cd test && npm run build",
1215
"prepublishOnly": "npm run build"
1316
},
14-
"author": "bddjr",
15-
"license": "MIT",
16-
"description": "",
1717
"repository": {
1818
"type": "git",
1919
"url": "git+https://github.com/bddjr/vite-plugin-singlefile-compression.git"

src/cutPrefix.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export function cutPrefix(str: string, prefix: string) {
2+
return str.startsWith(prefix)
3+
? str.slice(prefix.length)
4+
: str
5+
}

src/getTemplate.ts

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
11
import fs from 'fs'
22
import { fileURLToPath } from 'url'
3+
import { compress, compressFormat } from './compress.js'
34

4-
const files = {
5-
base64: fs.readFileSync(
6-
fileURLToPath(import.meta.resolve("./template/base64.js"))
7-
).toString(),
8-
9-
base128: fs.readFileSync(
10-
fileURLToPath(import.meta.resolve("./template/base128.js"))
11-
).toString(),
5+
function r(name: string) {
6+
return fs.readFileSync(
7+
fileURLToPath(import.meta.resolve(`./template/${name}.js`))
8+
).toString()
9+
}
1210

13-
assets: fs.readFileSync(
14-
fileURLToPath(import.meta.resolve("./template/assets.js"))
15-
).toString().split('{"":""}', 2),
11+
function rt(name: string, template: string) {
12+
const s = r(name).split(template, 2)
13+
if (s.length !== 2) throw "s.length!==2"
14+
return s
15+
}
1616

17-
resolve: fs.readFileSync(
18-
fileURLToPath(import.meta.resolve("./template/resolve.js"))
19-
).toString(),
17+
const files = {
18+
base64: r('base64'),
19+
base128: r('base128'),
20+
assets: rt('assets', '{"":""}'),
21+
css: rt('css', '"<style>"'),
22+
icon: rt('icon', '"<icon>"'),
23+
importmeta: rt('importmeta', '"<path>"'),
2024
}
2125

2226
export const template = {
23-
base(script: string, format: string, useBase128: boolean) {
27+
base(script: string, format: compressFormat, useBase128: boolean) {
28+
script = compress(format, script, useBase128)
2429
if (useBase128) {
2530
return files.base128
2631
.replace("<format>", format)
@@ -33,5 +38,13 @@ export const template = {
3338
assets(assetsJSON: string) {
3439
return files.assets.join(assetsJSON)
3540
},
36-
resolve: files.resolve,
41+
css(cssSource: string) {
42+
return files.css.join(JSON.stringify(cssSource))
43+
},
44+
icon(dataURL: string) {
45+
return files.icon.join(JSON.stringify(dataURL))
46+
},
47+
importmeta(p: string) {
48+
return files.importmeta.join(JSON.stringify(p))
49+
},
3750
}

0 commit comments

Comments
 (0)