UNPKG

1.52 kBPlain TextView Raw
1/**
2 * @license
3 * Copyright (c) 2018 The Polymer Project Authors. All rights reserved.
4 * This code may only be used under the BSD style license found at
5 * http://polymer.github.io/LICENSE.txt
6 * The complete set of authors may be found at
7 * http://polymer.github.io/AUTHORS.txt
8 * The complete set of contributors may be found at
9 * http://polymer.github.io/CONTRIBUTORS.txt
10 * Code distributed by Google as part of the polymer project is also
11 * subject to an additional IP rights grant found at
12 * http://polymer.github.io/PATENTS.txt
13 */
14
15import {htmlTransform} from './html-transform';
16import {AsyncTransformStream, getFileContents} from './streams';
17
18import File = require('vinyl');
19
20/**
21 * When compiling to ES5 we need to inject Babel's helpers into a global so
22 * that they don't need to be included with each compiled file.
23 */
24export 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}