Skip to content

Commit

Permalink
type: fix copy *d.ts issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Mar 29, 2023
1 parent b3d6865 commit 9625b57
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 24 deletions.
4 changes: 2 additions & 2 deletions examples/react-component/package.json
Expand Up @@ -4,8 +4,8 @@
"private": true,
"description": "React Component Example.",
"scripts": {
"watch": "tsbb watch src/*.{jsx,js} --useBabel",
"build": "tsbb build src/*.{jsx,js} --useBabel && npm run css:build && npm run css:build:dist",
"watch": "tsbb watch src/*.{jsx,js,d.ts} --useBabel",
"build": "tsbb build src/*.{jsx,js,d.ts} --useBabel && npm run css:build && npm run css:build:dist",
"css:build": "compile-less -d src -o esm",
"css:watch": "compile-less -d src -o esm --watch",
"css:build:dist": "compile-less -d src --combine dist/markdown-editor.css --rm-global",
Expand Down
9 changes: 5 additions & 4 deletions packages/babel/src/index.ts
@@ -1,9 +1,8 @@
import fs from 'fs-extra';
import path from 'node:path';
import { Log, __dirname } from '@tsbb/typescript';
import { Log, getEmojiIcon, getExt, __dirname } from '@tsbb/typescript';
import { TransformOptions } from '@babel/core';
import babelPluginJsx from '@vue/babel-plugin-jsx';
// import { TransformOptions } from '@babel/core';
import { transform } from './transform.js';
import { getOutputPath } from './utils.js';
import { getCjsTransformOption, getESMTransformOption } from './config.js';
Expand Down Expand Up @@ -126,13 +125,15 @@ function transformFile(
.then((result) => {
fs.ensureFileSync(outputFile);
fs.writeFile(outputFile, result?.code || '');
log.icon('🐶').success(`┈┈▶ \x1b[33;1m${folderFilePath}\x1b[0m => \x1b[33;1m${outFileName}\x1b[0m`);
log
.icon(getEmojiIcon(outputFile))
.success(`${getExt(outFileName)}┈┈▶ \x1b[33;1m${folderFilePath}\x1b[0m => \x1b[33;1m${outFileName}\x1b[0m`);
if (options.sourceMaps === 'both' || options.sourceMaps) {
if (result?.map) {
const sourceMapPath = path.join(outputFile + '.map');
fs.writeFileSync(sourceMapPath, JSON.stringify(result?.map, null, 2));
log
.icon('🐶')
.icon(getEmojiIcon(outputFile))
.success(
`┈┈▶ \x1b[33;1m${folderFilePath}\x1b[0m => \x1b[33;1m${path.relative(
projectDirectory,
Expand Down
2 changes: 1 addition & 1 deletion packages/babel/src/utils.ts
Expand Up @@ -6,7 +6,7 @@ import { BabelCompileOptions } from './index.js';
* Convert suffix
*/
export const convertExtname = (str: string = '') => {
return str.replace(/\.(m?js|m?ts)$/i, '.js').replace(/\.(jsx?|tsx?|c?js)$/i, '.js');
return str.replace(/\.(m?ts|m?js|jsx?|tsx?|c?js)(?<!\.d\.ts)$/i, '.js');
};

interface OutputPathResult {
Expand Down
38 changes: 27 additions & 11 deletions packages/core/src/watcher/babelTransform.ts
@@ -1,5 +1,5 @@
import babelCompile, { getOutputPath } from '@tsbb/babel';
import tsCompile, { findConfigFile, Log, getRootsFolderName, CopyFilesOptions } from '@tsbb/typescript';
import tsCompile, { findConfigFile, Log, getExt, getRootsFolderName, CopyFilesOptions } from '@tsbb/typescript';
import path from 'node:path';
import ts from 'typescript';
import fs from 'fs-extra';
Expand All @@ -11,26 +11,42 @@ export function babelTransform(options: CompileOptions = {}) {
const rootDirsRelative = [...new Set(getRootsFolderName(options.entry))];
const entry = rootDirsRelative.map((item) => path.resolve(item));
const onFilesChange: CopyFilesOptions['onFilesChange'] = (eventName, filepath, stats) => {
if (/\.(m?js|jsx?|m?ts|tsx?|c?js)$/i.test(filepath) && !/\.d\.ts$/i.test(filepath)) {
const log = new Log();
if (/^(add|change)$/.test(eventName)) {
if (/\.(m?js|jsx?|m?ts|tsx?|c?js)$/i.test(filepath)) {
const log = new Log().name();
const dt = getOutputPath(filepath, options);
if (/^(add|change)$/.test(eventName) && !/\.d\.ts$/i.test(filepath)) {
babelCompile(filepath, { ...options });
} else if (/\.d\.ts$/i.test(filepath)) {
if (typeof cjs !== 'boolean') {
fs.ensureDirSync(path.dirname(dt.cjs.path));
fs.copyFile(filepath, dt.cjs.path);
log
.icon('🐶')
.success(
`${getExt(filepath)}┈┈▶ \x1b[32;1m${dt.folderFilePath}\x1b[0m => \x1b[34;1m${dt.cjs.tsFileName}\x1b[0m`,
);
}
if (typeof esm !== 'boolean') {
fs.ensureDirSync(path.dirname(dt.esm.path));
fs.copyFile(filepath, dt.esm.path);
log
.icon('🐶')
.success(
`${getExt(filepath)}┈┈▶ \x1b[32;1m${dt.folderFilePath}\x1b[0m => \x1b[34;1m${dt.esm.tsFileName}\x1b[0m`,
);
}
}
if (/^(unlink|unlinkDir)$/.test(eventName)) {
const dt = getOutputPath(filepath, options);
fs.remove(dt.cjs.path);
fs.remove(dt.esm.path);
log
.name()
.icon('🗑️')
.success(`┈┈▶ \x1b[32;1m${path.relative(process.cwd(), filepath)}\x1b[0m`);
log.icon('🗑️').success(`┈┈▶ \x1b[32;1m${path.relative(process.cwd(), filepath)}\x1b[0m`);
}
}
};
const onReady = () => {
const log = new Log();
if (!options.watch) {
log.name().icon('\n🎉').error('\x1b[32;1mCompilation successful!\x1b[0m\n');
// log.name().icon('\n🎉').error('\x1b[32;1mCompilation successful!\x1b[0m\n');
} else {
log.name().icon('\n🎉').error('\x1b[32;1mWatching for file changes.\x1b[0m\n');
}
Expand Down Expand Up @@ -69,5 +85,5 @@ const writeFile = (to: string, target: string, fileName: string, content: string
const log = new Log();
log.name();
ts.sys.writeFile(to, content, writeByteOrderMark);
log.icon('🐳').success(`┈┈▶ [ts] \x1b[32;1m${fileName}\x1b[0m => \x1b[34;1m${target}\x1b[0m`);
log.icon('🐳').success(`${getExt(fileName)}┈┈▶ \x1b[32;1m${fileName}\x1b[0m => \x1b[34;1m${target}\x1b[0m`);
};
16 changes: 10 additions & 6 deletions packages/typescript/src/utils.ts
Expand Up @@ -27,8 +27,8 @@ const getLnCol = (text: string = '', pos: number = 0) => {
return { ln: lineNum, col: lineStartPos, text: lines[lineNum - 1] };
};

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

export const getEmojiIcon = (fileName: string) => {
let icon = /.d.ts$/i.test(fileName) ? '🐳' : '👉';
icon = /.js.map$/i.test(fileName) ? '🚩' : icon;
return icon;
};

export const writeFile = (fileName: string, data: string, writeByteOrderMark?: boolean) => {
const outputFile = path.join(process.cwd(), fileName);
ts.sys.writeFile(outputFile, data, writeByteOrderMark);
const log = new Log();
let icon = /.d.ts$/i.test(fileName) ? '🐳' : '👉';
icon = /.js.map$/i.test(fileName) ? '🚩' : icon;
log
.name()
.icon(icon)
.success(`${getEmoji(path.extname(fileName))}┈┈▶ \x1b[32;1m${fileName}\x1b[0m`);
.icon(getEmojiIcon(fileName))
.success(`${getExt(fileName)}┈┈▶ \x1b[32;1m${fileName}\x1b[0m`);
};

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

0 comments on commit 9625b57

Please sign in to comment.