-
Notifications
You must be signed in to change notification settings - Fork 2
/
gulpfile.js
205 lines (173 loc) · 5.33 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
/* eslint-env node */
const path = require('path');
const {task, src, dest, series, parallel} = require('gulp');
const sass = require('gulp-dart-sass');
const rename = require('gulp-rename');
const replace = require('gulp-replace');
const ts = require('gulp-typescript');
const rimraf = require('rimraf');
const BUILD_CLIENT_DIR = path.resolve('build');
const SERVER_CLIENT_DIR = path.resolve('server');
const ESM_DIR = 'esm';
const CJS_DIR = 'cjs';
const SASS_LOADER_OPTIONS = {
includePaths: ['./node_modules'],
};
const TS_CONFIG_FILENAME = 'tsconfig.json';
const CONFIG_EXTENSION_FOR_DECLARATION = {
emitDeclarationOnly: true,
isolatedModules: false,
declaration: true,
};
const CONFIG_EXTENSION_FOR_COMPILE = {
incremental: true,
isolatedModules: true,
};
const SRC_FOR_INDEX_BUILD = [
'src/**/*.{ts,tsx,js,jsx}',
'!src/demo/**/*',
'!src/stories/**/*',
'!src/**/__stories__/**/*',
'!src/**/__tests__/**/*',
'!src/data/**/*',
'!src/server.ts',
'!test-utils/**/*',
];
const SRC_FOR_SERVER_REQUIREMENTS = [
'src/data/**/*.{js,jsx,ts,tsx}',
'src/models/**/*.{js,jsx,ts,tsx}',
];
const getTsConfig = ({filename = TS_CONFIG_FILENAME, modules = false, extension = {}}) => {
return ts.createProject(filename, {
module: modules ? 'esnext' : 'commonjs',
...extension,
});
};
task('clean', (done) => {
rimraf.sync(BUILD_CLIENT_DIR);
rimraf.sync(SERVER_CLIENT_DIR);
rimraf.sync('styles/**/*.css');
done();
});
function compileTs(modules = false) {
const tsProject = getTsConfig({
modules,
extension: CONFIG_EXTENSION_FOR_COMPILE,
});
return src(SRC_FOR_INDEX_BUILD)
.pipe(
replace(/import '.+\.scss';/g, (match) =>
modules ? match.replace('.scss', '.css') : '',
),
)
.pipe(tsProject())
.pipe(dest(path.resolve(BUILD_CLIENT_DIR, modules ? ESM_DIR : CJS_DIR)));
}
function compileTsDeclaration(modules = false) {
const tsProject = getTsConfig({
modules,
extension: CONFIG_EXTENSION_FOR_DECLARATION,
});
return src(SRC_FOR_INDEX_BUILD)
.pipe(
replace(/import '.+\.scss';/g, (match) =>
modules ? match.replace('.scss', '.css') : '',
),
)
.pipe(tsProject())
.pipe(dest(path.resolve(BUILD_CLIENT_DIR, modules ? ESM_DIR : CJS_DIR)));
}
function compileServerFile() {
const tsProject = getTsConfig({
extension: {
declaration: true,
isolatedModules: false,
},
});
return src('src/server.ts')
.pipe(rename('index.js'))
.pipe(tsProject())
.pipe(dest(path.resolve(SERVER_CLIENT_DIR)));
}
function compileServerRequirements() {
const tsProject = getTsConfig({
extension: CONFIG_EXTENSION_FOR_COMPILE,
});
return src(SRC_FOR_SERVER_REQUIREMENTS, {base: 'src'})
.pipe(tsProject())
.pipe(dest(path.resolve(SERVER_CLIENT_DIR)));
}
function compileServerRequirementsDeclaration() {
const tsProject = getTsConfig({
extension: CONFIG_EXTENSION_FOR_DECLARATION,
});
return src(SRC_FOR_SERVER_REQUIREMENTS, {base: 'src'})
.pipe(tsProject())
.pipe(dest(path.resolve(SERVER_CLIENT_DIR)));
}
task('compile-to-esm', () => {
return compileTs(true);
});
task('compile-to-cjs', () => {
return compileTs();
});
task('compile-to-esm-declaration', () => {
return compileTsDeclaration(true);
});
task('compile-to-cjs-declaration', () => {
return compileTsDeclaration();
});
task('compile-server-file', () => {
return compileServerFile();
});
task('compile-server-requirements', () => {
return compileServerRequirements();
});
task('compile-server-requirements-declaration', () => {
return compileServerRequirementsDeclaration();
});
task('copy-js-declarations', () => {
return src([
'src/**/*.d.ts',
'!src/demo/**/*.d.ts',
'!src/stories/**/*.d.ts',
'!src/**/__stories__/**/*.d.ts',
'!src/**/__tests__/**/*.d.ts',
'!test-utils/**/*.d.ts',
])
.pipe(dest(path.resolve(BUILD_CLIENT_DIR, ESM_DIR)))
.pipe(dest(path.resolve(BUILD_CLIENT_DIR, CJS_DIR)));
});
task('copy-i18n', () => {
return src(['src/**/i18n/*.json'])
.pipe(dest(path.resolve(BUILD_CLIENT_DIR, ESM_DIR)))
.pipe(dest(path.resolve(BUILD_CLIENT_DIR, CJS_DIR)));
});
task('styles-global', () => {
return src('styles/*.scss')
.pipe(sass(SASS_LOADER_OPTIONS).on('error', sass.logError))
.pipe(dest('styles'));
});
task('styles-components', () => {
return src([`src/**/*.scss`, `!src/**/__stories__/**/*.scss`])
.pipe(sass(SASS_LOADER_OPTIONS).on('error', sass.logError))
.pipe(dest(path.resolve(BUILD_CLIENT_DIR, ESM_DIR)))
.pipe(dest(path.resolve(BUILD_CLIENT_DIR, CJS_DIR)));
});
task(
'build',
series([
'clean',
parallel(['compile-to-esm', 'compile-to-cjs']),
parallel(['compile-to-esm-declaration', 'compile-to-cjs-declaration']),
parallel([
'compile-server-file',
'compile-server-requirements',
'compile-server-requirements-declaration',
]),
'copy-js-declarations',
'copy-i18n',
parallel(['styles-global', 'styles-components']),
]),
);
task('default', series(['build']));