forked from sanity-io/sanity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
145 lines (128 loc) · 4.22 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
/* 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 log = require('fancy-log')
const through = require('through2')
const {runTsc} = require('./scripts/runTsc')
const {getPackagePaths} = require('./scripts/utils/getPackagePaths')
const SRC_DIR = 'src'
const LEGACY_DEST_DIR = 'lib'
const DEST_DIR = 'dist'
// 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 = [
'dev/workshop',
'dev/depcheck-test',
/dev\/.*-studio/,
/examples\/.*-studio/,
'packages/@sanity/date-input',
'packages/@sanity/eventsource',
'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, destDir) {
return withDisplayName(compileTaskName('babel', packageDir, 'cjs'), () =>
src([`${SRC_DIR}/**/*.{js,jsx,ts,tsx}`, '!**/*.{test,spec}.{js,jsx,ts,tsx}'], {cwd: packageDir})
.pipe(
changed(destDir, {
cwd: packageDir,
transformPath: (orgPath) => orgPath.replace(/\.(t|j)sx?$/, '.js'),
})
)
.pipe(babel())
.pipe(dest(destDir, {cwd: packageDir}))
)
}
function copyAssets(packageDir, destDir) {
return withDisplayName(compileTaskName('assets', packageDir), () =>
src(`${SRC_DIR}/**/*`, {cwd: packageDir})
.pipe(filter(['**/*.*', '!**/*.js', '!**/*.jsx', '!**/*.ts', '!**/*.tsx']))
.pipe(changed(destDir, {cwd: packageDir}))
.pipe(dest(destDir, {cwd: packageDir}))
)
}
function buildPackage(packageDir) {
return parallel(
buildJavaScript(packageDir, LEGACY_DEST_DIR),
copyAssets(packageDir, LEGACY_DEST_DIR)
)
}
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)
)
)
)
function matchPackages(names) {
const matchPkgs = names.map((n) => `packages/${n}`)
return (pkg) => matchPkgs.includes(pkg)
}
const CLI_PKGS = [
'@sanity/cli',
'@sanity/core',
'@sanity/export',
'@sanity/import',
'@sanity/mutator',
'@sanity/resolver',
'@sanity/server',
'@sanity/util',
'@sanity/webpack-loader',
]
exports.js = buildJSAndAssets
exports['build:cli'] = parallel(PACKAGE_PATHS.filter(matchPackages(CLI_PKGS)).map(buildPackage))
exports.watchTS = series(buildTS, watchTS)
exports.watchJS = series(buildJSAndAssets, watchJSAndAssets)
exports.build = series(buildJSAndAssets, buildTS)
exports.watch = series(buildJSAndAssets, parallel(watchJSAndAssets, watchTS))
exports.clean = () =>
del(PACKAGE_PATHS.flatMap((pth) => [path.join(pth, LEGACY_DEST_DIR), path.join(pth, DEST_DIR)]))