forked from carbon-design-system/carbon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
293 lines (249 loc) · 6.43 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
'use strict';
// Node
const path = require('path');
// Styles
const sass = require('gulp-sass');
const autoprefixer = require('gulp-autoprefixer');
// Javascript deps
const babel = require('gulp-babel');
const eslint = require('gulp-eslint');
const uglify = require('gulp-uglify');
const pump = require('pump');
// BrowserSync
const browserSync = require('browser-sync').create();
// Gulp
const gulp = require('gulp');
const rename = require('gulp-rename');
const sourcemaps = require('gulp-sourcemaps');
const gutil = require('gulp-util');
const jsdoc = require('gulp-jsdoc3');
// Generic utility
const del = require('del');
const exec = require('child_process').exec;
// Test environment
const Server = require('karma').Server;
const cloptions = require('minimist')(process.argv.slice(2), {
alias: {
k: 'keepalive',
},
boolean: ['keepalive'],
});
/**
* BrowserSync
*/
gulp.task('browser-sync', ['build:dev'], () => {
browserSync.init({
logPrefix: 'Carbon Components',
open: false,
proxy: 'localhost:8080',
timestamps: false,
});
});
/**
* Clean
*/
// Use: npm run prebuild
gulp.task('clean', () => del([
'scss',
'css',
'es',
'umd',
'scripts',
'html',
'dist',
'demo/**/*.{js,map}',
'!demo/js/demo-switcher.js',
'!demo/js/theme-switcher.js',
'!demo/index.js',
'!demo/polyfills/*.js',
]));
/**
* JavaScript Tasks
*/
gulp.task('scripts:dev', (cb) => {
exec('./node_modules/.bin/rollup -c tools/rollup.config.dev.js', (err) => {
browserSync.reload();
cb(err);
});
});
gulp.task('scripts:umd', () => {
const srcFiles = ['./src/**/*.js'];
const babelOpts = {
presets: [
['env', {
targets: {
browsers: ['last 1 version', 'ie >= 11'],
},
}],
],
plugins: ['transform-es2015-modules-umd', 'transform-class-properties'],
};
return gulp.src(srcFiles)
.pipe(babel(babelOpts))
.pipe(gulp.dest('umd/'));
});
gulp.task('scripts:es', () => {
const srcFiles = ['./src/**/*.js'];
const babelOpts = {
presets: [
['env', {
modules: false,
targets: {
browsers: ['last 1 version', 'ie >= 11'],
},
}],
],
plugins: ['transform-class-properties'],
};
return gulp.src(srcFiles)
.pipe(babel(babelOpts))
.pipe(gulp.dest('es/'));
});
gulp.task('scripts:rollup', (cb) => {
exec('./node_modules/.bin/rollup -c tools/rollup.config.js', (err) => {
cb(err);
});
});
gulp.task('scripts:compiled', ['scripts:rollup'], (cb) => {
const srcFile = './scripts/carbon-components.js';
pump([
gulp.src(srcFile),
uglify(),
rename('carbon-components.min.js'),
gulp.dest('scripts'),
], cb);
});
/**
* Sass Tasks
*/
gulp.task('sass:compiled', () => {
function buildStyles(prod) {
return gulp.src('src/globals/scss/styles.scss')
.pipe(sourcemaps.init())
.pipe(sass({
outputStyle: prod ? 'compressed' : 'expanded',
}).on('error', sass.logError))
.pipe(autoprefixer({
browsers: ['> 1%', 'last 2 versions'],
}))
.pipe(rename((filePath) => {
if (filePath.basename === 'styles') {
filePath.basename = 'carbon-components';
}
if (prod) {
filePath.extname = `.min${filePath.extname}`;
}
}))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('css'))
.pipe(browserSync.stream());
}
buildStyles(); // Expanded CSS
buildStyles(true); // Minified CSS
});
gulp.task('sass:dev', () =>
gulp.src('demo/**/demo.scss')
.pipe(sourcemaps.init())
.pipe(sass({
outputStyle: 'expanded',
}).on('error', sass.logError))
.pipe(autoprefixer({
browsers: ['> 1%', 'last 2 versions'],
}))
.pipe(rename({ dirname: '' }))
.pipe(sourcemaps.write())
.pipe(gulp.dest('demo'))
.pipe(browserSync.stream()));
gulp.task('sass:source', () => {
const srcFiles = './src/**/*.scss';
return gulp.src(srcFiles)
.pipe(gulp.dest('scss'));
});
gulp.task('html:source', () => {
const srcFiles = './src/components/**/*.html';
return gulp.src(srcFiles)
.pipe(gulp.dest('html'));
});
/**
* Lint
*/
gulp.task('lint', () =>
gulp.src([
'gulpfile.js',
'server.js',
'src/**/*.js',
'tests/**/*.js',
'demo/**/*.js',
])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError())
.pipe(eslint.results((results) => {
const count = results.warningCount;
if (count > 0) {
throw new gutil.PluginError('gulp-eslint', {
name: 'ESLintWarning',
message: `Has ${count} warning${count > 1 ? 's' : ''}`,
});
}
})));
/**
* JSDoc
*/
gulp.task('jsdoc', (cb) => {
gulp.src('./src/**/*.js')
.pipe(babel({
plugins: ['transform-class-properties'],
babelrc: false,
}))
.pipe(gulp.dest('./docs/js/tmp'))
.on('end', () => {
gulp.src(['README.md', 'docs/js/tmp/**/*.js'], { read: false })
.pipe(jsdoc(Object.assign(require('gulp-jsdoc3/dist/jsdocConfig.json'), { // eslint-disable-line global-require
opts: {
destination: './docs/js',
},
}), (err) => {
if (err) {
cb(err);
} else {
del('./docs/js/tmp', cb);
}
}));
})
.on('error', cb);
});
/**
* Test
*/
gulp.task('test', ['test:unit', 'test:a11y']);
gulp.task('test:unit', (done) => {
new Server({
configFile: path.resolve(__dirname, 'tests/karma.conf.js'),
singleRun: !cloptions.keepalive,
}, done).start();
});
gulp.task('test:a11y', ['sass:compiled'], (done) => {
new Server({
configFile: path.resolve(__dirname, 'tests/karma-ibma.conf.js'),
singleRun: !cloptions.keepalive,
}, done).start();
});
// Watch Tasks
gulp.task('watch', () => {
gulp.watch('src/**/**/*.html').on('change', browserSync.reload);
gulp.watch(['src/**/**/*.js'], ['scripts:dev']);
gulp.watch(['src/**/**/*.scss', 'demo/**/*.scss'], ['sass:dev']);
});
gulp.task('serve', ['browser-sync', 'watch']);
// Build task collection
gulp.task('build:scripts', ['scripts:umd', 'scripts:es', 'scripts:compiled']);
gulp.task('build:styles', ['sass:compiled', 'sass:source']);
// Mapped to npm run build
gulp.task('build', ['build:scripts', 'build:styles', 'html:source']);
// For demo environment
gulp.task('build:dev', ['sass:dev', 'scripts:dev']);
gulp.task('default', () => {
// eslint-disable-next-line no-console
console.log('\n\n Please use `$ npm run dev` and navigate to \n http://localhost:3000 to view project locally \n\n');
});