-
Notifications
You must be signed in to change notification settings - Fork 16
/
gulpfile.js
129 lines (110 loc) · 4.2 KB
/
gulpfile.js
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
/**
* @license
* Copyright (c) 2016 The GDG Spain Authors. All rights reserved.
* This code may only be used under the MIT style license found at http://gdg.es/LICENSE.txt
*/
/* eslint-env es6, node */
/* eslint-disable no-console */
'use strict';
const del = require('del');
const gulp = require('gulp');
const gulpif = require('gulp-if');
const htmlmin = require('gulp-htmlmin');
const imagemin = require('gulp-imagemin');
const jsonmin = require('gulp-jsonmin');
const mergeStream = require('merge-stream');
const polymerBuild = require('polymer-build');
const replace = require('gulp-replace');
const uglify = require('gulp-uglify');
const environment = process.env.NODE_ENV || 'development';
const swPrecacheConfig = require('./sw-precache-config.js');
const polymerJson = require('./polymer.json');
const polymerProject = new polymerBuild.PolymerProject(polymerJson);
const buildDirectory = 'build';
const settings = {
authDomain: {
develop: 'gdg-es-develop.firebaseapp.com',
production: 'gdg-es.firebaseapp.com'
},
databaseUrl: {
develop: 'https://gdg-es-develop.firebaseio.com',
production: 'https://gdg-es.firebaseio.com'
}
};
/**
* Waits for the given ReadableStream.
*/
function waitFor(stream) {
return new Promise((resolve, reject) => {
stream.on('end', resolve);
stream.on('error', reject);
});
}
function build() {
return new Promise((resolve) => {
// Let's create some inline code splitters.
let sourcesStreamSplitter = new polymerBuild.HtmlSplitter();
let dependenciesStreamSplitter = new polymerBuild.HtmlSplitter();
// Okay, so first thing we do is clear the build directory.
console.log(`Deleting ${buildDirectory} directory...`);
del([buildDirectory])
.then(() => {
// Let's start by getting your source files.
let sourcesStream = polymerProject.sources()
.pipe(gulpif(/\.(png|gif|jpg|svg)$/, imagemin()))
// The `sourcesStreamSplitter` created above can be added here to
// pull any inline styles and scripts out of their HTML files and
// into seperate CSS and JS files in the build stream.
.pipe(sourcesStreamSplitter.split())
// Let's optimice your source files.
.pipe(gulpif(/\.html$/, htmlmin({
collapseWhitespace: true
})))
.pipe(gulpif(/\.js$/, uglify()))
.pipe(gulpif(/\.json$/, jsonmin()))
// Let's do the some changes for production
.pipe(gulpif(environment === 'production', replace(
settings.authDomain.develop,
settings.authDomain.production
)))
.pipe(gulpif(environment === 'production', replace(
settings.databaseUrl.develop,
settings.databaseUrl.production
)))
// Rejoin your source files.
.pipe(sourcesStreamSplitter.rejoin());
// Similarly, you can get your dependencies seperately and perform any
// dependency-only optimizations here as well.
let dependenciesStream = polymerProject.dependencies()
.pipe(dependenciesStreamSplitter.split())
// Add any dependency optimizations here.
.pipe(dependenciesStreamSplitter.rejoin());
// Okay, now let's merge them into a single build stream.
let buildStream = mergeStream(sourcesStream, dependenciesStream)
.once('data', () => {
console.log('Analyzing build dependencies...');
});
// This will bundle dependencies into your fragments so you can lazy
// load them.
buildStream = buildStream.pipe(polymerProject.bundler());
// Okay, time to pipe to the build directory
buildStream = buildStream.pipe(gulp.dest(buildDirectory));
// Wait for the buildStream to complete
return waitFor(buildStream);
})
.then(() => {
console.log('Generating the Service Worker...');
return polymerBuild.addServiceWorker({
project: polymerProject,
buildRoot: buildDirectory,
bundled: true,
swPrecacheConfig: swPrecacheConfig
});
})
.then(() => {
console.log('Build complete!');
resolve();
});
});
}
gulp.task('build', build);