zyd-server-build2 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +75 -0
- package/index.js +245 -0
- package/package.json +28 -0
- package/publish.sh +5 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 zz
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# [zyd-server-build2](https://github.com/hfzhae/zyd-server-build2)
|
|
2
|
+
<p>
|
|
3
|
+
<a href="https://github.com/hfzhae/zyd-server-build2/blob/main/LICENSE"><img style="margin-right:5px;" src="https://img.shields.io/badge/license-MIT-grren.svg"></a>
|
|
4
|
+
<a href="https://www.npmjs.com/package/uglify-js"><img style="margin-right:5px;" src="https://img.shields.io/badge/uglifyJs-3.x-blue.svg"></a>
|
|
5
|
+
<a href="https://www.npmjs.com/package/zyd-server-build2"><img style="margin-right:5px;" src="https://img.shields.io/badge/npm-passing-yellow.svg"></a>
|
|
6
|
+
</p>
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
```
|
|
10
|
+
$ npm install -s zyd-server-build2
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quickstart
|
|
14
|
+
>./babelrc
|
|
15
|
+
```json
|
|
16
|
+
{
|
|
17
|
+
"presets": [
|
|
18
|
+
"@babel/preset-env"
|
|
19
|
+
],
|
|
20
|
+
"plugins": [
|
|
21
|
+
[
|
|
22
|
+
"@babel/plugin-proposal-decorators",
|
|
23
|
+
{
|
|
24
|
+
"legacy": true
|
|
25
|
+
}
|
|
26
|
+
]
|
|
27
|
+
]
|
|
28
|
+
}
|
|
29
|
+
```
|
|
30
|
+
>./jsconfig.json
|
|
31
|
+
```json
|
|
32
|
+
{
|
|
33
|
+
"compilerOptions": {
|
|
34
|
+
"experimentalDecorators": true
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
>/package.json
|
|
39
|
+
```json
|
|
40
|
+
{
|
|
41
|
+
"name": "myApp",
|
|
42
|
+
"version": "1.0.0",
|
|
43
|
+
"description": "",
|
|
44
|
+
"main": "index.js",
|
|
45
|
+
"scripts": {
|
|
46
|
+
"build": "node build.js"
|
|
47
|
+
},
|
|
48
|
+
"author": "",
|
|
49
|
+
"license": "ISC"
|
|
50
|
+
}
|
|
51
|
+
>/build.js
|
|
52
|
+
```js
|
|
53
|
+
const zsb = require("zyd-server-build2")
|
|
54
|
+
zsb()
|
|
55
|
+
```
|
|
56
|
+
## Options
|
|
57
|
+
>/build.js
|
|
58
|
+
```js
|
|
59
|
+
const zsb = require("zyd-server-build2")
|
|
60
|
+
zsb({
|
|
61
|
+
src: "..", // 源文件夹, . 代表当前目录, .. 代表上级目录
|
|
62
|
+
dst: "../build", // 目标文件夹, . 代表当前目录, .. 代表上级目录
|
|
63
|
+
noBuildFile: ["../server/config/index.js"], // 不需要混淆压缩的文件(完整路径),文件路径前需要包含src的内容,如实例,会被无改动打包到目标文件夹中
|
|
64
|
+
noBuildDir: ["../server/public/js"]. // 不需要混淆压缩的文件夹(完整路径),文件路径前需要包含src的内容,如实例,会被无改动打包到目标文件夹中
|
|
65
|
+
ignoreDir: [ // 需要忽略的文件夹(完整路径),文件夹路径前需要包含src的内容,如实例,忽略后不会被打包到目标文件夹中
|
|
66
|
+
"../.git",
|
|
67
|
+
"../server/node_modules",
|
|
68
|
+
],
|
|
69
|
+
ignoreFile: ["../.gitmodules"], // 需要忽略的文件(完整路径),文件路径前需要包含src的内容,如实例,忽略后不会被打包到目标文件夹中
|
|
70
|
+
noDeleteDir: [".git", "node_modules"], // 需要忽略删除的文件或文件夹,默认:[".git", "node_modules"]
|
|
71
|
+
copyright: "Powered by zydsoft™", // 版权申明
|
|
72
|
+
})
|
|
73
|
+
```
|
|
74
|
+
## License
|
|
75
|
+
[MIT](https://github.com/hfzhae/zyd-server-build/blob/main/LICENSE)
|
package/index.js
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Powered by zydsoft™
|
|
3
|
+
* 2021-9-14
|
|
4
|
+
* zz
|
|
5
|
+
*/
|
|
6
|
+
const { minify } = require("terser")
|
|
7
|
+
const { exec } = require('child_process')
|
|
8
|
+
const fs = require("fs")
|
|
9
|
+
const path = require("path")
|
|
10
|
+
const count = [0, 0, 0]
|
|
11
|
+
const timer = []
|
|
12
|
+
|
|
13
|
+
function build ({ src, dst, ignoreDir = [], ignoreFile = [], copyright = "", noBuildFile = [], noBuildDir = [], confused = 1 }) {
|
|
14
|
+
if (!src || !dst) {
|
|
15
|
+
return
|
|
16
|
+
}
|
|
17
|
+
ignoreDir.length > 0 && ignoreDir.push(dst)
|
|
18
|
+
fs.readdirSync(src).forEach(file => {
|
|
19
|
+
const _src = src + "/" + file
|
|
20
|
+
const _dst = dst + "/" + file
|
|
21
|
+
const st = fs.statSync(_src)
|
|
22
|
+
if (st.isFile()) {
|
|
23
|
+
count[2] += st.size
|
|
24
|
+
count[0]++
|
|
25
|
+
if (ignore(ignoreFile, _src)) {
|
|
26
|
+
count[2] -= st.size
|
|
27
|
+
console.log(_src, "\x1B[36m=>\x1B[0m", _dst, `\x1B[33m${bytesToSize(st.size)}\x1B[0m`, "\x1B[31mSkip x\x1B[0m")
|
|
28
|
+
count[0]--
|
|
29
|
+
return
|
|
30
|
+
}
|
|
31
|
+
if (path.extname(_src) === ".js") {
|
|
32
|
+
if (ignore(noBuildFile, _src) || confused === 0) {
|
|
33
|
+
console.log(_src, "\x1B[36m=>\x1B[0m", _dst, `\x1B[33m${bytesToSize(st.size)}\x1B[0m`, "\x1B[32m√\x1B[0m")
|
|
34
|
+
readable = fs.createReadStream(_src);
|
|
35
|
+
writable = fs.createWriteStream(_dst);
|
|
36
|
+
readable.pipe(writable);
|
|
37
|
+
} else {
|
|
38
|
+
exec(`npx babel ${_src} --out-file ${_dst}`, (err, stdout, stderr) => {
|
|
39
|
+
if (err) {
|
|
40
|
+
console.error(`执行Babel命令时出错: ${err}`)
|
|
41
|
+
return
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// 输出Babel命令的标准输出和标准错误
|
|
45
|
+
// console.log(`标准输出:${stdout}`);
|
|
46
|
+
// console.error(`标准错误:${stderr}`);
|
|
47
|
+
readable = fs.readFileSync(_src, 'utf-8')
|
|
48
|
+
const result = minify(readable, {
|
|
49
|
+
parse: {
|
|
50
|
+
// parse options
|
|
51
|
+
},
|
|
52
|
+
compress: {
|
|
53
|
+
drop_console: false, // 删除console
|
|
54
|
+
dead_code: true, // 移除没被引用的代码
|
|
55
|
+
drop_debugger: true, // 移除 debugger;
|
|
56
|
+
hoist_funs: true, // 提升函数声明
|
|
57
|
+
join_vars: true, // 合并连续 var 声明
|
|
58
|
+
booleans: true, // 优化布尔运算,例如 !!a? b : c → a ? b : c
|
|
59
|
+
loops: true, // 当do、while 、 for循环的判断条件可以确定是,对其进行优化。
|
|
60
|
+
hoist_vars: true, // (默认 false) -- 提升 var 声明 (默认是false,因为那会加大文件的size)
|
|
61
|
+
properties: false, // (默认 false) — 传一个对象来自定义指明混淆对象属性的选项。
|
|
62
|
+
unsafe_math: true, // (默认 false) -- 优化数字表达式,例如2 * x * 3 变成 6 * x, 可能会导致不精确的浮点数结果。
|
|
63
|
+
unused: true, // 干掉没有被引用的函数和变量。(除非设置"keep_assign",否则变量的简单直接赋值也不算被引用。)
|
|
64
|
+
pure_getters: true, // 默认是 false. 如果你传入true,UglifyJS会假设对象属性的引用(例如foo.bar 或 foo["bar"])没有函数副作用。
|
|
65
|
+
// keep_fnames: true, // 默认 false。传 true来防止压缩器干掉函数名。对那些依赖
|
|
66
|
+
passes: 10, // 默认 1。运行压缩的次数。在某些情况下,用一个大于1的数字参数可以进一步压缩代码大小。注意:数字越大压缩耗时越长。
|
|
67
|
+
global_defs: {
|
|
68
|
+
DEBUG: false
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
mangle: {
|
|
72
|
+
// mangle options
|
|
73
|
+
toplevel: true, // 干掉顶层作用域中没有被引用的函数 ("funcs")和/或变量("vars") (默认是false , true 的话即函数变量都干掉)
|
|
74
|
+
keep_fnames: true, // 默认 false。传 true来防止压缩器干掉函数名。对那些依赖
|
|
75
|
+
eval: true, // 混淆那些在with或eval中出现的名字(默认禁用)。
|
|
76
|
+
properties: {
|
|
77
|
+
// mangle property options
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
format: {
|
|
81
|
+
// format options (can also use `output` for backwards compatibility)
|
|
82
|
+
},
|
|
83
|
+
sourceMap: {
|
|
84
|
+
// source map options
|
|
85
|
+
},
|
|
86
|
+
ecma: 2016, // specify one of: 5, 2015, 2016, etc.
|
|
87
|
+
enclose: false, // or specify true, or "args:values"
|
|
88
|
+
keep_classnames: false,
|
|
89
|
+
keep_fnames: false,
|
|
90
|
+
ie8: false,
|
|
91
|
+
module: true,
|
|
92
|
+
nameCache: null, // or specify a name cache object
|
|
93
|
+
safari10: false,
|
|
94
|
+
toplevel: false
|
|
95
|
+
})
|
|
96
|
+
fs.writeFileSync(_dst, (copyright ? "/* " + copyright + " */" : "") + result.code)
|
|
97
|
+
console.log(_src, "\x1B[36m=>\x1B[0m", _dst, `\x1B[33m${bytesToSize(st.size)}\x1B[0m`, "\x1B[32mCode confused √\x1B[0m")
|
|
98
|
+
})
|
|
99
|
+
count[1]++
|
|
100
|
+
}
|
|
101
|
+
} else {
|
|
102
|
+
console.log(_src, "\x1B[36m=>\x1B[0m", _dst, `\x1B[33m${bytesToSize(st.size)}\x1B[0m`, "\x1B[32m√\x1B[0m")
|
|
103
|
+
readable = fs.createReadStream(_src)
|
|
104
|
+
writable = fs.createWriteStream(_dst)
|
|
105
|
+
readable.pipe(writable)
|
|
106
|
+
}
|
|
107
|
+
// sleep(100)
|
|
108
|
+
// count[3] += fs.statSync(_dst).size
|
|
109
|
+
} else if (st.isDirectory()) {
|
|
110
|
+
if (ignore(ignoreDir, _src)) {
|
|
111
|
+
return
|
|
112
|
+
}
|
|
113
|
+
makeDir(_dst)
|
|
114
|
+
build({ src: _src, dst: _dst, ignoreDir, ignoreFile, copyright, noBuildFile, noBuildDir, confused: ignore(noBuildDir, _src) ? 0 : 1 })
|
|
115
|
+
}
|
|
116
|
+
})
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function ignore (arr, str) {
|
|
120
|
+
let d = false
|
|
121
|
+
arr.forEach(item => {
|
|
122
|
+
item === str && (d = true)
|
|
123
|
+
})
|
|
124
|
+
return d
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function exists (dst, noDeleteDir) {
|
|
128
|
+
if (!fs.existsSync(dst)) {
|
|
129
|
+
makeDir(dst)
|
|
130
|
+
} else {
|
|
131
|
+
// del(dst)
|
|
132
|
+
delDir(dst, noDeleteDir)
|
|
133
|
+
// makeDir(dst)
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function makeDir (dirpath) {
|
|
138
|
+
if (!fs.existsSync(dirpath)) {
|
|
139
|
+
var pathtmp;
|
|
140
|
+
dirpath.split("/").forEach(dirname => {
|
|
141
|
+
if (pathtmp) {
|
|
142
|
+
pathtmp = path.join(pathtmp, dirname);
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
//如果在linux系统中,第一个dirname的值为空,所以赋值为"/"
|
|
146
|
+
if (dirname) {
|
|
147
|
+
pathtmp = dirname;
|
|
148
|
+
} else {
|
|
149
|
+
pathtmp = "/";
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
if (!fs.existsSync(pathtmp)) {
|
|
153
|
+
if (!fs.mkdirSync(pathtmp)) {
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
// } else {
|
|
159
|
+
// deleteFolderFiles(dirpath);
|
|
160
|
+
}
|
|
161
|
+
return true;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const delDir = (url, noDeleteDir) => {
|
|
165
|
+
var files = []
|
|
166
|
+
files = fs.readdirSync(url)
|
|
167
|
+
for (var i = 0; i < files.length; i++) {
|
|
168
|
+
if (!noDeleteDir.find(item => item === files[i])) {
|
|
169
|
+
var path = url + '/' + files[i]
|
|
170
|
+
if (fs.statSync(path).isFile()) {
|
|
171
|
+
fs.unlinkSync(path)
|
|
172
|
+
} else {
|
|
173
|
+
delDir(path, noDeleteDir)
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
function bytesToSize (bytes) {
|
|
181
|
+
if (bytes === 0) return '0 B';
|
|
182
|
+
var k = 1024;
|
|
183
|
+
sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
|
184
|
+
i = Math.floor(Math.log(bytes) / Math.log(k))
|
|
185
|
+
//return (bytes / Math.pow(k, i)) + ' ' + sizes[i];
|
|
186
|
+
return (bytes / Math.pow(k, i)).toPrecision(3) + ' ' + sizes[i];
|
|
187
|
+
//toPrecision(3) 后面保留两位小数,如1.00GB
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* 阻塞函数
|
|
192
|
+
* @param {Number} milliSeconds 毫秒数
|
|
193
|
+
*/
|
|
194
|
+
function sleep (milliSeconds) {
|
|
195
|
+
const startTime = new Date().getTime()
|
|
196
|
+
while (new Date().getTime() < startTime + milliSeconds) { }
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function formatDuration (ms) {
|
|
200
|
+
if (ms < 0) ms = -ms;
|
|
201
|
+
const time = {
|
|
202
|
+
day: Math.floor(ms / 86400000),
|
|
203
|
+
hour: Math.floor(ms / 3600000) % 24,
|
|
204
|
+
minute: Math.floor(ms / 60000) % 60,
|
|
205
|
+
second: Math.floor(ms / 1000) % 60,
|
|
206
|
+
millisecond: Math.floor(ms) % 1000
|
|
207
|
+
};
|
|
208
|
+
return Object.entries(time)
|
|
209
|
+
.filter(val => val[1] !== 0)
|
|
210
|
+
.map(([key, val]) => `${val} ${key}${val !== 1 ? 's' : ''}`)
|
|
211
|
+
.join(', ');
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
module.exports = ({
|
|
215
|
+
src = ".",
|
|
216
|
+
dst = "./build",
|
|
217
|
+
ignoreDir = ["./.git", "./node_modules"],
|
|
218
|
+
ignoreFile = ["./build.js"],
|
|
219
|
+
copyright = "",
|
|
220
|
+
noBuildFile = [],
|
|
221
|
+
noBuildDir = [],
|
|
222
|
+
noDeleteDir = [".git", "node_modules"],
|
|
223
|
+
} = {}) => {
|
|
224
|
+
console.log(`\x1B[33m
|
|
225
|
+
┌─────────────────────┐
|
|
226
|
+
│ Powered by zydsoft™ │
|
|
227
|
+
│ zyd-server-build │
|
|
228
|
+
└─────────────────────┘
|
|
229
|
+
\x1B[0m`)
|
|
230
|
+
console.log("\x1B[36m\x1B[1mStart build...\x1B[0m\x1B[0m")
|
|
231
|
+
timer[0] = new Date()
|
|
232
|
+
exists(dst, noDeleteDir)
|
|
233
|
+
build({ src, dst, ignoreDir, ignoreFile, copyright, noBuildFile, noBuildDir })
|
|
234
|
+
console.log("")
|
|
235
|
+
console.log(`number of files: \x1B[36m${count[0]}\x1B[0m`)
|
|
236
|
+
console.log(`confusion number: \x1B[36m${count[1]}\x1B[0m`)
|
|
237
|
+
console.log(`size: \x1B[36m${bytesToSize(count[2])}\x1B[0m`)
|
|
238
|
+
console.log(`time consuming: \x1B[36m${formatDuration(new Date() - timer[0])}\x1B[0m`)
|
|
239
|
+
// console.log("\x1B[1m\x1B[32mbuild success √\x1B[0m\x1B[0m")
|
|
240
|
+
console.log(`\x1B[1m\x1B[32m
|
|
241
|
+
┌─────────────────┐
|
|
242
|
+
│ build success √ │
|
|
243
|
+
└─────────────────┘
|
|
244
|
+
\x1B[0m\x1B[0m`)
|
|
245
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "zyd-server-build2",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/hfzhae/zyd-server-build2.git"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [],
|
|
14
|
+
"author": "zhangzheng",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/hfzhae/zyd-server-build2/issues"
|
|
18
|
+
},
|
|
19
|
+
"homepage": "https://github.com/hfzhae/zyd-server-build2#readme",
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"@babel/cli": "^7.21.0",
|
|
22
|
+
"@babel/core": "^7.21.4",
|
|
23
|
+
"@babel/node": "^7.20.7",
|
|
24
|
+
"@babel/plugin-proposal-decorators": "^7.21.0",
|
|
25
|
+
"@babel/preset-env": "^7.21.4",
|
|
26
|
+
"terser": "^5.16.8"
|
|
27
|
+
}
|
|
28
|
+
}
|