-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.js
175 lines (154 loc) · 3.74 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
const gulp = require('gulp')
const path = require('path')
const sass = require('sass')
const ghPages = require('gh-pages')
const through = require('through2')
const postcss = require('postcss')
const autoprefixer = require('autoprefixer')
const shell = require('gulp-shell')
const pkg = require('./package.json')
require('dotenv/config')
/**
* @param outDir {string}
* @return {string}
*/
function tsc(outDir) {
const tsconfigPath = path.resolve('./tsconfig.build.json')
const args = [
'--project',
`"${tsconfigPath}"`,
'--declaration',
'--emitDeclarationOnly',
'--declarationDir',
`"${outDir}"`,
'--noEmit false'
]
return `tsc ${args.join(' ')}`
}
/**
* @typedef {object} BabelConfig
* @property {string} sourceDir
* @property {string} outDir
* @property {string} configFile
* @property {boolean} [watch]
*/
/**
* @param config {BabelConfig}
* @return string
*/
function babel(config) {
const ignore = [
'**/test',
'**/*.d.ts',
'**/__tests__',
'**/types.{ts,tsx}',
'**/*.{test,spec}.{ts,tsx}'
]
const extensions = ['.js', '.ts', '.tsx']
const { sourceDir, outDir, configFile, watch = false } = config
const args = [
'--config-file',
path.resolve(configFile),
'--extensions',
`"${extensions.join(',')}"`,
sourceDir,
'--out-dir',
outDir,
'--ignore',
`"${ignore.join('","')}"`,
'--no-comments'
]
if (watch) {
args.push('--watch')
}
return `babel ${args.join(' ')}`
}
gulp.task('build-web-example', shell.task('yarn build-web-example'))
gulp.task(
'deploy-gh-pages',
gulp.series('build-web-example', function deploy(done) {
const GH_TOKEN = process.env.GH_TOKEN
const GH_PAGES_BRANCH = process.env.GH_PAGES_BRANCH
if (!GH_TOKEN) {
throw new Error(
'The GH_TOKEN environment variable could not be provided. Check the .env file.'
)
}
if (!GH_PAGES_BRANCH) {
throw new Error(
'The GH_PAGES_BRANCH environment variable could not be provided. Check the .env file.'
)
}
const repoPath = pkg.repository.split('/').slice(-2).join('/')
const repo = `https://${GH_TOKEN}@github.com/${repoPath}`
const basePath = path.resolve('./examples/web-example/dist')
ghPages
.publish(basePath, {
repo,
branch: GH_PAGES_BRANCH
})
.then(() => done())
.catch((error) => done(error))
})
)
gulp.task(
'build-esm',
shell.task(
babel({
sourceDir: './src',
outDir: './lib/esm',
configFile: '.babelrc.esm.json'
})
)
)
gulp.task(
'build-cjs',
shell.task(
babel({
sourceDir: './src',
outDir: './lib/cjs',
configFile: '.babelrc.cjs.json'
})
)
)
gulp.task('build-types', shell.task(tsc('./lib/types')))
gulp.task('build-styles', () => {
return gulp
.src('./src/styles/*.scss', {
ignore: ['./src/styles/_*.scss']
})
.pipe(
through.obj((chunk, enc, callback) => {
sass
.compileAsync(chunk.path, {
style: 'compressed'
})
.then((value) => value.css)
.then((css) => {
return postcss([autoprefixer({ env: 'postcss' })])
.process(css)
.then((result) => result.css)
})
.then((css) => {
chunk.path = chunk.path.replace(/\.scss$/, '.css')
chunk.contents = Buffer.from(css)
callback(null, chunk)
})
.catch((error) => callback(error))
})
)
.pipe(gulp.dest('./lib/styles'))
})
gulp.task('copy-styles', () => {
return gulp.src('./src/styles/*').pipe(gulp.dest('./lib/styles/scss'))
})
gulp.task(
'build',
gulp.series(
'build-esm',
'build-cjs',
'build-types',
'build-styles',
'copy-styles'
)
)