This repository has been archived by the owner on Mar 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
69 lines (62 loc) · 1.88 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
/* eslint-disable no-console, @typescript-eslint/no-var-requires */
const fs = require('fs');
const gulp = require('gulp');
const insert = require('gulp-insert');
const rename = require('gulp-rename');
const replace = require('gulp-replace');
const terser = require('gulp-terser');
const tmpTsPath = './src/tmp.ts';
const tmpJsPath = './build/tmp.js';
gulp.task('build', async () => {
return new Promise(resolve => {
gulp
.src(tmpJsPath)
.pipe(replace(/export\s/g, ''))
.pipe(
terser({
compress: true,
toplevel: true,
mangle: {
properties: {
regex: /^_/
}
}
})
)
.pipe(replace(/%60/g, '% 60')) // Chromeでブックマークレットを登録するときに ` に変換されるための対策
.pipe(insert.wrap('javascript:(()=>{', '})();')) // eslint-disable-line no-script-url
.pipe(rename('bookmarklet.js'))
.pipe(gulp.dest('build'))
.on('end', () => {
[tmpTsPath, 'build/classes.js', 'build/index.js', 'build/tmp.js'].forEach(file =>
fs.unlink(file, err => {
if (err) throw err;
})
);
resolve();
});
});
});
gulp.task('setup', async () => {
const readFile = filePath => {
return new Promise(resolve => {
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) throw err;
resolve(data);
});
});
};
const writeFile = (filePath, data) => {
return new Promise(resolve => {
fs.writeFile(filePath, data, err => {
if (err) throw err;
resolve();
});
});
};
let data = await readFile('./src/classes.ts');
data += await readFile('./src/index.ts');
data = data.replace(/^(import|export|declare)\s/gm, '// $1 ');
data = data.replace(/^interface MyWindow extends /gm, 'interface ');
await writeFile(tmpTsPath, data);
});