-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.mjs
124 lines (108 loc) · 3.77 KB
/
gulpfile.mjs
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
// General imports
import autoprefixer from 'autoprefixer';
import branch from 'branch-pipe';
import gulp from 'gulp';
import sassBackend from 'sass';
import { Transform } from 'stream';
// Processors
import babel from 'gulp-babel';
import concat from 'gulp-concat';
import filter from 'gulp-filter';
import map from 'gulp-map';
import rename from 'gulp-rename';
import postcss from 'gulp-postcss';
import createSassProcessor from 'gulp-sass';
const sass = createSassProcessor(sassBackend);
const SASS_SOURCES = 'src/**/*.scss';
const SASS_DECLARATIONS = 'src/styles/**/*.scss';
const SASS_MODULES_STYLESHEETS = ['src/**/*.scss', '!src/styles/**/*.scss'];
const SVG_ICONS = 'src/**/*.svg';
const TYPESCRIPT_SOURCES = ['src/**/*.{ts,tsx}', '!src/**/*.test.*'];
const createCommonjsCompiler = () => babel({ extends: './babel.commonjs.config.cjs' });
const createEsmCompiler = () => babel({ extends: './babel.esm.config.cjs' });
gulp.task('build:cjs', function () {
return gulp
.src([...TYPESCRIPT_SOURCES, SVG_ICONS])
.pipe(
branch.obj((src) => [
src
.pipe(filter(TYPESCRIPT_SOURCES))
.pipe(createCommonjsCompiler())
.pipe(rename((file) => (file.extname = '.cjs'))),
src
.pipe(filter(SVG_ICONS))
.pipe(createCommonjsCompiler())
.pipe(rename((file) => (file.extname = '.svg.cjs'))),
]),
)
.pipe(gulp.dest('build/cjs/'));
});
gulp.task('watch:cjs', watch([...TYPESCRIPT_SOURCES, SVG_ICONS], 'build:cjs'));
gulp.task('build:esm', function () {
return gulp
.src([...TYPESCRIPT_SOURCES, SVG_ICONS])
.pipe(
branch.obj((src) => [
src
.pipe(filter(TYPESCRIPT_SOURCES))
.pipe(createEsmCompiler())
.pipe(rename((file) => (file.extname = '.mjs'))),
src
.pipe(filter(SVG_ICONS))
.pipe(createEsmCompiler())
.pipe(rename((file) => (file.extname = '.svg.mjs'))),
]),
)
.pipe(gulp.dest('build/esm/'));
});
gulp.task('watch:esm', watch([...TYPESCRIPT_SOURCES, SVG_ICONS], 'build:esm'));
gulp.task('build:sass', function () {
return gulp
.src(SASS_SOURCES)
.pipe(branch.obj((src) => [copySassDeclarations(src), compileComponentsStylesheets(src)]))
.pipe(gulp.dest('build/'));
});
gulp.task('watch:sass', watch(SASS_SOURCES, 'build:sass'));
/**
* @param {string|string[]}files
* @param {string} build
* @returns {*}
*/
function watch(files, build) {
return gulp.series(build, function () {
return gulp
.watch(files, gulp.series(build))
.on('ready', () => console.log('Watching files'))
.on('all', (event, path) => console.log(`[${event}] ${path}`));
});
}
/**
* @param {Transform} stream
* @returns {Transform}
*/
function copySassDeclarations(stream) {
return stream.pipe(filter(SASS_DECLARATIONS));
}
/**
* @param {Transform} stream
* @returns {Transform}
*/
function compileComponentsStylesheets(stream) {
/**
* @param {vinyl:File} file
* @returns {vinyl:File}
*/
function toSassIndex(file) {
const path = file.path.replace(file.base, '.').replace(/\.scss$/, '');
const index = file.clone({ contents: false });
index.contents = Buffer.from(`@import "${path}";\n`);
return index;
}
return stream
.pipe(filter(SASS_MODULES_STYLESHEETS))
.pipe(map(toSassIndex))
.pipe(concat('styles.scss'))
.pipe(sass({ importPath: 'src/' }))
.pipe(postcss([autoprefixer({ grid: true })]))
.pipe(rename({ extname: '.css', dirname: 'styles/' }));
}