Skip to content

Commit e579cf8

Browse files
committed
chore: optimizing package size
1 parent 64a089e commit e579cf8

File tree

104 files changed

+278
-221
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

104 files changed

+278
-221
lines changed

.eslintignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
src/common/lib
33
miniprogram_dist
44
node_modules
5-
common
5+
common
6+
_example

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
**/*.md
22
src/_common
3+
_example

example/components/demo-block/index.less

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@import '../../../src/common/index.less';
1+
@import '../../../src/common/style/index.less';
22

33
.demo-block {
44
margin: @spacer-4 0 0;

package.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"commitizen": "^4.2.4",
6767
"cross-env": "^7.0.2",
6868
"cz-conventional-changelog": "^3.3.0",
69+
"dayjs": "^1.10.7",
6970
"del": "^6.1.1",
7071
"eslint": "^7.0.0",
7172
"eslint-config-airbnb-base": "^14.2.1",
@@ -76,14 +77,19 @@
7677
"gray-matter": "^4.0.3",
7778
"gulp": "^4.0.2",
7879
"gulp-changed": "^4.0.2",
80+
"gulp-clean-css": "^4.3.0",
81+
"gulp-htmlmin": "^5.0.1",
7982
"gulp-if": "^3.0.0",
83+
"gulp-insert": "^0.5.0",
84+
"gulp-jsonminify": "^1.1.0",
8085
"gulp-less": "^5.0.0",
8186
"gulp-mp-npm": "^1.9.7",
8287
"gulp-plumber": "^1.2.1",
8388
"gulp-rename": "^2.0.0",
8489
"gulp-replace": "^1.0.0",
8590
"gulp-replace-task": "^2.0.1",
8691
"gulp-sourcemaps": "^3.0.0",
92+
"gulp-terser": "^2.1.0",
8793
"gulp-typescript": "^6.0.0-alpha.1",
8894
"husky": "^7.0.4",
8995
"jest": "^26.6.3",
@@ -92,6 +98,7 @@
9298
"less": "^4.1.1",
9399
"lint-staged": "^10.0.0-1",
94100
"lodash": "^4.17.21",
101+
"merge2": "^1.4.1",
95102
"miniprogram-api-typings": "^3.4.6",
96103
"miniprogram-automator": "^0.10.0",
97104
"miniprogram-simulate": "^1.6.0",
@@ -104,6 +111,7 @@
104111
"tdesign-icons-view": "^0.3.2",
105112
"tdesign-publish-cli": "^0.0.12",
106113
"tdesign-site-components": "^0.15.2",
114+
"tinycolor2": "^1.4.2",
107115
"typescript": "~4.7.2",
108116
"vite": "^2.7.6",
109117
"vite-plugin-tdoc": "^2.0.1",
@@ -122,9 +130,5 @@
122130
"{src,example}/**/*.{js,ts}": [
123131
"eslint --fix"
124132
]
125-
},
126-
"dependencies": {
127-
"dayjs": "^1.10.7",
128-
"tinycolor2": "^1.4.2"
129133
}
130134
}

script/gulpfile.base.js

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
1+
const path = require('path');
12
const gulp = require('gulp');
23
const del = require('del');
34
const replace = require('gulp-replace');
45
const plumber = require('gulp-plumber');
56
const changed = require('gulp-changed');
67
const gulpTs = require('gulp-typescript');
8+
const merge2 = require('merge2');
79
const sourcemaps = require('gulp-sourcemaps');
810
const gulpLess = require('gulp-less');
11+
const cssmin = require('gulp-clean-css');
12+
const gulpInsert = require('gulp-insert');
13+
const jsmin = require('gulp-terser');
14+
const jsonmin = require('gulp-jsonminify');
15+
const wxmlmin = require('gulp-htmlmin');
916
const rename = require('gulp-rename');
1017
const replaceTask = require('gulp-replace-task');
1118
const mpNpm = require('gulp-mp-npm');
1219
const gulpIf = require('gulp-if');
1320

1421
const config = require('./config');
1522

23+
const baseCssPath = path.resolve(__dirname, '../src/common/style/index.wxss');
24+
1625
const isProduction = process.env.NODE_ENV === 'production';
1726

1827
// set displayName
@@ -35,20 +44,28 @@ const generateConfigReplaceTask = (replaceConfig, options = {}) => {
3544
});
3645
};
3746

47+
const isComponentFolder = (dir) => {
48+
return dir === 'src';
49+
};
50+
3851
/* return gulpfile base tasks */
3952
module.exports = (src, dist, moduleName) => {
4053
const tsProject = gulpTs.createProject('tsconfig.json', {
4154
declaration: true,
4255
removeComments: isProduction,
56+
importHelpers: true,
57+
noEmitHelpers: true,
4358
});
4459

4560
// options
4661
const ignore = ['**/__test__', '**/__test__/**', '**/_example/**', '**/_common/**'];
4762
const srcOptions = { base: src, ignore };
4863
const watchOptions = { events: ['add', 'change'] };
4964
const gulpErrorPath = 'example/utils/gulpError.js';
65+
5066
// 文件匹配路径
5167
const globs = {
68+
wxml: `${src}/**/*.wxml`, // 匹配 wxml 文件
5269
ts: `${src}/**/*.ts`, // 匹配 ts 文件
5370
js: `${src}/**/*.js`, // 匹配 js 文件
5471
wxs: `${src}/**/*.wxs`, // 匹配 wxs 文件
@@ -57,6 +74,7 @@ module.exports = (src, dist, moduleName) => {
5774
wxss: `${src}/**/*.wxss`, // 匹配 wxss 文件
5875
md: `${src}/**/*.md`, // 匹配 md 文件
5976
};
77+
6078
// 匹配需要拷贝的文件
6179
globs.copy = [
6280
`${src}/**`,
@@ -66,6 +84,7 @@ module.exports = (src, dist, moduleName) => {
6684
`!${globs.json}`,
6785
`!${globs.less}`,
6886
`!${globs.wxss}`,
87+
`!${globs.md}`,
6988
'!**/_example/**',
7089
];
7190

@@ -104,11 +123,30 @@ module.exports = (src, dist, moduleName) => {
104123
.pipe(changed(dist)) // 过滤掉未改变的文件
105124
.pipe(gulp.dest(dist));
106125

126+
/** `gulp wxml`
127+
* 处理 wxml
128+
* */
129+
tasks.wxml = () =>
130+
gulp
131+
.src(globs.wxml, { ...srcOptions, since: since(tasks.wxml) })
132+
.pipe(
133+
gulpIf(
134+
isComponentFolder(src) && isProduction,
135+
wxmlmin({
136+
removeComments: true,
137+
collapseWhitespace: true,
138+
keepClosingSlash: true,
139+
caseSensitive: true,
140+
}),
141+
),
142+
)
143+
.pipe(gulp.dest(dist));
144+
107145
/** `gulp ts`
108146
* 处理ts
109147
* */
110-
tasks.ts = () =>
111-
gulp
148+
tasks.ts = () => {
149+
const tsResult = gulp
112150
.src(globs.ts, srcOptions)
113151
.pipe(
114152
plumber({
@@ -119,10 +157,17 @@ module.exports = (src, dist, moduleName) => {
119157
)
120158
.pipe(generateConfigReplaceTask(config, { stringify: true }))
121159
.pipe(gulpIf(!isProduction, sourcemaps.init()))
122-
.pipe(tsProject()) // 编译ts
123-
.pipe(mpNpm())
124-
.pipe(gulpIf(!isProduction, sourcemaps.write('.')))
125-
.pipe(gulp.dest(dist));
160+
.pipe(tsProject()); // 编译ts
161+
162+
return merge2(
163+
tsResult.js
164+
.pipe(mpNpm())
165+
.pipe(gulpIf(isComponentFolder(src) && isProduction, jsmin()))
166+
.pipe(sourcemaps.write('.'))
167+
.pipe(gulp.dest(dist)),
168+
tsResult.dts.pipe(gulp.dest(dist)), // 直接输出.d.ts文件
169+
);
170+
};
126171

127172
/** `gulp js`
128173
* 处理js
@@ -131,6 +176,7 @@ module.exports = (src, dist, moduleName) => {
131176
gulp
132177
.src(globs.js, { ...srcOptions, since: since(tasks.js) })
133178
.pipe(generateConfigReplaceTask(config, { stringify: true }))
179+
.pipe(gulpIf(isComponentFolder(src) && isProduction, jsmin()))
134180
.pipe(gulp.dest(dist));
135181

136182
/** `gulp wxs`
@@ -145,7 +191,11 @@ module.exports = (src, dist, moduleName) => {
145191
/** `gulp json`
146192
* 处理json
147193
* */
148-
tasks.json = () => gulp.src(globs.json, { ...srcOptions, dot: true, since: since(tasks.json) }).pipe(gulp.dest(dist));
194+
tasks.json = () =>
195+
gulp
196+
.src(globs.json, { ...srcOptions, dot: true, since: since(tasks.json) })
197+
.pipe(gulpIf(isComponentFolder(src) && isProduction, jsonmin()))
198+
.pipe(gulp.dest(dist));
149199

150200
/** `gulp less`
151201
* 处理less
@@ -164,6 +214,21 @@ module.exports = (src, dist, moduleName) => {
164214
.pipe(generateConfigReplaceTask(config, { stringify: false }))
165215
.pipe(gulpIf(!isProduction, sourcemaps.init()))
166216
.pipe(gulpLess()) // 编译less
217+
.pipe(gulpIf(isComponentFolder(src) && isProduction, cssmin()))
218+
.pipe(
219+
gulpIf(
220+
isComponentFolder(src),
221+
gulpInsert.transform((contents, file) => {
222+
if (!file.path.includes(`src${path.sep}common`)) {
223+
const relativePath = path
224+
.relative(path.normalize(`${file.path}${path.sep}..`), baseCssPath)
225+
.replace(/\\/g, '/');
226+
contents = `@import '${relativePath}';${contents}`;
227+
}
228+
return contents;
229+
}),
230+
),
231+
)
167232
.pipe(rename({ extname: '.wxss' }))
168233
.pipe(gulpIf(!isProduction, sourcemaps.write('.')))
169234
.pipe(gulp.dest(dist));
@@ -187,14 +252,15 @@ module.exports = (src, dist, moduleName) => {
187252
tasks.build = gulp.series(
188253
tasks.clear,
189254
tasks.resetError,
190-
gulp.parallel(tasks.copy, tasks.ts, tasks.js, tasks.wxs, tasks.json, tasks.less, tasks.wxss),
255+
gulp.parallel(tasks.copy, tasks.wxml, tasks.ts, tasks.js, tasks.wxs, tasks.json, tasks.less, tasks.wxss),
191256
);
192257

193258
/** `gulp watch`
194259
* 监听
195260
* */
196261
tasks.watch = () => {
197262
gulp.watch(globs.copy, watchOptions, tasks.copy);
263+
gulp.watch(globs.wxml, watchOptions, tasks.wxml);
198264
gulp.watch(globs.ts, watchOptions, gulp.series(tasks.resetError, tasks.ts));
199265
gulp.watch(globs.js, watchOptions, tasks.js);
200266
gulp.watch(globs.wxs, watchOptions, tasks.wxs);

src/action-sheet/action-sheet.less

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@import '../common/style/index.less';
1+
@import '../common/style/base.less';
22

33
@action-sheet: ~'@{prefix}-action-sheet';
44

src/avatar-group/avatar-group.less

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@import '../common/style/index.less';
1+
@import '../common/style/base.less';
22

33
//组合头像偏移量
44
@avatar-group-offset-small: var(--td-avatar-group-margin-left-small, -16rpx);

src/avatar/avatar.less

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@import '../common/style/index.less';
1+
@import '../common/style/base.less';
22

33
@avatar-bg-color: var(--td-avatar-bg-color, @brand-color-light-active);
44
@avatar-content-color: var(--td-avatar-content-color, @brand-color);

src/back-top/back-top.less

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@import '../common/style/index.less';
1+
@import '../common/style/base.less';
22

33
@backtop: ~'@{prefix}-back-top';
44
@back-top-text-font-size: @font-size;

src/badge/badge.less

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@import '../common/style/index.less';
1+
@import '../common/style/base.less';
22

33
@badge-text-color: var(--td-badge-text-color, @text-color-anti);
44
@badge-content-text-color: var(--td-badge-content-text-color, @text-color-primary);

src/button/button.less

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@import '../common/style/index.less';
1+
@import '../common/style/base.less';
22

33
@button-border-width: var(--td-button-border-width, 4rpx);
44
@button-border-radius: var(--td-button-border-radius, @radius-default);

src/calendar/calendar.less

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@import '../common/style/index.less';
1+
@import '../common/style/base.less';
22

33
@calendar-radius: var(--td-calendar-radius, 24rpx);
44
@calendar-bg-color: var(--td-calendar-bg-color, @bg-color-container);

src/cascader/cascader.less

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@import '../common/style/index.less';
1+
@import '../common/style/base.less';
22

33
@cascader: ~'@{prefix}-cascader';
44

src/cell-group/cell-group.less

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// @import (css) '../common/index.wxss';
2-
@import '../common/style/index.less';
2+
@import '../common/style/base.less';
33

44
@cell-group-title-font-size: var(--td-cell-group-title-font-size, 28rpx);
55
@cell-group-title-line-height: var(--td-cell-group-title-line-height, 90rpx);

src/cell/cell.less

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@import '../common/style/index.less';
1+
@import '../common/style/base.less';
22

33
@cell-height: var(--td-cell-height, auto);
44
@cell-line-height: var(--td-cell-line-height, 48rpx);

src/check-tag/check-tag.less

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@import '../common/index.less';
1+
@import '../common/style/base.less';
22

33
// themes
44
@tag-default-color: var(--td-tag-default-color, @bg-color-component);

src/checkbox/checkbox.less

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@import '../common/style/index.less';
1+
@import '../common/style/base.less';
22

33
@checkbox: ~'@{prefix}-checkbox';
44

src/col/col.less

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@import '../common/index.less';
1+
@import '../common/style/base.less';
22

33
.@{prefix}-col {
44
display: block;

src/collapse-panel/collapse-panel.less

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@import '../common/index.less';
1+
@import '../common/style/base.less';
22

33
@collapse-header-text-color: var(--td-collapse-header-text-color, @text-color-primary);
44
@collapse-content-text-color: var(--td-collapse-content-text-color, @text-color-primary);

src/collapse/collapse.less

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@import '../common/index.less';
1+
@import '../common/style/base.less';
22

33
.@{prefix}-collapse {
44
&--card {

src/color-picker/color-picker.less

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@import '../common/style/index.less';
1+
@import '../common/style/base.less';
22

33
@color-picker-panel-width: var(--td-color-picker-panel-width, 750rpx);
44
@color-picker-panel-padding: var(--td-color-picker-panel-padding, 32rpx);

src/common/index.less

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/common/shared/color-picker/gradient.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import isString from 'lodash/isString';
2-
import isNull from 'lodash/isNull';
1+
import { isString, isNull } from '../../validator';
32
/* eslint-disable no-param-reassign */
43
/**
54
* 用于反解析渐变字符串为对象

0 commit comments

Comments
 (0)