forked from ant-design/ant-design-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
214 lines (200 loc) · 4.95 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
const gulp = require('gulp')
const less = require('gulp-less')
const path = require('path')
const postcss = require('gulp-postcss')
const babel = require('gulp-babel')
const ts = require('gulp-typescript')
const del = require('del')
const webpackStream = require('webpack-stream')
const webpack = require('webpack')
const through = require('through2')
const BundleAnalyzerPlugin =
require('webpack-bundle-analyzer').BundleAnalyzerPlugin
const tsconfig = require('./tsconfig.json')
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/**/*'],
})
.pipe(
less({
paths: [path.join(__dirname, 'src')],
relativeUrls: true,
})
)
.pipe(gulp.dest('./lib/es'))
.pipe(gulp.dest('./lib/cjs'))
}
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: 'ESNext',
})
return gulp
.src(['src/**/*.{ts,tsx}'], {
ignore: ['**/demos/**/*', '**/tests/**/*'],
})
.pipe(tsProject)
.pipe(babel())
.pipe(gulp.dest('lib/es/'))
}
function buildDeclaration() {
const tsProject = ts({
...tsconfig.compilerOptions,
module: 'ESNext',
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 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,
},
resolve: {
extensions: ['.js', '.json'],
},
plugins: [
new BundleAnalyzerPlugin({
analyzerMode: 'json',
}),
],
module: {
rules: [
{
test: /\.js$/,
use: {
loader: 'babel-loader',
options: {
'presets': [
[
'@babel/preset-env',
{
'loose': true,
'modules': false,
'targets': {
'chrome': '49',
'ios': '10',
},
},
],
'@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: 'React',
},
],
},
webpack
)
)
.pipe(gulp.dest('lib/umd/'))
}
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
const stringified = JSON.stringify(parsed, null, 2)
file.contents = Buffer.from(stringified)
cb(null, file)
})
)
.pipe(gulp.dest('./lib/'))
}
function create2xFolder() {
return gulp
.src('./lib/**', {
base: './lib/',
ignore: ['./lib/2x/demos/**/*'],
})
.pipe(gulp.dest('./lib/2x/'))
}
function build2xCSS() {
return gulp
.src('./lib/2x/**/*.css', {
base: './lib/2x/',
})
.pipe(postcss([pxMultiplePlugin]))
.pipe(
gulp.dest('./lib/2x', {
overwrite: true,
})
)
}
exports.umdWebpack = umdWebpack
exports.default = gulp.series(
clean,
buildES,
gulp.parallel(buildCJS, buildDeclaration, buildStyle),
copyAssets,
copyMetaFiles,
generatePackageJSON,
gulp.series(create2xFolder, build2xCSS),
gulp.parallel(umdWebpack)
)