forked from ant-design/ant-design-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
343 lines (321 loc) · 8.15 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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
const gulp = require('gulp')
const less = require('gulp-less')
const path = require('path')
const postcss = require('gulp-postcss')
const babel = require('gulp-babel')
const replace = require('gulp-replace')
const ts = require('gulp-typescript')
const del = require('del')
const webpackStream = require('webpack-stream')
const webpack = require('webpack')
const through = require('through2')
const vite = require('vite')
const rename = require('gulp-rename')
const autoprefixer = require('autoprefixer')
const BundleAnalyzerPlugin =
require('webpack-bundle-analyzer').BundleAnalyzerPlugin
const tsconfig = require('./tsconfig.json')
const packageJson = require('./package.json')
const StatoscopeWebpackPlugin = require('@statoscope/webpack-plugin').default
const pxMultiplePlugin = require('postcss-px-multiple')({ times: 2 })
function clean() {
return del('./lib/**')
}
function buildStyle() {
return gulp
.src(['./src/**/*.less'], {
base: './src/',
ignore: ['**/demos/**/*', '**/tests/**/*', '*.patch.less'],
})
.pipe(
less({
paths: [path.join(__dirname, 'src')],
relativeUrls: true,
})
)
.pipe(
postcss([
autoprefixer({
overrideBrowserslist: 'iOS >= 10, Chrome >= 49',
}),
])
)
.pipe(gulp.dest('./lib/es'))
.pipe(gulp.dest('./lib/cjs'))
}
function copyPatchStyle(prefix = '') {
return () =>
gulp
.src([`./lib${prefix}/es/global/css-vars-patch.css`])
.pipe(
rename({
dirname: '',
extname: '.css',
})
)
.pipe(gulp.dest(`./lib${prefix}/bundle`))
}
function copyAssets() {
return gulp
.src('./src/assets/**/*')
.pipe(gulp.dest('lib/assets'))
.pipe(gulp.dest('lib/es/assets'))
.pipe(gulp.dest('lib/cjs/assets'))
}
function buildCJS() {
return gulp
.src(['lib/es/**/*.js'])
.pipe(
babel({
'plugins': ['@babel/plugin-transform-modules-commonjs'],
})
)
.pipe(gulp.dest('lib/cjs/'))
}
function buildES() {
const tsProject = ts({
...tsconfig.compilerOptions,
module: 'ES6',
})
return gulp
.src(['src/**/*.{ts,tsx}'], {
ignore: ['**/demos/**/*', '**/tests/**/*'],
})
.pipe(tsProject)
.pipe(
babel({
'plugins': ['./babel-transform-less-to-css'],
})
)
.pipe(gulp.dest('lib/es/'))
}
function buildDeclaration() {
const tsProject = ts({
...tsconfig.compilerOptions,
module: 'ES6',
declaration: true,
emitDeclarationOnly: true,
})
return gulp
.src(['src/**/*.{ts,tsx}'], {
ignore: ['**/demos/**/*', '**/tests/**/*'],
})
.pipe(tsProject)
.pipe(gulp.dest('lib/es/'))
.pipe(gulp.dest('lib/cjs/'))
}
function getViteConfigForPackage({ env, formats, external }) {
const name = packageJson.name
const isProd = env === 'production'
return {
root: process.cwd(),
mode: env,
logLevel: 'silent',
define: { 'process.env.NODE_ENV': `"${env}"` },
build: {
cssTarget: 'chrome61',
lib: {
name: 'antdMobile',
entry: './lib/es/index.js',
formats,
fileName: format => `${name}.${format}${isProd ? '' : `.${env}`}.js`,
},
rollupOptions: {
external,
output: {
dir: './lib/bundle',
// exports: 'named',
globals: {
'react': 'React',
'react-dom': 'ReactDOM',
},
},
},
minify: isProd ? 'esbuild' : false,
},
}
}
async function buildBundles(cb) {
const envs = ['development', 'production']
const configs = envs.map(env =>
getViteConfigForPackage({
env,
formats: ['es', 'cjs', 'umd'],
external: ['react', 'react-dom'],
})
)
await Promise.all(configs.map(config => vite.build(config)))
cb && cb()
}
function umdWebpack() {
return gulp
.src('lib/es/index.js')
.pipe(
webpackStream(
{
output: {
filename: 'antd-mobile.js',
library: {
type: 'umd',
name: 'antdMobile',
},
},
mode: 'production',
optimization: {
usedExports: true,
},
performance: {
hints: false,
},
resolve: {
extensions: ['.js', '.json'],
},
plugins: [
new BundleAnalyzerPlugin({
analyzerMode: 'static',
openAnalyzer: false,
reportFilename: 'report/report.html',
}),
new StatoscopeWebpackPlugin({
saveReportTo: 'report/statoscope/report.html',
saveStatsTo: 'report/statoscope/stats.json',
open: false,
}),
],
module: {
rules: [
{
test: /\.m?js$/,
use: {
loader: 'babel-loader',
options: {
'presets': [
[
'@babel/preset-env',
{
'loose': true,
'modules': false,
'targets': {
'chrome': '49',
'ios': '9',
},
},
],
'@babel/preset-typescript',
'@babel/preset-react',
],
},
},
},
{
test: /\.(png|svg|jpg|gif|jpeg)$/,
type: 'asset/inline',
},
{
test: /\.css$/i,
use: ['style-loader', 'css-loader'],
},
],
},
externals: [
{
react: {
commonjs: 'react',
commonjs2: 'react',
amd: 'react',
root: 'React',
},
'react-dom': {
commonjs: 'react-dom',
commonjs2: 'react-dom',
amd: 'react-dom',
root: 'ReactDOM',
},
},
],
},
webpack
)
)
.pipe(gulp.dest('lib/umd/'))
}
function copyUmd() {
return gulp
.src(['lib/umd/antd-mobile.js'])
.pipe(rename('antd-mobile.compatible.umd.js'))
.pipe(gulp.dest('lib/bundle/'))
}
function copyMetaFiles() {
return gulp.src(['./README.md', './LICENSE.txt']).pipe(gulp.dest('./lib/'))
}
function generatePackageJSON() {
return gulp
.src('./package.json')
.pipe(
through.obj((file, enc, cb) => {
const rawJSON = file.contents.toString()
const parsed = JSON.parse(rawJSON)
delete parsed.scripts
delete parsed.devDependencies
delete parsed.publishConfig
delete parsed.files
delete parsed.resolutions
delete parsed.packageManager
const stringified = JSON.stringify(parsed, null, 2)
file.contents = Buffer.from(stringified)
cb(null, file)
})
)
.pipe(gulp.dest('./lib/'))
}
function init2xFolder() {
return gulp
.src('./lib/**', {
base: './lib/',
})
.pipe(gulp.dest('./lib/2x/'))
}
function build2xCSS() {
return (
gulp
.src('./lib/2x/**/*.css', {
base: './lib/2x/',
})
// Hack fix since postcss-px-multiple ignores the `@supports` block
.pipe(
replace(
'@supports not (color: var(--adm-color-text))',
'@media screen and (min-width: 999999px)'
)
)
.pipe(postcss([pxMultiplePlugin]))
.pipe(
replace(
'@media screen and (min-width: 999999px)',
'@supports not (color: var(--adm-color-text))'
)
)
.pipe(
gulp.dest('./lib/2x', {
overwrite: true,
})
)
)
}
exports.umdWebpack = umdWebpack
exports.buildBundles = buildBundles
exports.default = gulp.series(
clean,
buildES,
buildCJS,
gulp.parallel(buildDeclaration, buildStyle),
copyAssets,
copyMetaFiles,
generatePackageJSON,
buildBundles,
gulp.series(init2xFolder, build2xCSS),
umdWebpack,
copyUmd,
copyPatchStyle(),
copyPatchStyle('/2x')
)