forked from KanoComputing/tapcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
120 lines (106 loc) · 4.04 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
const env = process.env.NODE_ENV || 'staging';
const gulp = require('gulp');
const gulpIf = require('gulp-if');
const path = require('path');
const babel = require('gulp-babel');
const cssSlam = require('css-slam').gulp;
const htmlMinifier = require('gulp-html-minifier');
const htmlReplace = require('gulp-html-replace');
const htmlAutoprefixer = require('gulp-autoprefixer-html');
const strip = require('gulp-strip-comments');
const uglify = require('gulp-uglify-es').default;
const version = require('./package.json').version;
const del = require('del');
const mergeStream = require('merge-stream');
const polymerBuild = require('polymer-build');
const polymerJson = require('./polymer.json');
const swPrecacheConfig = require('./sw-precache-config.js');
const project = new polymerBuild.PolymerProject(polymerJson);
const targetDir = 'www';
function waitFor(stream) {
return new Promise((resolve, reject) => {
stream.on('end', resolve);
stream.on('error', reject);
});
}
function getEnvVars () {
let code = '';
code += 'window.Kano = window.Kano || {};';
code += 'window.Kano.Tapcode = window.Kano.Tapcode || {};';
code += 'window.Kano.Tapcode.Config = window.Kano.Tapcode.Config || {};';
code += `window.Kano.Tapcode.Config.VERSION = '${version}';`;
return code;
}
function build() {
return new Promise((resolve, reject) => {
let srcSplit = new polymerBuild.HtmlSplitter();
let depsSplit = new polymerBuild.HtmlSplitter();
let src = project.sources()
.pipe(srcSplit.split())
.pipe(gulpIf(/\.js$/,
babel({
presets: ['es2015']
}))
)
.pipe(gulpIf(/\.js$/, uglify()))
.pipe(gulpIf(/\.(css|html)$/, htmlAutoprefixer()))
.pipe(gulpIf(/\.(css|html)$/, cssSlam()))
.pipe(gulpIf(/\.html$/, htmlReplace({
env: `<script type="text/javascript">${getEnvVars()}</script>`,
config: `<link rel="import" href="/src/config/${env}.html">`,
es5adapter: '<script src="./bower_components/webcomponentsjs/custom-elements-es5-adapter.js"></script>'
})))
.pipe(gulpIf(/\.(html|css|js)$/, strip()))
.pipe(gulpIf(/\.html$/, htmlMinifier({
removeComments: true,
collapseWhitespace: true
})))
.pipe(srcSplit.rejoin());
let deps = project.dependencies()
.pipe(depsSplit.split())
.pipe(gulpIf(/\.js$/,
babel({
ignore: [
'bower_components/webcomponentsjs/custom-elements-es5-adapter.js',
'bower_components/js-md5/build/md5.min.js'
],
presets: ['es2015'],
plugins: ['transform-remove-strict-mode']
}))
)
.pipe(gulpIf(/\.js$/, uglify()))
.pipe(gulpIf(/\.(css|html)$/, htmlAutoprefixer({
recognizeSelfClosing: true,
xmlMode: true
})))
.pipe(gulpIf(/\.(css|html)$/, cssSlam()))
.pipe(gulpIf(/\.(html|css|js)$/, strip()))
.pipe(gulpIf(/\.html$/, htmlMinifier({
removeComments: true,
collapseWhitespace: true
})))
.pipe(depsSplit.rejoin());
let build = mergeStream(src, deps)
.pipe(project.bundler())
.pipe(project.addPushManifest())
.pipe(gulp.dest(targetDir));
return waitFor(build).then(() => {
return polymerBuild.addServiceWorker({
project: project,
buildRoot: targetDir,
bundled: true,
swPrecacheConfig: swPrecacheConfig
});
})
.then(resolve)
.catch((err) => {
console.log(err);
reject(err);
});
});
}
gulp.task('clean', () => {
return del([targetDir]);
});
gulp.task('build', ['clean'], build);
gulp.task('default', ['build']);