-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
139 lines (122 loc) · 4.21 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
const gulp = require('gulp');
const rollup = require('rollup');
const resolve = require('rollup-plugin-node-resolve');
const commonjs = require('rollup-plugin-commonjs');
const compiler = require('google-closure-compiler-js').gulp();
const fs = require('fs');
const archiver = require('archiver');
const cleanCSS = require('gulp-clean-css');
const inlinesource = require('gulp-inline-source');
const rename = require('gulp-rename');
const notifier = require('node-notifier');
gulp.task('build', () => {
return do_rollup('./src/index.js', './dist/library.js')
.then(() => do_closure_compile('./dist/library.js', './dist/'))
.then(() => do_clean_css('./src/styles/main.css', 'dist'))
.then(() => do_inline_source('./src/index.template.html', './dist'))
.then(() => postbuild());
});
gulp.task('default', () => {
return do_rollup('./src/index.js', './dist/library.js')
.then(() => do_clean_css('./src/styles/main.css', 'dist'));
});
gulp.task('build_source', () => {
return do_rollup('./src/index.js', './dist/library.cjs.js')
.then(() => do_inline_source('./src/index.template.html', './dist'))
.then(() => notifier.notify('JS compiled.'));
});
gulp.task('build_styles', () => {
return do_clean_css('./src/styles/main.css', 'dist')
.then(() => do_inline_source('./src/index.template.html', './dist'))
.then(() => notifier.notify('CSS compiled.'));
});
gulp.task('watch', function() {
gulp.watch('src/**/*.js', ['build_source']);
gulp.watch('src/styles/main.css', ['build_styles']);
});
function do_rollup(source, destination) {
return rollup.rollup({
input: source,
plugins: [
resolve(),
commonjs()
]
})
.then(bundle => {
return bundle.write({
file: destination,
format: 'iife',
// name: 'library',
// sourcemap: true
});
});
}
function do_closure_compile(source, destination) {
return new Promise(function(resolve, reject) {
gulp.src(source, {base: destination})
.pipe(compiler({
compilationLevel: 'ADVANCED',
// compilationLevel: 'SIMPLE',
// compilationLevel: 'WHITESPACE_ONLY',
warningLevel: 'QUIET',
// outputWrapper: '(function(){\n%output%\n}).call(this)',
jsOutputFile: 'library.cjs.js', // outputs single file
languageIn: 'ECMASCRIPT6',
languageOut: 'ECMASCRIPT6',
rewritePolyfills: false,
// assumeFunctionWrapper: true,
// createSourceMap: true,
}))
.pipe(gulp.dest(destination))
.on('error', reject)
.on('end', resolve)
})
}
function do_clean_css(source, destination) {
return new Promise(function(resolve, reject) {
gulp.src(source)
.pipe(cleanCSS())
.pipe(gulp.dest(destination))
.on('error', reject)
.on('end', resolve)
})
}
function do_inline_source(source, destination) {
return new Promise(function(resolve, reject) {
gulp.src(source)
.pipe(inlinesource({compress: false}))
.pipe(rename('index.html'))
.pipe(gulp.dest(destination))
.on('error', reject)
.on('end', resolve)
})
}
function postbuild() {
// fs.unlinkSync('./dist/script.js')
// fs.unlinkSync('./dist/style.css')
let output = fs.createWriteStream('./dist/build.zip')
let archive = archiver('zip', {
zlib: { level: 9 } // set compression to best
})
output.on('close', function() {
let usedBytes = archive.pointer();
let totalBytes = 13312;
console.log(usedBytes + '/' + totalBytes + ' total bytes (' + (Math.ceil(usedBytes / totalBytes * 1000000) / 10000) + '%)');
});
archive.on('warning', function(err) {
if (err.code === 'ENOENT') {
console.warn(err)
} else {
throw err;
}
});
archive.on('error', function(err) {
throw err;
});
archive.pipe(output);
archive.append(
fs.createReadStream('./dist/index.html'), {
name: 'index.html'
})
archive.finalize()
}