-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
141 lines (126 loc) · 4.55 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
var gulp = require('gulp'),
browserify = require('browserify'),
watchify = require('watchify'),
tsify = require('tsify'),
editJson = require('gulp-json-editor'),
less = require('gulp-less'),
pug = require('gulp-pug'),
zip = require('gulp-zip'),
logger = require('gulp-logger'),
gutil = require('gulp-util'),
source = require('vinyl-source-stream'),
path = require('path'),
exec = require('child_process').exec,
Promise = require('es6-promise').Promise;
function buildScript(entry, watch) {
let bundler = browserify({
entries: entry,
debug: true,
plugin: [tsify],
cache: {}, packageCache: {} // for watchify
});
function rebundle() {
return bundler
.bundle()
.on('error', function(error) {
gutil.log(error.toString());
this.emit('end');
})
.pipe(source(path.basename(entry).replace(/\.ts$/, '.js')))
.pipe(gulp.dest('app/js'))
.pipe(logger({ beforeEach: '[ts] wrote: ' }));
}
// watchify を有効にする
if (watch) {
// watchify は file イベントによって追加されたファイルの更新をすべて update イベントで
// 通知してしまう。 entry の依存に含まれるファイルの更新のみを検知して再生成したいため、
// watchify が内部で検出している依存関係を proxy して依存の有無を判定できるようにする。
let deps = {};
bundler._options.cache = new Proxy(bundler._options.cache, {
set: (cache, file, dep) => {
cache[file] = dep;
deps[file] = true;
}
});
bundler.on('reset', () => { deps = {}; });
bundler.on('update', (files) => {
if (files.some((file) => { return deps[file]; })) rebundle();
});
bundler.plugin(watchify);
}
return rebundle();
}
var targets = [
{
name: 'content',
entry: './src/content.ts'
},
{
name: 'background',
entry: './src/background.ts'
},
{
name: 'options',
entry: './src/options.ts'
}
];
for (let t of targets) {
gulp.task(`${t.name}-browserify`, () => { return buildScript(t.entry, false); });
gulp.task(`${t.name}-watchify`, () => { return buildScript(t.entry, true); });
}
gulp.task('typescript', gulp.parallel(targets.map(t => `${t.name}-browserify`)));
gulp.task('typescript:watch', gulp.parallel(targets.map(t => `${t.name}-watchify`)));
gulp.task('less', () => {
return gulp.src('src/**/*.less')
.pipe(less())
.pipe(gulp.dest('app/css'))
.pipe(logger({ beforeEach: '[less] wrote: ' }));
});
gulp.task('pug', () => {
return gulp.src('src/**/*.pug')
.pipe(pug({ pretty: true }))
.pipe(gulp.dest('app/html'))
.pipe(logger({ beforeEach: '[pug] wrote: ' }));
});
gulp.task('img', () => {
return gulp.src('src/img/sized/*.png')
.pipe(gulp.dest('app/img'))
.pipe(logger({ beforeEach: '[img] wrote: ' }));
});
gulp.task('manifest', () => {
return version().then(function(version) {
return gulp.src('src/manifest.json')
.pipe(editJson({ version : version }))
.pipe(gulp.dest('app/'))
.pipe(logger({ beforeEach: '[manifest] wrote: ' }));
});
});
function version() {
var promise = new Promise(function(resolve, reject) {
exec('git describe --tags --always --dirty', function(err, stdout, stderr) {
return err ? reject(err) : resolve(stdout);
});
});
return promise.then(function(desc) {
var version = desc.replace(/\n$/, '')
.replace(/-(\d+)/, '.$1')
.replace(/-g[0-9a-f]+/, '')
.replace(/-dirty/, '');
return version;
});
}
gulp.task('build', gulp.parallel(['typescript', 'less', 'pug', 'img', 'manifest']));
gulp.task('zip', () => {
return version().then(function(version) {
return gulp.src('app/**/*')
.pipe(zip('compaito-' + version + '.zip'))
.pipe(gulp.dest('releases'));
});
});
gulp.task('package', gulp.series('build', 'zip'));
gulp.task('watch', gulp.parallel(['typescript:watch', () => {
gulp.watch('src/**/*.less', gulp.parallel(['less']));
gulp.watch('src/**/*.pug', gulp.parallel(['pug']));
gulp.watch('src/manifest.json', gulp.parallel(['manifest']));
}]));
gulp.task('default', gulp.series('build', 'watch'));