forked from kingdee/kdesign
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
139 lines (128 loc) · 4.17 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
const path = require('path')
const gulp = require('gulp')
const through2 = require('through2')
const merge2 = require('merge2')
const stripCode = require('gulp-strip-code')
const babel = require('gulp-babel')
const ts = require('gulp-typescript')
const tsDefaultReporter = ts.reporter.defaultReporter()
const cloneDeep = require('lodash/cloneDeep')
const { finalizeCompile } = require('./scripts/generate-theme')
const babelBaseConfig = require('./babel.config')
const tsConfig = require('./tsconfig.json')
const transformLess = require('./scripts/transformLess')
const rimraf = require('rimraf')
const libDir = path.join(__dirname, './lib')
const esDir = path.join(__dirname, './es')
function cssInjection(content) {
return content
.replace(/\/style\/?'/g, "/style/css'")
.replace(/\/style\/?"/g, '/style/css"')
.replace(/\.less/g, '.css')
}
function babelify(js, modules) {
const babelConfig = cloneDeep(babelBaseConfig)
babelConfig.presets[0] = ['@babel/preset-env', {}] // bisheng官网使用@babel/plugin, 组件库使用@babel/runtime
babelConfig.plugins[0] = ['@babel/plugin-transform-runtime', { corejs: 3 }]
if (modules === false) {
babelConfig.presets[0] = ['@babel/preset-env', { modules: false }]
}
let stream = js.pipe(babel(babelConfig)).pipe(
through2.obj(function z(file, encoding, next) {
this.push(file.clone())
if (file.path.match(/(\/|\\)style(\/|\\)index\.js/)) {
const content = file.contents.toString(encoding)
if (content.indexOf("'react-native'") !== -1) {
next()
return
}
file.contents = Buffer.from(cssInjection(content))
file.path = file.path.replace(/index\.js/, 'css.js')
this.push(file)
next()
} else {
next()
}
}),
)
if (modules === false) {
stream = stream.pipe(
stripCode({
start_comment: '@remove-on-es-build-begin',
end_comment: '@remove-on-es-build-end',
}),
)
}
return stream.pipe(gulp.dest(modules === false ? esDir : libDir))
}
function compile(modules) {
rimraf.sync(modules !== false ? libDir : esDir)
const assets = gulp.src(['components/**/*.@(png|svg|woff|css)']).pipe(gulp.dest(modules === false ? esDir : libDir))
const less = gulp
.src(['components/**/*.less'])
.pipe(
through2.obj(function (file, encoding, next) {
this.push(file.clone())
if (file.path.match(/(\/|\\)style(\/|\\)index\.less$/)) {
transformLess(file.path)
.then((css) => {
file.contents = Buffer.from(css)
file.path = file.path.replace(/\.less$/, '.css')
this.push(file)
next()
})
.catch((e) => {
console.error(e)
})
} else {
next()
}
}),
)
.pipe(gulp.dest(modules === false ? esDir : libDir))
let error = 0
const source = [
'components/**/*.tsx',
'components/**/*.ts',
'components/**/*.js',
'typings/**/*.d.ts',
'!components/**/__tests__/**',
]
// allow jsx file in components/xxx/
if (tsConfig.compilerOptions && tsConfig.compilerOptions.allowJs) {
source.unshift('components/**/*.jsx')
}
const tsResult = gulp.src(source).pipe(
ts(tsConfig.compilerOptions, {
error(e) {
tsDefaultReporter.error(e)
error = 1
},
finish: tsDefaultReporter.finish,
}),
)
function check() {
// eslint-disable-next-line no-undef
if (error && !argv['ignore-error']) {
process.exit(1)
}
}
tsResult.on('finish', check)
tsResult.on('end', check)
const tsFilesStream = babelify(tsResult.js, modules)
const tsd = tsResult.dts.pipe(gulp.dest(modules === false ? esDir : libDir))
return merge2([less, tsFilesStream, tsd, assets])
}
gulp.task('compile-with-es', (done) => {
console.log('[Parallel] Compile to es...')
compile(false).on('finish', done)
})
gulp.task('compile-with-lib', (done) => {
console.log('[Parallel] Compile to js...')
compile().on('finish', done)
})
gulp.task('compile-finalize', (done) => {
finalizeCompile()
done()
})
gulp.task('compile', gulp.series(gulp.parallel('compile-with-es', 'compile-with-lib'), 'compile-finalize'))