forked from CMSgov/design-system
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
318 lines (287 loc) · 9.96 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
const fs = require('fs');
const path = require('path');
const gulp = require('gulp');
const gulpif = require('gulp-if');
const babel = require('gulp-babel');
const dartSass = require('sass');
const gulpSass = require('gulp-sass');
const sass = gulpSass(dartSass);
const sourcemaps = require('gulp-sourcemaps');
const ts = require('gulp-typescript');
const postcss = require('gulp-postcss');
const postcssImport = require('postcss-import');
const autoprefixer = require('autoprefixer');
const cssnano = require('cssnano');
const filter = require('gulp-filter');
const log = require('fancy-log');
const svgmin = require('gulp-svgmin');
const webpack = require('webpack-stream');
const generateWebpackConfig = require('./webpack.config');
/*
* command line arguments and global variables
*/
const args = require('yargs/yargs')(process.argv.slice(2)).argv;
const willMinifySvg = args.minifySvg ?? false;
const rootPath = args.package ?? path.join('packages', 'design-system');
const isCore = rootPath.includes('design-system') ?? false;
const corePackageFiles = path.join('packages', 'design-system', 'dist');
const distPath = path.join(rootPath, 'dist');
const distReactComponents = path.join(distPath, 'react-components');
const distPreactComponents = path.join(distPath, 'preact-components');
const distWebComponents = path.join(distPath, 'web-components');
const srcPath = path.join(rootPath, 'src');
const imageCorePath = path.join(corePackageFiles, 'images');
const sassCorePath = path.join(corePackageFiles, 'styles');
const fontsCorePath = path.join(corePackageFiles, 'fonts');
const nameTask = (fn, displayName) => {
fn.displayName = displayName;
return fn;
};
/**
* clean up dist folder if it exists
*/
const cleanDist = (cb) => {
if (fs.existsSync(distPath)) {
fs.readdirSync(distPath, (err, files) => {
files.forEach((f) => {
// don't clean out the scss folder that was just created
if (f !== 'scss') fs.rm(f, { recursive: true });
});
});
}
cb();
};
cleanDist.displayName = '🧹 cleaning up dist path';
/**
* Copy theme files from styles/themes to dist
*/
const copyThemes = (cb) => {
const themeFiles = `${srcPath}/styles/*-theme.css`;
gulp
.src(themeFiles)
.pipe(gulp.dest(path.join(distPath, 'css')))
.on('end', cb);
};
copyThemes.displayName = '📎 copying themes to dist/css folder';
/**
* compile sass assets to css, copy to /dist/css folder
*/
const compileSass = (cb) => {
const envDev = process.env.NODE_ENV === 'development';
const sassSourcePaths = isCore
? `${srcPath}/styles/**/*.scss`
: [`${sassCorePath}/**/*.scss`, `${srcPath}/styles/**/*.scss`];
gulp
.src(sassSourcePaths)
.pipe(gulpif(envDev, sourcemaps.init()))
.pipe(sass({ outputStyle: 'expanded' }))
.pipe(gulpif(envDev, sourcemaps.write()))
.pipe(
postcss([
postcssImport(), // inline imports
autoprefixer(), // add any necessary vendor prefixes
...(!envDev ? [cssnano()] : []), // minify css
])
)
.pipe(gulp.dest(path.join(distPath, 'css')))
.on('end', cb);
};
compileSass.displayName = '🖍 compiling sass into css';
/**
* copy image assets, minify svg files if necessary
*/
const copyImages = (cb) => {
// non-core packages get core image assets as well
const imageSourcePaths = isCore
? `${srcPath}/images/**/*`
: [`${imageCorePath}/**/*`, `${srcPath}/images/**/*`];
const filtered = filter(`**/*.svg`, { restore: true });
gulp
.src(imageSourcePaths)
.pipe(filtered) // we filter out svgs and minify if necessary
.pipe(
gulpif(
willMinifySvg,
svgmin({
plugins: [
{ cleanupIDs: false },
{ removeTitle: false },
{ removeDesc: false },
{ removeHiddenElems: false },
{ removeUnknownsAndDefaults: { keepRoleAttr: true } },
],
})
)
)
.pipe(filtered.restore) // then restore full glob for copying
.pipe(gulp.dest(path.join(distPath, 'images')))
.on('end', cb);
};
copyImages.displayName = '🖼 copying images to dist folder with optional minification';
/**
* copy font files to dist folder
*/
const copyFonts = (cb) => {
// non-core packages get core font assets as well
const fontSourcePaths = isCore
? `${srcPath}/fonts/**/*`
: [`${srcPath}/fonts/**/*`, `${fontsCorePath}/**/*`];
gulp
.src(fontSourcePaths)
.pipe(gulp.dest(path.join(distPath, 'fonts')))
.on('end', cb);
};
copyFonts.displayName = '📎 copying fonts to dist folder';
/**
* copy json files (internationalization) to dist/components, dist/esnext & dist/types
*/
const copyJSON = (cb) => {
gulp
.src(`${srcPath}/components/**/*.json`)
.pipe(gulp.dest(path.join(distReactComponents, 'cjs')))
.pipe(gulp.dest(path.join(distReactComponents, 'esm')))
.pipe(gulp.dest(path.join(distReactComponents, 'types')))
.pipe(gulp.dest(path.join(distPreactComponents, 'cjs')))
.pipe(gulp.dest(path.join(distPreactComponents, 'esm')))
.pipe(gulp.dest(path.join(distPreactComponents, 'types')))
.on('end', cb);
};
copyJSON.displayName = '📎 copying json data to dist folder';
/*
* jsSrcGlob used to create ts definitions and transpile to js, esnext
*/
const jsSrcGlob = [
`${srcPath}/components/**/*.{js,jsx,ts,tsx}`,
`!${srcPath}/components/**/*{.test,.test.interaction,.spec,.d,.stories}.{js,jsx,ts,tsx}`,
`!${srcPath}/components/setupTests.{js,jsx,ts,tsx}`,
`!${srcPath}/components/**/{__mocks__,__tests__}/**/*`,
];
const preactAliases = {
react: 'preact/compat',
'react-dom': 'preact/compat',
'react/jsx-runtime': 'preact/jsx-runtime',
};
const preactTsConfig = {
paths: {
react: [require.resolve('preact/compat')],
'react-dom': [require.resolve('preact/compat')],
},
};
const preactBabelConfig = { plugins: [['module-resolver', { alias: preactAliases }]] };
const esmBabelConfig = { presets: [['@babel/preset-env', { modules: false }]] };
const cjsBabelConfig = { presets: [['@babel/preset-env', { modules: 'commonjs' }]] };
/**
* Helper function that returns the actual task function to compile our source
* TypeScript into a target format and drop it at the specified location.
*/
const compileJs = (options) => (cb) => {
gulp
.src(jsSrcGlob, { base: path.join(srcPath, 'components') })
.pipe(babel(options.babelConfig))
.on('error', (error) => {
log.error('there was an error transpiling: ' + error);
})
.pipe(gulp.dest(options.dest))
.on('end', cb);
};
// More helper functions for compiling JS into spefific formats
const compileCjs = (dest, babelConfig = {}) =>
nameTask(
compileJs({ dest, babelConfig: { ...cjsBabelConfig, ...babelConfig } }),
`🔨 compiling typescript into cjs: ${path.relative(distPath, dest)}`
);
const compileEsm = (dest, babelConfig = {}) =>
nameTask(
compileJs({
dest,
babelConfig: { ...esmBabelConfig, ...babelConfig },
}),
`🔨 compiling typescript into esm: ${path.relative(distPath, dest)}`
);
/*
* create typescript definition files for the package
*/
const compileTypescriptDefs = (tsConfig = {}) =>
nameTask((cb) => {
const tsProject = ts.createProject('tsconfig.json', {
declaration: true,
allowJs: true,
...tsConfig,
});
gulp
.src(jsSrcGlob, { base: path.join(srcPath, 'components') })
.pipe(tsProject())
// They put the definitions-file stream in a "dts" property
.dts.pipe(gulp.dest(path.join(distReactComponents, 'types')))
.on('finish', cb);
}, '📖 generating typescript definition files');
/**
* Helper function that returns the actual task function that bundles our
* TypeScript source for distribution on the CDN
*/
const bundleJs = (options) => (cb) => {
gulp
.src(options.entryPath)
.pipe(webpack(generateWebpackConfig(options)))
.pipe(gulp.dest(options.dest))
.on('end', cb);
};
const bundleReactComponents = bundleJs({
entryPath: path.resolve(distReactComponents, 'esm', 'index.js'),
dest: path.join(distReactComponents, 'bundle'),
});
bundleReactComponents.displayName = '📦 bundling react components for cdn distribution';
const bundlePreactComponents = bundleJs({
entryPath: path.resolve(distPreactComponents, 'esm', 'index.js'),
dest: path.join(distPreactComponents, 'bundle'),
preact: true,
});
bundlePreactComponents.displayName = '📦 bundling preact components for cdn distribution';
const bundleWebComponents = bundleJs({
entryPath: path.resolve(distPreactComponents, 'esm', 'web-components', 'index.js'),
dest: path.join(distWebComponents, 'bundle'),
preact: true,
webComponents: true,
});
bundleWebComponents.displayName = '📦 bundling web components for cdn distribution';
const compileReactComponents = gulp.series(
compileCjs(path.join(distReactComponents, 'cjs')),
compileEsm(path.join(distReactComponents, 'esm')),
compileTypescriptDefs(),
bundleReactComponents
);
const compilePreactComponents = gulp.series(
compileCjs(path.join(distPreactComponents, 'cjs'), preactBabelConfig),
compileEsm(path.join(distPreactComponents, 'esm'), preactBabelConfig),
// TODO: Re-enable when all components are functional components. TypeScript
// is throwing 121 errors when we try to replace react with preact/compat,
// and the components that are throwing the errors are all class components.
// compileTypescriptDefs(preactTsConfig),
bundlePreactComponents,
bundleWebComponents
);
/*
* displays help if run without any options
*/
const displayHelp = (cb) => {
log();
log('usage:');
log('yarn gulp build <params>');
log(' --package <cmsds system/child system path> // i.e. packages/ds-healthcare-gov');
log(' --minifySvg // will enable svg minification during image asset copying');
log();
cb();
};
/*
* build command which runs compilation process
*/
log('🪴 building the cmsds');
exports.build = gulp.series(
cleanDist,
gulp.parallel(copyThemes, copyImages, copyFonts, copyJSON),
gulp.parallel(compileSass, compileReactComponents, compilePreactComponents)
);
/*
* command line help
*/
exports.default = displayHelp;