-
Notifications
You must be signed in to change notification settings - Fork 2
/
defineGulpTasks.js
127 lines (108 loc) · 4.5 KB
/
defineGulpTasks.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
import browserSync from 'browser-sync';
import { glob } from 'glob';
import gulpIf from 'gulp-if';
import gulpPostCss from 'gulp-postcss';
import gulpDartSass from 'gulp-dart-sass';
import gulpSourcemaps from 'gulp-sourcemaps';
import gulpSvg from 'gulp-svgo';
import merge from 'merge-stream';
import path from 'path';
import * as rollup from 'rollup';
import { nodeResolve as rollupNodeResolve } from '@rollup/plugin-node-resolve';
import rollupCommonJS from '@rollup/plugin-commonjs';
import nunjucksRendererPipe from './lib/rendering/nunjucks-renderer-pipe.js';
import postCssPlugins from './postcss.config.js';
import svgConfig from './svgo-config.js';
function setupGulpTasks(gulp) {
const isProduction = process.env.NODE_ENV === 'production';
const isDevelopment = !isProduction;
const sassOptions = {
includePaths: ['./node_modules/normalize.css', './node_modules/prismjs/themes'],
};
const scripts = glob.sync('./src/prototypes/**/index.js').map((entryPoint) => ({
entryPoint: entryPoint,
outputFile: entryPoint.replace(/.*src\//, ''),
}));
gulp.task('prototype-kit:clean', () => {
return Promise.resolve();
});
function createBuildScriptTask({ entryPoint, outputFile }) {
const taskName = `prototype-kit:build-script:${outputFile}`;
gulp.task(taskName, async () => {
const bundle = await rollup.rollup({
input: entryPoint,
plugins: [
rollupNodeResolve(),
rollupCommonJS({
include: /node_modules/,
}),
],
});
await bundle.write({
file: `./build/${outputFile}`,
format: 'cjs',
name: path.basename(outputFile),
sourcemap: true,
});
});
return taskName;
}
gulp.task('prototype-kit:build-script', gulp.series(...scripts.map(createBuildScriptTask)));
gulp.task('prototype-kit:build-styles', () => {
return gulp
.src(['./src/**/*.scss', '!**/_*/**'])
.pipe(gulpIf(isDevelopment, gulpSourcemaps.init()))
.pipe(gulpDartSass(sassOptions).on('error', gulpDartSass.logError))
.pipe(gulpIf(isProduction, gulpPostCss(postCssPlugins())))
.pipe(gulpIf(isDevelopment, gulpSourcemaps.write('./')))
.pipe(gulp.dest('./build'))
.pipe(browserSync.stream());
});
gulp.task('prototype-kit:build-svg', () => {
return gulp.src('./src/**/*.svg').pipe(gulpSvg(svgConfig)).pipe(gulp.dest('./build')).pipe(browserSync.stream());
});
gulp.task('prototype-kit:build-pages', () => {
return gulp.src(['./src/**/*.njk', '!**/_*/**']).pipe(nunjucksRendererPipe).pipe(gulp.dest('./build'));
});
gulp.task('prototype-kit:copy-design-system-files', () => {
return merge([
gulp.src('./node_modules/@ons/design-system/[0-9]*/**/*').pipe(gulp.dest('./build')),
gulp.src('./node_modules/@ons/design-system/css/**/*').pipe(gulp.dest('./build/css')),
gulp.src('./node_modules/@ons/design-system/favicons/**/*').pipe(gulp.dest('./build/favicons')),
gulp.src('./node_modules/@ons/design-system/fonts/**/*').pipe(gulp.dest('./build/fonts')),
gulp.src('./node_modules/@ons/design-system/img/**/*').pipe(gulp.dest('./build/img')),
gulp.src('./node_modules/@ons/design-system/scripts/**/*').pipe(gulp.dest('./build/scripts')),
]);
});
gulp.task('prototype-kit:copy-js-files', () => {
return gulp.src('./src/js/*.js').pipe(gulp.dest('./build/js'));
});
gulp.task('prototype-kit:watch-and-build', async () => {
browserSync.init({
proxy: 'localhost:3010',
});
gulp.watch('./src/**/*.njk').on('change', browserSync.reload);
gulp.watch('./src/**/*.scss', gulp.series('prototype-kit:build-styles'));
gulp.watch('./src/**/*.js', gulp.series('prototype-kit:build-script'));
gulp.watch('./src/svg/**/*.svg', gulp.series('prototype-kit:build-svg'));
});
gulp.task('prototype-kit:start-dev-server', async () => {
await import('./lib/dev-server.js');
});
gulp.task(
'prototype-kit:build-assets',
gulp.series(
'prototype-kit:copy-design-system-files',
'prototype-kit:build-script',
'prototype-kit:build-styles',
'prototype-kit:build-svg',
),
);
gulp.task(
'prototype-kit:start',
gulp.series('prototype-kit:build-assets', 'prototype-kit:watch-and-build', 'prototype-kit:start-dev-server'),
);
gulp.task('prototype-kit:watch', gulp.series('prototype-kit:watch-and-build', 'prototype-kit:start-dev-server'));
gulp.task('prototype-kit:build', gulp.series('prototype-kit:build-assets', 'prototype-kit:build-pages'));
}
export default setupGulpTasks;