-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
172 lines (159 loc) · 3.86 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
const gulp = require('gulp');
const less = require('gulp-less');
const path = require('path');
// 删除文件
const del = require('del');
// 转js
const babel = require('gulp-babel');
// ts转js
const ts = require('gulp-typescript');
// css
const postcss = require('gulp-postcss');
const pxMultiplePlugin = require('postcss-px-multiple')({ times: 2 });
const replace = require('gulp-replace');
// 操作文件内容
const through = require('through2');
const tsconfig = require('./tsconfig.json');
// 先清除 lib目录
function clean() {
return del('./lib/**');
}
// 打包js 从src目录的ts -> lib目录下的es
function buildES() {
const tsProject = ts({
...tsconfig.compilerOptions,
module: 'ES6',
});
return gulp
.src(['./src/**/*.{ts,tsx}'], {
ignore: ['**/demos/**/*', '**/tests/**/*'],
})
.pipe(tsProject)
.pipe(
babel({
'plugins': [babelTransformLessToCss()],
})
)
.pipe(gulp.dest('lib/es/'));
}
// 直接将es 转 cjs
function buildCJS() {
return gulp
.src(['lib/es/**/*.js'])
.pipe(
babel({
'plugins': ['@babel/plugin-transform-modules-commonjs'],
})
)
.pipe(gulp.dest('lib/cjs/'));
}
// less -> css
function babelTransformLessToCss() {
return {
visitor: {
ImportDeclaration(path, source) {
if (path.node.source.value.endsWith('.less')) {
path.node.source.value = path.node.source.value.replace(/\.less$/, '.css');
}
},
},
};
}
// 打包 ts tsx
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/'));
}
// 将所有的 less -> css
function buildStyle() {
return gulp
.src(['./src/**/*.less'], {
base: './src/',
ignore: ['**/demos/**/*', '**/tests/**/*', '*.patch.less'],
})
.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'));
}
// 拷贝介绍 README
function copyMetaFiles() {
return gulp.src(['./README.md', './LICENSE']).pipe(gulp.dest('./lib/'));
}
// package.json文件进行整理并迁移
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/'));
}
// 拷贝2倍
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(replace('--aunt-hd: 1;', '--aunt-hd: 2;'))
.pipe(
gulp.dest('./lib/2x', {
overwrite: true,
})
);
}
exports.default = gulp.series(
clean,
buildES,
buildCJS,
gulp.parallel(buildDeclaration, buildStyle),
copyAssets,
copyMetaFiles,
generatePackageJSON,
gulp.series(create2xFolder, build2xCSS)
);