-
Notifications
You must be signed in to change notification settings - Fork 2
/
gulpfile.js
144 lines (125 loc) · 4.49 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* eslint-disable */
var gulp = require('gulp');
var spawn = require('child_process').spawn;
var del = require('del');
var crisper = require('gulp-crisper');
var gulpif = require('gulp-if');
var sourcemaps = require('gulp-sourcemaps');
var babel = require('gulp-babel');
var gulpIgnore = require('gulp-ignore');
var PolymerProject = require('polymer-build').PolymerProject;
var mergeStream = require('merge-stream');
var HtmlSplitter = require('polymer-build').HtmlSplitter;
var sourcesHtmlSplitter = new HtmlSplitter();
var project = new PolymerProject(require('./polymer.json'));
var generateServiceWorker = require('polymer-build').generateServiceWorker;
var addServiceWorker = require('polymer-build').addServiceWorker;
var isWin = /^win/.test(process.platform);
function waitFor(stream) {
return new Promise((resolve, reject) => {
stream.on('end', resolve);
stream.on('error', reject);
});
}
gulp.spawnCmd = function (command) {
if (isWin){
return command + ".cmd";
} else {
return command;
}
};
gulp.copy = function(src, dest){
return gulp.src(src, {base:"."})
.pipe(gulp.dest(dest));
};
gulp.copyBase = function(src, dest, base){
return gulp.src(src, {base: base})
.pipe(gulp.dest(dest));
};
gulp.buildSources = function (sources) {
return project.sources()
.pipe(sourcesHtmlSplitter.split()) // split inline JS & CSS out into individual .js & .css files
.pipe(sourcemaps.init())
.pipe(gulpif('*.js', babel({
presets: ['es2015']
})))
.pipe(sourcemaps.write())
.pipe(sourcesHtmlSplitter.rejoin());
};
gulp.task('transpile-es2015', [], function () {
return gulp.src(['src/**/*.{js,html}', '!bower_components/**/*'])
.pipe(sourcesHtmlSplitter.split()) // split inline JS & CSS out into individual .js & .css files
.pipe(sourcemaps.init())
.pipe(gulpif('*.js', babel({
presets: ['es2015']
})))
.pipe(sourcemaps.write())
.pipe(sourcesHtmlSplitter.rejoin()) // rejoins those files back into their original location
.pipe(gulp.dest('.tmp/src'));
});
/*
Define build stages
*/
gulp.task('copy-temp', ['transpile-es2015'], function () {
gulp.copy('test/**/*', '.tmp');
gulp.copy('bower_components/**/*', '.tmp');
gulp.copy('node_modules/**/*', '.tmp');
gulp.copy('index.html', '.tmp');
gulp.copy('bower.json', '.tmp');
gulp.copy('package.json', '.tmp');
gulp.copy('polymer.json', '.tmp');
gulp.copy('manifest.json', '.tmp');
gulp.copy('service-worker.js', '.tmp');
gulp.copy('firebase-messaging-sw.js', '.tmp');
gulp.copy('sw-precache-config.js', '.tmp');
gulp.copy('images/**/*', '.tmp');
});
gulp.task('test-exec', ['copy-temp'], function (onComplete) {
spawn(gulp.spawnCmd('polymer'), ['test'], { cwd: '.tmp/', stdio: 'inherit' })
.on('close', function (){
onComplete(null);
}).on('error', function (error) {
onComplete(error);
});
});
gulp.task('test', ['test-exec'], function () {
del(['.tmp/']);
});
gulp.task('clean', function () {
del(['.tmp/']);
del(['dist/']);
});
gulp.task('serve', ['copy-temp'], function (onComplete) {
spawn(gulp.spawnCmd('polymer'), ['serve', '--open', '.'], { cwd: '.tmp/', stdio: 'inherit' })
.on('close', function (){
}).on('error', function (error) {
onComplete(error);
});
gulp.watch('src/**/*', ['copy-temp']);
});
function writeBundledServiceWorker(project, swPreCache) {
// On windows if we pass the path with back slashes the sw-precache node module is not going
// to strip the build/bundled or build/unbundled because the path was passed in with backslash.
return addServiceWorker({
project: project,
buildRoot: 'production-build/',
swPrecacheConfig: swPreCache,
bundled: true
});
}
gulp.task('build', [], function (onComplete) {
var stream = mergeStream(gulp.buildSources(project.sources()), project.dependencies());
console.log("Sources Merged");
stream = stream.pipe(project.bundler);
console.log("BUNDLING");
stream = stream.pipe(gulp.dest('production-build/'));
waitFor(stream).then(function () {
var swPreCache = require('./sw-precache-config.js');
console.log(swPreCache);
writeBundledServiceWorker(project, swPreCache).then(() => {
console.log("SW-Generated");
console.log("Build Complete!");
onComplete();
});
});
});