This repository has been archived by the owner on Dec 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 128
/
build.ts
149 lines (128 loc) · 5.22 KB
/
build.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
/**
* @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 clone = require('clone');
import * as fs from 'fs';
import * as gulp from 'gulp';
import * as gulpif from 'gulp-if';
import * as gutil from 'gulp-util';
import mergeStream = require('merge-stream');
import * as path from 'path';
import {PassThrough, Readable} from 'stream';
import * as logging from 'plylog';
import {PolymerProject, addServiceWorker, forkStream, DepsIndex, SWConfig} from 'polymer-build';
const uglify = require('gulp-uglify');
const cssSlam = require('css-slam').gulp;
const htmlmin = require('gulp-html-minifier');
import {ProjectConfig} from '../project-config';
import {PrefetchTransform} from './prefetch';
import {waitFor} from './streams';
import {parsePreCacheConfig} from './sw-precache';
let logger = logging.getLogger('cli.build.build');
export interface BuildOptions {
sources?: string[];
includeDependencies?: string[];
swPrecacheConfig?: string;
insertDependencyLinks?: boolean;
// TODO(fks) 07-21-2016: Fully complete these with available options
html?: {
collapseWhitespace?: boolean;
removeComments?: boolean;
};
css?: {
stripWhitespace?: boolean;
};
js?: {
minify?: boolean;
};
}
export function build(options: BuildOptions, config: ProjectConfig): Promise<any> {
return new Promise<any>((buildResolve, _) => {
let polymerProject = new PolymerProject({
root: config.root,
shell: config.shell,
entrypoint: config.entrypoint,
fragments: config.fragments,
sourceGlobs: options.sources,
includeDependencies: options.includeDependencies,
});
if (options.insertDependencyLinks) {
logger.debug(`Additional dependency links will be inserted into application`);
}
// mix in optimization options from build command
// TODO: let this be set by the user
let optimizeOptions = {
html: Object.assign({removeComments: true}, options.html),
css: Object.assign({stripWhitespace: true}, options.css),
js: Object.assign({minify: true}, options.js),
};
logger.info(`Building application...`);
logger.debug(`Reading source files...`);
let sourcesStream = polymerProject.sources()
.pipe(polymerProject.splitHtml())
.pipe(gulpif(/\.js$/, uglify(optimizeOptions.js)))
.pipe(gulpif(/\.css$/, cssSlam(optimizeOptions.css)))
.pipe(gulpif(/\.html$/, htmlmin(optimizeOptions.html)))
.pipe(polymerProject.rejoinHtml());
logger.debug(`Reading dependencies...`);
let depsStream = polymerProject.dependencies()
.pipe(polymerProject.splitHtml())
.pipe(polymerProject.rejoinHtml());
let buildStream = mergeStream(sourcesStream, depsStream)
.once('data', () => { logger.debug('Analyzing build dependencies...'); })
.pipe(polymerProject.analyzer);
let unbundledPhase = forkStream(buildStream)
.once('data', () => { logger.info('Generating build/unbundled...'); })
.pipe(
gulpif(
options.insertDependencyLinks,
new PrefetchTransform(polymerProject.root, polymerProject.entrypoint,
polymerProject.shell, polymerProject.fragments,
polymerProject.analyzer)
)
)
.pipe(gulp.dest('build/unbundled'));
let bundledPhase = forkStream(buildStream)
.once('data', () => { logger.info('Generating build/bundled...'); })
.pipe(polymerProject.bundler)
.pipe(gulp.dest('build/bundled'));
let swPrecacheConfig = path.resolve(polymerProject.root, options.swPrecacheConfig || 'sw-precache-config.js');
let loadSWConfig = parsePreCacheConfig(swPrecacheConfig);
loadSWConfig.then((swConfig) => {
if (swConfig) {
logger.debug(`Service worker config found`, swConfig);
} else {
logger.debug(`No service worker configuration found at ${swPrecacheConfig}, continuing with defaults`);
}
});
// Once the unbundled build stream is complete, create a service worker for the build
let unbundledPostProcessing = Promise.all([loadSWConfig, waitFor(unbundledPhase)]).then((results) => {
let swConfig: SWConfig = results[0];
return addServiceWorker({
buildRoot: 'build/unbundled',
project: polymerProject,
swConfig: swConfig,
});
});
// Once the bundled build stream is complete, create a service worker for the build
let bundledPostProcessing = Promise.all([loadSWConfig, waitFor(bundledPhase)]).then((results) => {
let swConfig: SWConfig = results[0];
return addServiceWorker({
buildRoot: 'build/bundled',
project: polymerProject,
swConfig: swConfig,
bundled: true,
});
});
return Promise.all([unbundledPostProcessing, bundledPostProcessing]).then(() => {
logger.info('Build complete!');
buildResolve();
});
});
}