-
Notifications
You must be signed in to change notification settings - Fork 50
/
gulpfile.js
177 lines (159 loc) · 5.13 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
const gulp = require('gulp');
const sourcemaps = require('gulp-sourcemaps');
const source = require('vinyl-source-stream');
const buffer = require('vinyl-buffer');
const browserify = require('browserify');
const watchify = require('watchify');
const babelify = require('babelify');
const stripComments = require('gulp-strip-comments');
const rename = require('gulp-rename');
const webserver = require('gulp-webserver');
const uglify = require('gulp-uglify');
const gutil = require('gulp-util');
const sass = require('gulp-sass');
const pump = require('pump');
const mocha = require('gulp-mocha');
const handlebars = require('gulp-handlebars');
const wrap = require('gulp-wrap');
const concat = require('gulp-concat');
const declare = require('gulp-declare');
const autoprefixer = require('gulp-autoprefixer');
const FILENAME = 'videojs-annotation-comments.js';
const CJSFILENAME = 'videojs-annotation-comments.cjs.js';
const PACKAGE = require('./package.json');
// compilation function for browserify/bundler/transpilation
function compile(watch, cb) {
const bundlerDefault = {
entry: './src/js/index.js',
browserifyOptions: { debug: true },
transformOptions: {},
filename: FILENAME
};
const bundlerCjs = {
entry: './src/js/annotation_comments.js',
browserifyOptions: {
debug: true,
standalone: 'AnnotationComments'
},
transformOptions: {
presets: ['es2015'],
plugins: ['babel-plugin-transform-es2015-modules-simple-commonjs']
},
filename: CJSFILENAME
};
function buildStream(bundler) {
return browserify(bundler.entry, bundler.browserifyOptions).transform(
babelify,
bundler.transformOptions
);
}
function rebundle(bundlers, watch) {
bundlers.forEach((b) => {
const browserifyStream = watch ? watchify(buildStream(b)) : buildStream(b);
browserifyStream
.bundle()
.on('log', gutil.log)
.on('error', gutil.log.bind(gutil.colors.red, 'Browserify Error'))
.pipe(source('src/js/index.js'))
.pipe(buffer())
.pipe(rename(b.filename))
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('./build'));
});
}
if (watch) {
rebundle([bundlerDefault], true);
buildStream(bundlerDefault).on('update', function () {
console.log('-> bundling...');
rebundle([bundlerDefault]);
});
} else {
rebundle([bundlerDefault, bundlerCjs], false);
cb();
}
}
gulp.task('dev_webserver', () => {
console.log(':::: > Test page at http://localhost:3004/test.html');
return gulp.src(['build', 'test', 'node_modules']).pipe(webserver({ port: 3004 }));
});
gulp.task('sass', () => {
return gulp
.src('src/css/*.scss')
.pipe(sass().on('error', sass.logError))
.pipe(
autoprefixer({
browsers: ['defaults', 'not ie <= 9'],
cascade: false
})
)
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('./build/css'))
.pipe(gulp.dest('./docs/build/css'));
});
gulp.task('templates:watch', () => {
gulp.watch('./src/templates/**/*.hbs', ['templates']);
});
gulp.task('sass:watch', () => {
gulp.watch('./src/css/**/*.scss', ['sass']);
});
gulp.task('transpile:watch', () => {
gulp.watch('./src/js/**/*', ['transpile']);
});
gulp.task('templates', () => {
gulp
.src('./src/templates/**/*.hbs')
.pipe(
handlebars({
handlebars: require('handlebars')
})
)
.pipe(wrap('Handlebars.template(<%= contents %>)'))
.pipe(
declare({
root: 'exports',
noRedeclare: true, // Avoid duplicate declarations
namespace: 'templates',
processName: function (filePath) {
// Allow nesting based on path using gulp-declare's processNameByPath()
// You can remove this option completely if you aren't using nested folders
// Drop the templates/ folder from the namespace path by removing it from the filePath
return declare.processNameByPath(filePath.replace('src/templates/', ''));
}
})
)
.pipe(concat('templates.js'))
.pipe(wrap('var Handlebars = require("handlebars/runtime");\n <%= contents %>'))
.pipe(gulp.dest('./src/js/compiled/'));
});
gulp.task('build', ['templates', 'sass', 'transpile'], (cb) => {
pump([
gulp.src('build/videojs-annotation-comments.js'),
rename(FILENAME.replace('.js', '.min.js')),
stripComments(),
uglify(),
gulp.dest('./build'),
gulp.dest('./docs/build')
]);
pump(
[
gulp.src('build/videojs-annotation-comments.cjs.js'),
rename(CJSFILENAME.replace('.js', '.min.js')),
stripComments(),
uglify(),
gulp.dest('./build'),
gulp.dest('./docs/build')
],
cb
);
});
gulp.task('test', () => {
gulp.src(['test/mocha/components/*.js'], { read: false }).pipe(mocha());
});
gulp.task('tdd', function () {
gulp.watch(['src/**/*.js', 'src/**/.hbs', 'test/mocha/components/*.js'], ['test']);
});
gulp.task('transpile', (cb) => compile(false, cb));
gulp.task('bundle_watch', (cb) => compile(true, cb));
gulp.task('watch', ['dev_webserver', 'templates:watch', 'sass:watch', 'transpile:watch', 'tdd']);
gulp.task('default', ['watch']);