-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
291 lines (280 loc) · 9.33 KB
/
gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
const pkg = require('./package.json');
const typescript = require('rollup-plugin-typescript2');
const path = require('path');
const fs = require('fs');
// npm install del@3.0.0 --save-dev
const del = require("del");
// npm install gulp --save-dev
const gulp = require('gulp');
const rollup = require('rollup');
// npm install rollup-plugin-node-resolve --save-dev
const resolve = require('rollup-plugin-node-resolve');
// npm install rollup-plugin-commonjs --save-dev
const commonjs = require('rollup-plugin-commonjs');
// npm install rollup-plugin-cleanup --save-dev
const cleanup = require('rollup-plugin-cleanup');
const child_process = require("child_process");
// npm install gulp-uglify-es --save-dev
const uglify = require('gulp-uglify-es').default;
// npm install remove-internal --save-dev
// const internal = require('remove-internal');
const ts = require("typescript");
// npm install gulp-rename --save-dev
const rename = require('gulp-rename');
// npm install --save-dev gulp-strip-comments
const strip = require('gulp-strip-comments');
const workSpaceDir = '.'
function clear(done) {
const delList = ['./build/**'];
del(delList, { force: true }).then(paths => {
done();
}).catch((err) => {
throw err;
})
}
const tsCongfig = ["./tsconfig.cjs.json", "./tsconfig.es.json"]
function tsc(done) {
let mark = 0;
let _result = 0;
const start = function (result) {
mark--;
_result += result;
if (!mark) {
if (!_result) {
//进程全部执行完毕
console.log("tsc success!");
done();
}
}
};
for (let i = 0; i < tsCongfig.length; i++) {
mark++;
const cmd = ["-b", tsCongfig[i]];
const tscurl = path.join('', "./node_modules/.bin/tsc.cmd");
child_process.execFile(tscurl, cmd, (err, stdout, stderr) => {
if (err) {
console.log(err, '\n', stdout, '\n', stderr);
}
start(err);
});
}
}
const copyArr = [
{ path: 'libs/htmlparser.js', recursive: false },
{ path: 'libs/node-htmlparser.js', recursive: false }
]
const toPath = ['./build/es/', './build/lib/']
function copyFile(done) {
let mark = 0;
let _result = 0;
const start = function (result) {
mark--;
_result += result;
if (!mark) {
if (!_result) {
//进程全部执行完毕
console.log("copy success!");
done();
}
}
};
for (let i = 0; i < toPath.length; i++) {
const to = toPath[i];
for (let j = 0; j < copyArr.length; j++) {
mark++;
const copyPath = './src/' + copyArr[j].path;
const toPath = to + copyArr[j].path;
// nodejs 从 16.7.0 版本开始
fs.cp(copyPath, toPath, { recursive: copyArr[j].recursive }, (err) => {
if (err) {
console.error(err);
}
start(err);
});
}
}
}
const options = {
input: 'src/index.ts',
output: [
{
file: './build/index.cjs.js',
format: 'cjs'
},
{
file: './build/index.es.js',
format: 'es'
},
{
file: './build/beautiful.soup.js',
name: 'BeautifulSoup',
format: 'iife',
extend: true,
globals: { 'BeautifulSoup': 'BeautifulSoup' }
},
],
onwarn: (waring, warn) => {
if (waring.code == "CIRCULAR_DEPENDENCY") {
console.log("warnning Circular dependency:");
console.log(waring);
}
},
// treeshake: false, //建议忽略
treeshake: true,
// treeshake: { moduleSideEffects: false },
plugins: [
resolve(),
typescript({
tsconfig: workSpaceDir + "/tsconfig.json",
check: false, //Set to false to avoid doing any diagnostic checks on the code
tsconfigOverride: { compilerOptions: { removeComments: true } },
// include: /.*.ts/,
exclude: /node_modules/,
cacheRoot: `${workSpaceDir}/.rpt2_cache`,
}),
commonjs(),
cleanup()
]
}
function compile() {
return rollup.rollup(options).then(bundle => {
return Promise.all(options.output.map(bundle.write));
}).catch(err => {
console.log(err);
});
}
function isElidableOrTraversable(node) {
switch (node.kind) {
case ts.SyntaxKind.EnumMember://枚举成员
case ts.SyntaxKind.EnumDeclaration://枚举声明
case ts.SyntaxKind.IndexSignature:
case ts.SyntaxKind.ConstructSignature:
case ts.SyntaxKind.MethodDeclaration://方法声明
case ts.SyntaxKind.MethodSignature:
case ts.SyntaxKind.FunctionDeclaration://函数声明
case ts.SyntaxKind.PropertyDeclaration://属性声明
case ts.SyntaxKind.ClassDeclaration://类声明
case ts.SyntaxKind.InterfaceDeclaration://接口声明
case ts.SyntaxKind.ModuleDeclaration://模块声明
case ts.SyntaxKind.ModuleBlock:
case ts.SyntaxKind.TypeLiteral:
case ts.SyntaxKind.SyntaxList:
case ts.SyntaxKind.VariableStatement:
return true;
}
return false;
}
function elide(data) {
const sourceFile = ts.createSourceFile("input.d.ts", data, ts.ScriptTarget.Latest, true);
const elisions = [];
sourceFile.forEachChild(visit);
const count = elisions.length;
let result = data;
while (elisions.length > 0) {
const e = elisions.pop();
result = result.substr(0, e[0]) + result.substr(e[1]);
}
return { result, count };
function visit(node) {
// console.log(111, node.kind, isElidableOrTraversable(node))
// console.log(222, node.getFullText(), node.getLeadingTriviaWidth())
// console.log(333, node.getFullStart(), node.getEnd())
if (isElidableOrTraversable(node)) {
const trivia = node.getFullText().substr(0, node.getLeadingTriviaWidth());
if (trivia.indexOf('@internal') >= 0) {
elisions.push([node.getFullStart(), node.getEnd()]);
}
else {
node.forEachChild(visit);
}
}
}
}
async function removeInternal(done) {
const fun = (_path) => {
return new Promise(async (resolve, reject) => {
const files = fs.readdirSync(_path);
for (const test of files) {
const fn = path.join(_path, test);
const stat = fs.statSync(fn);
if (stat.isFile()) {
if (fn.indexOf(".d.ts") > -1) {
const fileContent = fs.readFileSync(fn, { encoding: 'utf-8' });
const elided = elide(fileContent).result;
fs.writeFileSync(fn, elided, "utf8");
}
} else if (stat.isDirectory()) {
await fun(_path + "/" + test);
}
}
resolve()
});
}
await fun('./build')
done()
}
function cleanJs() {
const cleanList = ['./build/**/*.js']
return gulp.src(cleanList, { base: './build' })
.pipe(strip())
.on('error', function (err) {
console.warn(err.toString());
})
.pipe(gulp.dest('./build'))
}
async function revise(done) {
const reviseJS = (_path) => {
return new Promise(async (resolve, reject) => {
const files = fs.readdirSync(_path);
for (const test of files) {
const fn = path.join(_path, test);
const stat = fs.statSync(fn);
if (stat.isFile()) {
let fileContent = fs.readFileSync(fn, { encoding: 'utf-8' });
// fileContent = fileContent.replace(`function runningInNode () {`, `function runningInNode () {\nreturn true;`);
/// <reference path="./main/SoupElement.ts" />
const libsList = fileContent.match(/[/]+ <reference path=['"][\w-./]+['"] [/]>/g);
if (libsList) {
for (let i = 0, len = libsList.length; i < len; i++) {
const item = libsList[i];
const libsName = item.match(/[/]+ <reference path=['"][\w-./]+['"] [/]>/);
fileContent = fileContent.replace(libsName[0], '');
}
}
fs.writeFileSync(fn, fileContent, "utf8");
} else if (stat.isDirectory()) {
await reviseJS(_path + "/" + test);
}
}
resolve()
});
}
await reviseJS('./build');
done();
}
function compressJs() {
const options = {
// 混淆配置
mangle: {
keep_fnames: true
}
}
const compressJsList = ['./build/index.cjs.js', './build/index.es.js', './build/beautiful.soup.js'];
return gulp.src(compressJsList)
.pipe(uglify(options))
.on('error', function (err) {
console.warn(err.toString());
})
.pipe(rename({ extname: ".min.js" }))
.pipe(gulp.dest('./build/min'));
}
exports.default = gulp.series(
clear,
tsc,
copyFile,
compile,
removeInternal,
cleanJs,
revise,
compressJs
)