forked from sanity-io/sanity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
121 lines (106 loc) · 3.57 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
/* eslint-disable @typescript-eslint/no-var-requires, import/no-unassigned-import, max-nested-callbacks */
const path = require('path')
const {src, dest, watch, parallel, series} = require('gulp')
const del = require('del')
const changed = require('gulp-changed')
const filter = require('gulp-filter')
const chalk = require('chalk')
const babel = require('gulp-babel')
const {runTsc} = require('./scripts/runTsc')
const log = require('fancy-log')
const through = require('through2')
const {getPackagePaths} = require('./scripts/utils/getPackagePaths')
const SRC_DIR = 'src'
const DEST_DIR = 'lib'
// Regexes/names of packages that doesn't follow the src/lib convention
// or packages that does their own build (e.g. studios)
const IGNORED_PACKAGES = [
'examples/storybook',
/examples\/.*-studio/,
'packages/@sanity/date-input',
'packages/@sanity/eventsource',
'packages/@sanity/generate-help-url',
'packages/@sanity/plugin-loader',
'packages/create-sanity',
'packages/sanity',
]
const PACKAGE_PATHS = getPackagePaths().filter((pkgPath) =>
IGNORED_PACKAGES.every((pattern) =>
typeof pattern === 'string' ? pattern !== pkgPath : !pattern.test(pkgPath)
)
)
const withDisplayName = (name, fn) => {
fn.displayName = name
return fn
}
const TASK_INFO = {
babel: {title: 'Babel', color: chalk.yellowBright},
assets: {title: 'Assets (copy)', color: chalk.greenBright},
watch: {title: 'Watch', color: chalk.cyanBright},
_unknown: {title: 'Unknown', color: chalk.white},
}
const compileTaskName = (taskType, packagePath, extra = '') => {
const info = TASK_INFO[taskType] || TASK_INFO._unknown
return `${info.color(info.title)} → ${path.relative('packages', packagePath)}${
extra ? ` (${chalk.grey(extra)})` : ''
}`
}
function buildJavaScript(packageDir) {
return withDisplayName(compileTaskName('babel', packageDir), () =>
src(`${SRC_DIR}/**/*.{js,ts,tsx}`, {cwd: packageDir})
.pipe(
changed(DEST_DIR, {
cwd: packageDir,
transformPath: (orgPath) => orgPath.replace(/\.tsx?$/, '.js'),
})
)
.pipe(babel())
.pipe(dest(DEST_DIR, {cwd: packageDir}))
)
}
function copyAssets(packageDir) {
return withDisplayName(compileTaskName('assets', packageDir), () =>
src(`${SRC_DIR}/**/*`, {cwd: packageDir})
.pipe(filter(['**/*.*', '!**/*.js', '!**/*.ts', '!**/*.tsx']))
.pipe(changed(DEST_DIR, {cwd: packageDir}))
.pipe(dest(DEST_DIR, {cwd: packageDir}))
)
}
function buildPackage(packageDir) {
return parallel(buildJavaScript(packageDir), copyAssets(packageDir))
}
function watchPackage(name, packageDir, task) {
return withDisplayName(name, () => watch([`${SRC_DIR}/**/*`], {cwd: packageDir}, task))
}
const watchTS = function watchTS() {
return runTsc(path.join(__dirname), true).pipe(
through((data, enc, cb) => {
log(data.toString())
cb()
})
)
}
const buildTS = function buildTS() {
return runTsc(path.join(__dirname)).pipe(
through((data, enc, cb) => {
log(data.toString())
cb()
})
)
}
const buildJSAndAssets = parallel(PACKAGE_PATHS.map(buildPackage))
const watchJSAndAssets = parallel(
PACKAGE_PATHS.map((packageDir) =>
watchPackage(
compileTaskName('watch', packageDir, 'JS/Assets'),
packageDir,
buildPackage(packageDir)
)
)
)
exports.js = buildJSAndAssets
exports.ts = buildTS
exports.watchTS = series(buildTS, watchTS)
exports.build = series(buildJSAndAssets, buildTS)
exports.watch = series(buildJSAndAssets, parallel(watchJSAndAssets, watchTS))
exports.clean = () => del(PACKAGE_PATHS.map((pth) => path.join(pth, DEST_DIR)))