1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | import {htmlTransform} from './html-transform';
|
16 | import {AsyncTransformStream, getFileContents} from './streams';
|
17 |
|
18 | import File = require('vinyl');
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | export class BabelHelpersInjector extends AsyncTransformStream<File, File> {
|
25 | constructor(private entrypoint: string) {
|
26 | super({objectMode: true});
|
27 | }
|
28 |
|
29 | protected async * _transformIter(files: AsyncIterable<File>) {
|
30 | for await (const file of files) {
|
31 | yield await this.processFile(file);
|
32 | }
|
33 | }
|
34 |
|
35 | private async processFile(file: File): Promise<File> {
|
36 | if (file.path !== this.entrypoint) {
|
37 | return file;
|
38 | }
|
39 | const contents = await getFileContents(file);
|
40 | const transformed = htmlTransform(contents, {injectBabelHelpers: 'full'});
|
41 | const newFile = file.clone();
|
42 | newFile.contents = Buffer.from(transformed, 'utf-8');
|
43 | return newFile;
|
44 | }
|
45 | }
|