forked from Polymer/polymer-build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
polymer-project.ts
107 lines (91 loc) · 3.68 KB
/
polymer-project.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
/**
* @license
* Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
* This code may only be used under the BSD style license found at
* http://polymer.github.io/LICENSE.txt
* The complete set of authors may be found at
* http://polymer.github.io/AUTHORS.txt
* The complete set of contributors may be found at
* http://polymer.github.io/CONTRIBUTORS.txt
* Code distributed by Google as part of the polymer project is also
* subject to an additional IP rights grant found at
* http://polymer.github.io/PATENTS.txt
*/
import * as logging from 'plylog';
import {ProjectConfig, ProjectOptions} from 'polymer-project-config';
import {src as vinylSrc} from 'vinyl-fs';
import {BuildAnalyzer} from './analyzer';
import {BuildBundler, Options as BuildBundlerOptions} from './bundle';
import {CustomElementsEs5AdapterInjector} from './custom-elements-es5-adapter';
import {AddPushManifest} from './push-manifest';
const logger = logging.getLogger('polymer-project');
export class PolymerProject {
config: ProjectConfig;
/**
* A `Transform` stream that uses polymer-analyzer to analyze the files. It
* can be used to get information on dependencies and fragments for the
* project once the source & dependency streams have been piped into it.
*/
analyzer: BuildAnalyzer;
constructor(config: ProjectConfig|ProjectOptions|string) {
if (config.constructor.name === 'ProjectConfig') {
this.config = <ProjectConfig>config;
} else if (typeof config === 'string') {
this.config = ProjectConfig.loadConfigFromFile(config);
} else {
this.config = new ProjectConfig(config);
}
logger.debug(`build config loaded:`, this.config);
this.analyzer = new BuildAnalyzer(this.config);
}
/**
* Returns a `Transform` stream that modifies the files that pass through it
* based on the dependency analysis done by the `analyzer` transform. It
* "bundles" a project by injecting its dependencies into the application
* fragments themselves, so that a minimum number of requests need to be made
* to load.
*
* (NOTE: The analyzer stream must be in the pipeline somewhere before this.)
*/
bundler(options?: BuildBundlerOptions): BuildBundler {
return new BuildBundler(this.config, this.analyzer, options);
}
/**
* Returns the analyzer's stream of this project's source files - files
* matched by the project's `config.sources` value.
*/
sources(): NodeJS.ReadableStream {
return this.analyzer.sources();
}
/**
* Returns the analyzer's stream of this project's dependency files - files
* loaded inside the analyzed project that are not considered source files.
*/
dependencies(): NodeJS.ReadableStream {
let dependenciesStream: NodeJS.ReadableStream =
this.analyzer.dependencies();
// If we need to include additional dependencies, create a new vinyl
// source stream and pipe our default dependencyStream through it to
// combine.
if (this.config.extraDependencies.length > 0) {
const includeStream = vinylSrc(this.config.extraDependencies, {
cwdbase: true,
nodir: true,
passthrough: true,
});
dependenciesStream = dependenciesStream.pipe(includeStream);
}
return dependenciesStream;
}
addPushManifest(filePath?: string): NodeJS.ReadWriteStream {
return new AddPushManifest(this.config, filePath);
}
/**
* Returns a stream transformer that injects `custom-elements-es5-adapter.js`
* into the entry point HTML file. This adapter is needed when serving ES5
* to browsers that support the native Custom Elements API.
*/
addCustomElementsEs5Adapter(): NodeJS.ReadWriteStream {
return new CustomElementsEs5AdapterInjector();
}
}