forked from fasttime/Polytype
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
256 lines (226 loc) · 7.29 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
/* eslint-env node */
'use strict';
const { dest, parallel, series, src, task } = require('gulp');
async function bundle(inputPath, outputPath, format)
{
const { homepage, version } = require('./package.json');
const { rollup } = require('rollup');
const cleanup = require('rollup-plugin-cleanup');
const cleanupPlugin = cleanup({ maxEmptyLines: -1 });
const inputOptions = { input: inputPath, plugins: [cleanupPlugin] };
const bundle = await rollup(inputOptions);
const outputOptions =
{
banner: `// Polytype ${version} – ${homepage}\n`,
esModule: false,
file: outputPath,
format,
};
await bundle.write(outputOptions);
}
function minify(srcGlobs, module, extname)
{
const rename = require('gulp-rename');
const terser = require('gulp-terser');
const minifyOpts =
{
compress: { hoist_funs: true, passes: 2 },
module,
output: { comments: (node, comment) => comment.pos === 0 },
};
const stream =
src(srcGlobs).pipe(terser(minifyOpts)).pipe(rename({ extname })).pipe(dest('lib'));
return stream;
}
function readFileAsString(inputPath)
{
const { promises: { readFile } } = require('fs');
const promise = readFile(inputPath, 'utf8');
return promise;
}
task
(
'clean',
async () =>
{
const { promises: { rmdir } } = require('fs');
const paths = ['.nyc_output', 'coverage', 'lib', 'readme.md', 'test/spec-runner.html'];
await Promise.all(paths.map(path => rmdir(path, { recursive: true })));
},
);
task
(
'make-ts-defs',
async () =>
{
const { version } = require('./package.json');
const { promises: { mkdir, writeFile } } = require('fs');
const Handlebars = require('handlebars');
async function writeOutput(outputPath, asModule)
{
const output = template({ asModule, version });
await writeFile(outputPath, output);
}
const mkdirPromise = mkdir('lib', { recursive: true });
const input = await readFileAsString('src/polytype.d.ts.hbs');
const template = Handlebars.compile(input, { noEscape: true });
await mkdirPromise;
const promises =
[
writeOutput('lib/polytype-global.d.ts', false),
writeOutput('lib/polytype-module.d.ts', true),
];
await Promise.all(promises);
},
);
task
(
'lint',
() =>
{
const lint = require('gulp-fasttime-lint');
const stream =
lint
(
{
src: 'src/**/*.{js,mjs}',
globals: ['globalThis'],
parserOptions: { ecmaVersion: 2020, sourceType: 'module' },
rules: { 'prefer-named-capture-group': 'off' },
},
{
src: 'lib/**/*.d.ts',
parserOptions: { ecmaVersion: 2020, project: 'tsconfig.json' },
rules: { 'max-len': 'off' },
},
{
src: ['*.js', 'test/**/*.js'],
parserOptions: { ecmaVersion: 2020 },
},
{
src: 'example/**/*.{js,ts}',
envs: 'node',
globals: ['classes', 'console'],
parserOptions:
{ ecmaVersion: 2020, project: 'tsconfig.json', sourceType: 'module' },
rules:
{
'brace-style': 'off',
'comma-dangle':
[
'error',
{
'arrays': 'always-multiline',
'objects': 'always-multiline',
'imports': 'always-multiline',
'exports': 'always-multiline',
'functions': 'only-multiline',
},
],
'no-unused-vars':
['error', { varsIgnorePattern: '^(?:Green|WhiteUnit)Circle$' }],
'quotes': ['error', 'double'],
},
},
);
return stream;
},
);
task('bundle:cjs', () => bundle('src/polytype-esm.js', 'lib/polytype.cjs', 'cjs'));
task('bundle:esm', () => bundle('src/polytype-esm.js', 'lib/polytype.mjs', 'esm'));
task('bundle:global', () => bundle('src/polytype-global.js', 'lib/polytype.js', 'iife'));
task('minify:esm', () => minify('lib/polytype.mjs', true, '.min.mjs'));
task('minify:global', () => minify('lib/polytype.js', false, '.min.js'));
task
(
'test',
callback =>
{
const { fork } = require('child_process');
const { resolve } = require;
const nycPath = resolve('nyc/bin/nyc');
const modulePath = resolve('./test/node-spec-runner');
const childProcess =
fork
(
nycPath,
['--extension=.cjs', '--reporter=html', '--reporter=text-summary', '--', modulePath],
);
childProcess.on('exit', code => callback(code && 'Test failed'));
},
);
task
(
'make-spec-runner',
async () =>
{
const { promises: { readdir, writeFile } } = require('fs');
const Handlebars = require('handlebars');
const { extname } = require('path');
async function getSpecs()
{
const filenames = await readdir('test/spec/common');
const specs = filenames.filter(filename => extname(filename) === '.js').sort();
return specs;
}
async function getTemplate()
{
const input = await readFileAsString('src/spec-runner.html.hbs');
const template = Handlebars.compile(input);
return template;
}
const promises = [getTemplate(), getSpecs()];
const [template, specs] = await Promise.all(promises);
const output = template({ specs });
await writeFile('test/spec-runner.html', output);
},
);
task
(
'make-toc',
async () =>
{
const { version } = require('./package.json');
const { promises: { writeFile } } = require('fs');
const Handlebars = require('handlebars');
const toc = require('markdown-toc');
const input = await readFileAsString('src/readme.md.hbs');
const { content } = toc(input, { firsth1: false });
const template = Handlebars.compile(input, { noEscape: true });
const output = template({ toc: content, version });
await writeFile('readme.md', output, { mode: 0o444 });
},
);
task
(
'default',
series
(
'clean',
'make-ts-defs',
'lint',
parallel
(
'bundle:cjs',
series('bundle:esm', 'minify:esm'),
series('bundle:global', 'minify:global'),
),
'test',
parallel('make-spec-runner', 'make-toc'),
),
);
task
(
'build',
series
(
'make-ts-defs',
parallel
(
'bundle:cjs',
series('bundle:esm', 'minify:esm'),
series('bundle:global', 'minify:global'),
),
parallel('make-spec-runner', 'make-toc'),
),
);