-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.ts
332 lines (318 loc) · 10.2 KB
/
gulpfile.ts
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
const header = require('gulp-header') as typeof import('gulp-header');
import * as replace from 'gulp-replace';
import * as rollup from 'rollup';
import * as fs from 'fs-extra';
import * as path from 'path';
import * as glob from 'glob';
import * as gulp from 'gulp';
declare class Promise<T> {
constructor(
handlers: (resolve: (value: T) => any, reject: (err: any) => any) => any
);
then(callback: (value: T) => any): this;
static all<T>(promises: T[]): Promise<T[]>;
}
const ISTANBUL_IGNORE_NEXT = '/* istanbul ignore next */';
const typescriptInsertedData = [
['var __decorate = (this && this.__decorate)', '};'],
['var __awaiter = (this && this.__awaiter)', '};'],
];
function istanbulIgnoreTypescriptFn(file: string) {
let found = false;
for (const [start] of typescriptInsertedData) {
if (file.indexOf(start) > -1) {
found = true;
}
}
if (!found) {
return file;
}
const ignoredLines = [];
let ignoring = false;
const lines = file.split('\n');
for (let i = 0; i < lines.length; i++) {
if (ignoring) {
for (const [, endStr] of typescriptInsertedData) {
if (lines[i].indexOf(endStr) > -1) {
ignoring = false;
break;
}
}
ignoredLines.push(i);
} else {
for (const [startStr] of typescriptInsertedData) {
if (lines[i].indexOf(startStr) > -1) {
ignoring = true;
ignoredLines.push(i);
break;
}
}
}
}
// Find contiguous blocks
const blocks: {
start: number;
end: number;
}[] = [];
let currentBlock: {
start: number;
end: number;
} | null = {
start: 0,
end: Infinity,
};
for (let i = 1; i < ignoredLines.length; i++) {
if (ignoredLines[i] === ignoredLines[i - 1] + 1) {
// Contiguous
if (currentBlock === null) {
currentBlock = {
start: ignoredLines[i - 1],
end: Infinity,
};
}
} else {
// End of contiguous block
currentBlock!.end = ignoredLines[i - 1];
blocks.push(currentBlock!);
currentBlock = null;
}
}
if (currentBlock) {
currentBlock.end = ignoredLines[ignoredLines.length - 1];
blocks.push(currentBlock);
}
// Insert comments before/after those blocks
const newLines = [...lines];
for (let i = blocks.length - 1; i >= 0; i--) {
const { start } = blocks[i];
if (
newLines[Math.max(start - 1, 0)].indexOf(ISTANBUL_IGNORE_NEXT) ===
-1
) {
newLines.splice(start, 0, ISTANBUL_IGNORE_NEXT);
}
}
return newLines.join('\n');
}
function globProm(pattern: string, options?: any): Promise<string[]> {
return new Promise((resolve, reject) => [
glob(pattern, options || {}, (err: any | void, matches: string[]) => {
if (err) {
reject(err);
} else {
resolve(matches);
}
}),
]);
}
gulp.task(
'precoverage',
gulp.parallel(
function istanbulIgnoreTypescript() {
return new Promise((resolve) => {
globProm('src/**/*.js').then((filePaths) => {
Promise.all(
filePaths.map((filePath) => {
return fs
.readFile(filePath, {
encoding: 'utf8',
})
.then((content) => {
return fs.writeFile(
filePath,
istanbulIgnoreTypescriptFn(content),
{
encoding: 'utf8',
}
);
});
})
).then(resolve);
});
});
},
function copyMapsES() {
return gulp
.src(['**/*.map'], {
cwd: './build/es',
base: './build/es',
})
.pipe(gulp.dest('instrumented/'));
},
function copyMapsCJS() {
return gulp
.src(['**/*.map'], {
cwd: './build/cjs',
base: './build/cjs',
})
.pipe(gulp.dest('instrumented-cjs/'));
}
)
);
function fromEntries<V>(
entries: [string, V][]
): {
[key: string]: V;
} {
const obj: {
[key: string]: V;
} = {};
for (const [key, val] of entries) {
obj[key] = val;
}
return obj;
}
/**
* Filter out instrumented files from generated unit test
* coverage file
*/
gulp.task('filterInstrumented', () => {
return new Promise((resolve) => {
globProm('.nyc_output/*.json').then((filePaths) => {
Promise.all(
filePaths.map((filePath) => {
return fs
.readFile(filePath, {
encoding: 'utf8',
})
.then((content) => {
const parsed = JSON.parse(content);
const filtered = fromEntries(
Object.keys(parsed)
.filter((key) => {
return (
key.indexOf('instrumented') === -1
);
})
.map((key) => [key, parsed[key]])
);
return fs.writeFile(
filePath,
JSON.stringify(filtered, null, '\t'),
{
encoding: 'utf8',
}
);
});
})
).then(resolve);
});
});
});
gulp.task('replaceTestImports', () => {
return gulp
.src(['**/*.js'], {
cwd: './test',
base: './test',
})
.pipe(replace('/build/es/', '/instrumented/'))
.pipe(replace('/build/cjs/', '/instrumented-cjs/'))
.pipe(gulp.dest('test'));
});
gulp.task(
'prepack',
gulp.series(
function removeIstanbulIgnoresCompiled() {
return gulp
.src(['**/*.js', '**/*.d.ts'], {
cwd: 'build/',
base: 'build/',
})
.pipe(replace(/(\n\s+)?\/\*(\s*)istanbul(.*?)\*\//g, ''))
.pipe(gulp.dest('build/'));
},
function removeTypeChecks() {
return gulp
.src(['**/*.js'], {
cwd: 'build/',
base: 'build/',
})
.pipe(replace(/const __typecheck__ = \w+;/g, ''))
.pipe(replace(/__typecheck__;/g, ''))
.pipe(gulp.dest('build/'));
}
)
);
// Prepares the examples for hosting on gh-pages
gulp.task(
'prepareWebsite',
gulp.parallel(
function moveLitHTMLTypes() {
return gulp
.src(['**/*.*', '!**/*.js'], {
cwd: 'node_modules/lit-html/',
base: 'node_modules/lit-html/',
})
.pipe(gulp.dest('examples/modules/lit-html/'));
},
function moveLitHTML() {
return gulp
.src(['**/*.js'], {
cwd: 'node_modules/lit-html/',
base: 'node_modules/lit-html/',
})
.pipe(
header(
'var window = typeof window !== "undefined" ? window : {};'
)
)
.pipe(gulp.dest('examples/modules/lit-html/'));
},
async function moveBundledLitHTML() {
const bundle = await rollup.rollup({
input: path.join(
__dirname,
'node_modules/lit-html/lit-html.js'
),
});
const outPath = path.join(
__dirname,
'examples/modules/lit-html-bundled/',
'lit-html.js'
);
const { output } = await bundle.generate({
file: outPath,
name: 'lithtml',
format: 'esm',
});
await fs.mkdirp(path.dirname(outPath));
await fs.writeFile(
outPath,
'var window = typeof window !== "undefined" ? window : {};' +
output[0].code,
{
encoding: 'utf8',
}
);
return gulp
.src(['**/*.d.ts'], {
cwd: 'node_modules/lit-html/',
base: 'node_modules/lit-html/',
})
.pipe(gulp.dest('examples/modules/lit-html-bundled/'));
},
function movewclib() {
return gulp
.src(['**/*.*'], {
cwd: 'build/es/',
base: 'build/es/',
})
.pipe(gulp.dest('examples/modules/wc-lib/'));
},
function changeImports() {
return gulp
.src(['**/*.js', '**/*.ts'], {
cwd: 'examples',
base: 'examples',
})
.pipe(
replace(
/\.\.\/\.\.\/node\_modules\/lit\-html/g,
'../modules/lit-html-bundled'
)
)
.pipe(replace(/\.\.\/\.\.\/build\/es/g, '../modules/wc-lib'))
.pipe(gulp.dest('examples/'));
}
)
);