Skip to content

Commit 9625b57

Browse files
committedMar 29, 2023
type: fix copy *d.ts issue.
1 parent b3d6865 commit 9625b57

File tree

5 files changed

+45
-24
lines changed

5 files changed

+45
-24
lines changed
 

‎examples/react-component/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
"private": true,
55
"description": "React Component Example.",
66
"scripts": {
7-
"watch": "tsbb watch src/*.{jsx,js} --useBabel",
8-
"build": "tsbb build src/*.{jsx,js} --useBabel && npm run css:build && npm run css:build:dist",
7+
"watch": "tsbb watch src/*.{jsx,js,d.ts} --useBabel",
8+
"build": "tsbb build src/*.{jsx,js,d.ts} --useBabel && npm run css:build && npm run css:build:dist",
99
"css:build": "compile-less -d src -o esm",
1010
"css:watch": "compile-less -d src -o esm --watch",
1111
"css:build:dist": "compile-less -d src --combine dist/markdown-editor.css --rm-global",

‎packages/babel/src/index.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import fs from 'fs-extra';
22
import path from 'node:path';
3-
import { Log, __dirname } from '@tsbb/typescript';
3+
import { Log, getEmojiIcon, getExt, __dirname } from '@tsbb/typescript';
44
import { TransformOptions } from '@babel/core';
55
import babelPluginJsx from '@vue/babel-plugin-jsx';
6-
// import { TransformOptions } from '@babel/core';
76
import { transform } from './transform.js';
87
import { getOutputPath } from './utils.js';
98
import { getCjsTransformOption, getESMTransformOption } from './config.js';
@@ -126,13 +125,15 @@ function transformFile(
126125
.then((result) => {
127126
fs.ensureFileSync(outputFile);
128127
fs.writeFile(outputFile, result?.code || '');
129-
log.icon('🐶').success(`┈┈▶ \x1b[33;1m${folderFilePath}\x1b[0m => \x1b[33;1m${outFileName}\x1b[0m`);
128+
log
129+
.icon(getEmojiIcon(outputFile))
130+
.success(`${getExt(outFileName)}┈┈▶ \x1b[33;1m${folderFilePath}\x1b[0m => \x1b[33;1m${outFileName}\x1b[0m`);
130131
if (options.sourceMaps === 'both' || options.sourceMaps) {
131132
if (result?.map) {
132133
const sourceMapPath = path.join(outputFile + '.map');
133134
fs.writeFileSync(sourceMapPath, JSON.stringify(result?.map, null, 2));
134135
log
135-
.icon('🐶')
136+
.icon(getEmojiIcon(outputFile))
136137
.success(
137138
`┈┈▶ \x1b[33;1m${folderFilePath}\x1b[0m => \x1b[33;1m${path.relative(
138139
projectDirectory,

‎packages/babel/src/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { BabelCompileOptions } from './index.js';
66
* Convert suffix
77
*/
88
export const convertExtname = (str: string = '') => {
9-
return str.replace(/\.(m?js|m?ts)$/i, '.js').replace(/\.(jsx?|tsx?|c?js)$/i, '.js');
9+
return str.replace(/\.(m?ts|m?js|jsx?|tsx?|c?js)(?<!\.d\.ts)$/i, '.js');
1010
};
1111

1212
interface OutputPathResult {

‎packages/core/src/watcher/babelTransform.ts

+27-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import babelCompile, { getOutputPath } from '@tsbb/babel';
2-
import tsCompile, { findConfigFile, Log, getRootsFolderName, CopyFilesOptions } from '@tsbb/typescript';
2+
import tsCompile, { findConfigFile, Log, getExt, getRootsFolderName, CopyFilesOptions } from '@tsbb/typescript';
33
import path from 'node:path';
44
import ts from 'typescript';
55
import fs from 'fs-extra';
@@ -11,26 +11,42 @@ export function babelTransform(options: CompileOptions = {}) {
1111
const rootDirsRelative = [...new Set(getRootsFolderName(options.entry))];
1212
const entry = rootDirsRelative.map((item) => path.resolve(item));
1313
const onFilesChange: CopyFilesOptions['onFilesChange'] = (eventName, filepath, stats) => {
14-
if (/\.(m?js|jsx?|m?ts|tsx?|c?js)$/i.test(filepath) && !/\.d\.ts$/i.test(filepath)) {
15-
const log = new Log();
16-
if (/^(add|change)$/.test(eventName)) {
14+
if (/\.(m?js|jsx?|m?ts|tsx?|c?js)$/i.test(filepath)) {
15+
const log = new Log().name();
16+
const dt = getOutputPath(filepath, options);
17+
if (/^(add|change)$/.test(eventName) && !/\.d\.ts$/i.test(filepath)) {
1718
babelCompile(filepath, { ...options });
19+
} else if (/\.d\.ts$/i.test(filepath)) {
20+
if (typeof cjs !== 'boolean') {
21+
fs.ensureDirSync(path.dirname(dt.cjs.path));
22+
fs.copyFile(filepath, dt.cjs.path);
23+
log
24+
.icon('🐶')
25+
.success(
26+
`${getExt(filepath)}┈┈▶ \x1b[32;1m${dt.folderFilePath}\x1b[0m => \x1b[34;1m${dt.cjs.tsFileName}\x1b[0m`,
27+
);
28+
}
29+
if (typeof esm !== 'boolean') {
30+
fs.ensureDirSync(path.dirname(dt.esm.path));
31+
fs.copyFile(filepath, dt.esm.path);
32+
log
33+
.icon('🐶')
34+
.success(
35+
`${getExt(filepath)}┈┈▶ \x1b[32;1m${dt.folderFilePath}\x1b[0m => \x1b[34;1m${dt.esm.tsFileName}\x1b[0m`,
36+
);
37+
}
1838
}
1939
if (/^(unlink|unlinkDir)$/.test(eventName)) {
20-
const dt = getOutputPath(filepath, options);
2140
fs.remove(dt.cjs.path);
2241
fs.remove(dt.esm.path);
23-
log
24-
.name()
25-
.icon('🗑️')
26-
.success(`┈┈▶ \x1b[32;1m${path.relative(process.cwd(), filepath)}\x1b[0m`);
42+
log.icon('🗑️').success(`┈┈▶ \x1b[32;1m${path.relative(process.cwd(), filepath)}\x1b[0m`);
2743
}
2844
}
2945
};
3046
const onReady = () => {
3147
const log = new Log();
3248
if (!options.watch) {
33-
log.name().icon('\n🎉').error('\x1b[32;1mCompilation successful!\x1b[0m\n');
49+
// log.name().icon('\n🎉').error('\x1b[32;1mCompilation successful!\x1b[0m\n');
3450
} else {
3551
log.name().icon('\n🎉').error('\x1b[32;1mWatching for file changes.\x1b[0m\n');
3652
}
@@ -69,5 +85,5 @@ const writeFile = (to: string, target: string, fileName: string, content: string
6985
const log = new Log();
7086
log.name();
7187
ts.sys.writeFile(to, content, writeByteOrderMark);
72-
log.icon('🐳').success(`┈┈▶ [ts] \x1b[32;1m${fileName}\x1b[0m => \x1b[34;1m${target}\x1b[0m`);
88+
log.icon('🐳').success(`${getExt(fileName)}┈┈▶ \x1b[32;1m${fileName}\x1b[0m => \x1b[34;1m${target}\x1b[0m`);
7389
};

‎packages/typescript/src/utils.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ const getLnCol = (text: string = '', pos: number = 0) => {
2727
return { ln: lineNum, col: lineStartPos, text: lines[lineNum - 1] };
2828
};
2929

30-
const getEmoji = (extname: string) => {
31-
let ext = extname.toLocaleUpperCase().replace(/^\./, '');
30+
export const getExt = (extname: string) => {
31+
let ext = path.extname(extname).toLocaleUpperCase().replace(/^\./, '');
3232
ext = ext.padEnd(4, ' ');
3333
if (/^MAP/.test(ext)) {
3434
return `\x1b[34;1m${ext}\x1b[0m`;
@@ -41,16 +41,20 @@ const getEmoji = (extname: string) => {
4141
}
4242
};
4343

44+
export const getEmojiIcon = (fileName: string) => {
45+
let icon = /.d.ts$/i.test(fileName) ? '🐳' : '👉';
46+
icon = /.js.map$/i.test(fileName) ? '🚩' : icon;
47+
return icon;
48+
};
49+
4450
export const writeFile = (fileName: string, data: string, writeByteOrderMark?: boolean) => {
4551
const outputFile = path.join(process.cwd(), fileName);
4652
ts.sys.writeFile(outputFile, data, writeByteOrderMark);
4753
const log = new Log();
48-
let icon = /.d.ts$/i.test(fileName) ? '🐳' : '👉';
49-
icon = /.js.map$/i.test(fileName) ? '🚩' : icon;
5054
log
5155
.name()
52-
.icon(icon)
53-
.success(`${getEmoji(path.extname(fileName))}┈┈▶ \x1b[32;1m${fileName}\x1b[0m`);
56+
.icon(getEmojiIcon(fileName))
57+
.success(`${getExt(fileName)}┈┈▶ \x1b[32;1m${fileName}\x1b[0m`);
5458
};
5559

5660
export const getSourceFile: ts.CompilerHost['getSourceFile'] = (fileName, languageVersion, onError) => {

0 commit comments

Comments
 (0)
Please sign in to comment.