-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
133 lines (114 loc) · 4.12 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
const browserSync = require('browser-sync');
const browserify = require('browserify');
const gulp = require('gulp');
const gulpIf = require('gulp-if');
const gulpPostCss = require('gulp-postcss');
const gulpDartSass = require('gulp-dart-sass');
const gulpSourcemaps = require('gulp-sourcemaps');
const gulpTerser = require('gulp-terser');
const buffer = require('vinyl-buffer');
const source = require('vinyl-source-stream');
require('@babel/register');
const babelEsmConfig = require('./babel.conf.esm');
const babelNomoduleConfig = require('./babel.conf.nomodule');
const nunjucksRendererPipe = require('./lib/rendering/nunjucks-renderer-pipe.js').default;
const searchIndexPipe = require('./lib/rendering/search-index-pipe.js').default;
const postCssPlugins = require('./postcss.config').default;
const isProduction = process.env.NODE_ENV === 'production';
const isDevelopment = !isProduction;
const terserOptions = {
compress: {
drop_console: true,
},
};
const sassOptions = {
includePaths: ['./node_modules/normalize.css', './node_modules/prismjs/themes'],
outputStyle: 'compressed',
};
const scripts = [
{
entryPoint: './src/js/main.js',
outputFile: 'main.js',
config: babelEsmConfig,
},
{
entryPoint: ['./src/js/polyfills.js', './src/js/main.js'],
outputFile: 'main.es5.js',
config: babelNomoduleConfig,
},
{
entryPoint: './src/js/patternlib/index.js',
outputFile: 'patternlib.js',
config: babelEsmConfig,
},
{
entryPoint: './src/js/patternlib/index.js',
outputFile: 'patternlib.es5.js',
config: babelNomoduleConfig,
},
];
gulp.task('clean', () => {
return Promise.resolve();
});
function createBuildScriptTask({ entryPoint, outputFile, config }) {
const taskName = `build-script:${outputFile}`;
gulp.task(taskName, () => {
return browserify(entryPoint, { debug: isDevelopment })
.transform('babelify', { ...config, sourceMaps: isDevelopment })
.bundle()
.pipe(source(outputFile))
.pipe(buffer())
.pipe(gulpIf(isDevelopment, gulpSourcemaps.init({ loadMaps: true })))
.pipe(gulpIf(isProduction, gulpTerser(terserOptions)))
.pipe(gulpIf(isDevelopment, gulpSourcemaps.write('./')))
.pipe(gulp.dest('./build/scripts'))
.pipe(browserSync.stream());
});
return taskName;
}
gulp.task('build-script', gulp.series(...scripts.map(createBuildScriptTask)));
gulp.task('build-styles', () => {
return gulp
.src(`./src/scss/*.scss`)
.pipe(gulpIf(isDevelopment, gulpSourcemaps.init()))
.pipe(gulpDartSass(sassOptions).on('error', gulpDartSass.logError))
.pipe(gulpPostCss(postCssPlugins()))
.pipe(gulpIf(isDevelopment, gulpSourcemaps.write('./')))
.pipe(gulp.dest('./build/css'))
.pipe(browserSync.stream());
});
gulp.task('build-pages', () => {
return gulp
.src(['./src/**/*.njk', '!**/_*/**'])
.pipe(nunjucksRendererPipe)
.pipe(gulp.dest('./build'));
});
gulp.task('build-search-index', () => {
return gulp
.src('./src/search-index.json')
.pipe(searchIndexPipe)
.pipe(gulp.dest('./build'));
});
gulp.task('copy-static-files', () => {
return gulp.src('./src/static/**/*').pipe(gulp.dest('./build'));
});
gulp.task('copy-js-files', () => {
return gulp.src('./src/js/*.js').pipe(gulp.dest('./build/js'));
});
gulp.task('watch-and-build', async () => {
browserSync.init({
proxy: 'localhost:3010',
});
gulp.watch('./src/**/*.njk').on('change', browserSync.reload);
gulp.watch('./src/**/*.scss', gulp.series('build-styles'));
gulp.watch('./src/**/*.js', gulp.series('build-script'));
});
gulp.task('start-dev-server', async () => {
await import('./lib/dev-server.js');
});
gulp.task('build-assets', gulp.series('build-script', 'build-styles', 'build-search-index'));
gulp.task('build-assets-for-testing', gulp.series('build-script', 'build-styles'));
gulp.task('start', gulp.series('build-assets', 'watch-and-build', 'start-dev-server'));
gulp.task('watch', gulp.series('watch-and-build', 'start-dev-server'));
gulp.task('build', gulp.series('copy-static-files', 'build-assets', 'build-pages'));
gulp.task('build-package', gulp.series('copy-static-files', 'copy-js-files', 'build-assets'));