Skip to content

Commit 0df0a2a

Browse files
committed
chore: update init script
1 parent f5f8489 commit 0df0a2a

File tree

11 files changed

+186
-70
lines changed

11 files changed

+186
-70
lines changed

packages/tdesign-uniapp/example/script/release/core.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const fs = require('fs');
22
const path = require('path');
33
const { processLess } = require('./less');
4+
const { processTs } = require('./typescript');
45

56

67
async function copy({
@@ -25,7 +26,13 @@ async function copy({
2526
lessResult = await processLess(filePath, targetPath);
2627
}
2728

29+
// 对 .ts 文件进行编译(排除 .d.ts)
30+
let tsResult = false;
2831
if (!lessResult) {
32+
tsResult = processTs(filePath, targetPath);
33+
}
34+
35+
if (!lessResult && !tsResult) {
2936
fs.copyFileSync(filePath, targetPath);
3037
}
3138

packages/tdesign-uniapp/example/script/release/prepare.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const { deleteFolder } = require('t-comm');
44

55
const { config, DIST_BLACK_LIST } = require('./config');
66
const { copy } = require('./core.js');
7+
const { generateDts } = require('./typescript');
78

89
async function main() {
910
const {
@@ -54,6 +55,9 @@ async function prepareOne({ targetDir, sourceGlob, sourceDir }) {
5455
}
5556

5657
console.log(`[Wrote] done! Length is ${list.length}!`);
58+
59+
// 批量生成 .d.ts 声明文件
60+
generateDts(sourceDir, targetDir);
5761
}
5862

5963
main();
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const { execSync } = require('child_process');
4+
const ts = require('typescript');
5+
const { PROJECT_ROOT } = require('./config');
6+
7+
const COMPILE_TS = false;
8+
const GENERATE_DTS = false;
9+
10+
11+
/** 编译选项,与根目录 tsconfig.json 中的 compilerOptions 保持一致 */
12+
const COMPILER_OPTIONS = {
13+
target: ts.ScriptTarget.ES2015,
14+
module: ts.ModuleKind.ESNext,
15+
moduleResolution: ts.ModuleResolutionKind.NodeJs,
16+
esModuleInterop: true,
17+
allowSyntheticDefaultImports: true,
18+
experimentalDecorators: true,
19+
declaration: false,
20+
skipLibCheck: true,
21+
resolveJsonModule: true,
22+
allowJs: true,
23+
removeComments: false,
24+
};
25+
26+
/**
27+
* 将单个 .ts 文件编译为 .js 文件
28+
* @param {string} inputFile 源 .ts 文件的绝对路径
29+
* @param {string} rawOutputFile 目标文件的绝对路径(仍是 .ts 后缀,函数内部会改为 .js)
30+
* @returns {boolean|undefined} 成功返回 true,非 .ts 文件返回 undefined
31+
*/
32+
function processTs(inputFile, rawOutputFile) {
33+
if (!COMPILE_TS) return;
34+
35+
// 只处理 .ts 文件(排除 .d.ts)
36+
if (!inputFile.endsWith('.ts') || inputFile.endsWith('.d.ts')) {
37+
return;
38+
}
39+
40+
// 跳过纯类型定义文件(props.ts / type.ts / common.ts),这些文件仅提供类型声明,不需要编译为 JS
41+
const basename = path.basename(inputFile);
42+
if (basename === 'props.ts' || basename === 'type.ts' || basename === 'common.ts') {
43+
return;
44+
}
45+
46+
try {
47+
const tsCode = fs.readFileSync(inputFile, 'utf8');
48+
49+
const result = ts.transpileModule(tsCode, {
50+
compilerOptions: COMPILER_OPTIONS,
51+
fileName: path.basename(inputFile),
52+
});
53+
54+
// 将 4 空格缩进转换为 2 空格缩进
55+
const outputText = result.outputText.replace(/^( {4})+/gm, match => ' '.repeat(match.length / 4));
56+
57+
// 输出文件后缀改为 .js
58+
const outputFile = rawOutputFile.replace(/\.ts$/, '.js');
59+
fs.mkdirSync(path.dirname(outputFile), { recursive: true });
60+
fs.writeFileSync(outputFile, outputText);
61+
console.log(`✅ TS 编译完成: ${path.relative(PROJECT_ROOT, outputFile)}`);
62+
return true;
63+
} catch (err) {
64+
console.error(`❌ TS 编译失败: ${inputFile}`, err);
65+
return false;
66+
}
67+
}
68+
69+
/**
70+
* 使用 tsc 命令行批量生成 .d.ts 声明文件
71+
* @param {string} sourceDir 源码根目录(如 uniapp-components)
72+
* @param {string} targetDir 输出目录(如 dist)
73+
*/
74+
function generateDts(sourceDir, targetDir) {
75+
if (!GENERATE_DTS) return;
76+
// 收集 sourceDir 下所有 .ts 文件(排除 .d.ts、node_modules、_example)
77+
const tsFiles = collectTsFiles(sourceDir);
78+
79+
if (tsFiles.length === 0) {
80+
console.log('⚠️ 未找到需要生成 .d.ts 的 .ts 文件');
81+
return;
82+
}
83+
84+
console.log(`\n📝 开始生成 .d.ts 声明文件,共 ${tsFiles.length} 个 .ts 文件...`);
85+
86+
// 创建临时 tsconfig 文件,避免命令行参数过长
87+
const tmpTsConfig = path.join(sourceDir, '.tsconfig.dts.tmp.json');
88+
const tsconfigContent = {
89+
compilerOptions: {
90+
declaration: true,
91+
emitDeclarationOnly: true,
92+
skipLibCheck: true,
93+
target: 'ES2015',
94+
lib: ['ES2015', 'ES2016', 'ES2017', 'DOM'],
95+
module: 'ESNext',
96+
moduleResolution: 'node',
97+
experimentalDecorators: true,
98+
types: ['miniprogram-api-typings', '@dcloudio/types'],
99+
baseUrl: sourceDir,
100+
paths: {
101+
'./superComponent': ['./superComponent.placeholder'],
102+
},
103+
outDir: targetDir,
104+
rootDir: sourceDir,
105+
},
106+
files: tsFiles,
107+
};
108+
109+
fs.writeFileSync(tmpTsConfig, JSON.stringify(tsconfigContent, null, 2));
110+
111+
const tscBin = path.resolve(PROJECT_ROOT, 'node_modules/.bin/tsc');
112+
113+
try {
114+
execSync(`"${tscBin}" --project "${tmpTsConfig}"`, {
115+
cwd: PROJECT_ROOT,
116+
stdio: 'pipe',
117+
});
118+
console.log(`✅ .d.ts 声明文件生成完成,输出到 ${path.relative(PROJECT_ROOT, targetDir)}`);
119+
} catch (err) {
120+
const stdout = err.stdout ? err.stdout.toString() : '';
121+
const stderr = err.stderr ? err.stderr.toString() : '';
122+
const errorOutput = stdout || stderr;
123+
// tsc 有类型错误但默认 noEmitOnError=false,.d.ts 仍然会生成
124+
// 仅打印警告,不中断流程
125+
if (errorOutput) {
126+
console.warn(`⚠️ tsc 生成 .d.ts 时存在类型警告(不影响产物生成):\n${errorOutput}`);
127+
}
128+
console.log(`✅ .d.ts 声明文件生成完成(含警告),输出到 ${path.relative(PROJECT_ROOT, targetDir)}`);
129+
} finally {
130+
// 清理临时文件
131+
try {
132+
fs.unlinkSync(tmpTsConfig);
133+
} catch (e) { /* ignore */ }
134+
}
135+
}
136+
137+
/**
138+
* 递归收集目录下的所有 .ts 文件(排除 .d.ts、node_modules、_example)
139+
*/
140+
function collectTsFiles(dir) {
141+
const results = [];
142+
143+
function walk(currentDir) {
144+
const entries = fs.readdirSync(currentDir, { withFileTypes: true });
145+
for (const entry of entries) {
146+
const fullPath = path.join(currentDir, entry.name);
147+
if (entry.isDirectory()) {
148+
if (entry.name === 'node_modules' || entry.name === '_example') continue;
149+
walk(fullPath);
150+
} else if (entry.isFile() && entry.name.endsWith('.ts') && !entry.name.endsWith('.d.ts')) {
151+
results.push(fullPath);
152+
}
153+
}
154+
}
155+
156+
walk(dir);
157+
return results;
158+
}
159+
160+
module.exports = {
161+
processTs,
162+
generateDts,
163+
};

packages/tdesign-uniapp/example/script/watch/init.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const { deleteFolder } = require('t-comm');
55

66
const { config } = require('./config');
77
const { copyComponents, checkVue2CliExist, checkVue2HxExist, checkVue3HxExist } = require('./helper');
8+
const { generateDts } = require('../release/typescript');
89

910

1011
async function copyOneProject({
@@ -43,11 +44,13 @@ function clearTargetDir() {
4344

4445
if (checkVue2HxExist()) {
4546
deleteFolder(config.componentTargetDirInVue2Hx);
47+
deleteFolder(config.componentChatTargetDirInVue2Hx);
4648
deleteFolder(config.pagesMoreDirInVue2Hx);
4749
}
4850

4951
if (checkVue3HxExist()) {
5052
deleteFolder(config.componentTargetDirInVue3Hx);
53+
deleteFolder(config.componentChatTargetDirInVue3Hx);
5154
deleteFolder(config.pagesMoreDirInVue3Hx);
5255
}
5356
}
@@ -84,11 +87,17 @@ async function main() {
8487
isChat: false,
8588
});
8689

90+
// 为主包生成 .d.ts 声明文件
91+
generateDts(config.sourceDir, config.componentTargetDirInVue3Cli);
92+
8793
await copyOneProject({
8894
globMode: config.chatSourceGlob,
8995
sourceDir: config.chatSourceDir,
9096
isChat: true,
9197
});
98+
99+
// 为 chat 包生成 .d.ts 声明文件
100+
generateDts(config.chatSourceDir, config.componentTargetDirInVue3Cli);
92101
}
93102

94103

packages/tdesign-uniapp/example/src/pages/home/home.less

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
/* #ifdef VUE3 */
21
@import '@tdesign/uniapp/common/style/_variables.less';
3-
/* #endif */
42

5-
/* #ifdef VUE2 */
6-
@import '~@tdesign/uniapp/common/style/_variables.less';
7-
/* #endif */
83

94
.main {
105
width: 100%;

packages/tdesign-uniapp/example/src/style/app.less

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
/* #ifdef VUE3 */
21
@import '@tdesign/uniapp/common/style/_variables.less';
32
@import '@tdesign/uniapp/common/style/theme/index.less';
4-
/* #endif */
53

6-
/* #ifdef VUE2 */
7-
@import '~@tdesign/uniapp/common/style/_variables.less';
8-
@import '~@tdesign/uniapp/common/style/theme/index.less';
9-
/* #endif */
104

115
@font-face {
126
font-family: 'TCloudNumber';
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
export * from './superComponent';
21
export * from './flatTool';
32
export * from './instantiationDecorator';
43
export * from './control';

packages/uniapp-components/common/src/superComponent.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

packages/uniapp-components/input/input.vue

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ import { getCharacterLength, calcIcon, coalesce, nextTick } from '../common/util
151151
import { isDef } from '../common/validator';
152152
import { getInputClass } from './computed.js';
153153
import tools from '../common/utils.wxs';
154-
import { RELATION_MAP } from '../common/relation/parent-map.js';
154+
import { RELATION_MAP } from '../common/relation/parent-map';
155155
156156
157157
const name = `${prefix}-input`;
@@ -205,9 +205,6 @@ export default {
205205
tools,
206206
207207
dataValue: coalesce(this.value, this.defaultValue),
208-
209-
// rawValue: '',
210-
// innerMaxLen: -1,
211208
};
212209
},
213210
watch: {
@@ -282,30 +279,7 @@ export default {
282279
this.dataValue = value;
283280
this.count = isDef(value) ? String(value).length : 0;
284281
}
285-
286-
// this.updateInnerMaxLen();
287282
},
288-
// updateInnerMaxLen() {
289-
// this.innerMaxLen = this.getInnerMaxLen();
290-
// },
291-
// getInnerMaxLen() {
292-
// const {
293-
// allowInputOverMax,
294-
// maxcharacter,
295-
// maxlength,
296-
// dataValue,
297-
// rawValue,
298-
// count,
299-
// } = this;
300-
// return getInnerMaxLen({
301-
// allowInputOverMax,
302-
// maxcharacter,
303-
// maxlength,
304-
// dataValue,
305-
// rawValue,
306-
// count,
307-
// });
308-
// },
309283
310284
updateClearIconVisible(value = false) {
311285
const { clearTrigger, disabled, readonly } = this;

packages/uniapp-components/mixins/skyline.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getCurrentPage } from '../common/utils.js';
1+
import { getCurrentPage } from '../common/utils';
22

33
export default {
44
data() {

0 commit comments

Comments
 (0)