From 9625b5722fee42415e9c4c7809889d9b698a9516 Mon Sep 17 00:00:00 2001 From: jaywcjlove <398188662@qq.com> Date: Wed, 29 Mar 2023 13:01:26 +0800 Subject: [PATCH] type: fix copy *d.ts issue. --- examples/react-component/package.json | 4 +-- packages/babel/src/index.ts | 9 ++--- packages/babel/src/utils.ts | 2 +- packages/core/src/watcher/babelTransform.ts | 38 +++++++++++++++------ packages/typescript/src/utils.ts | 16 +++++---- 5 files changed, 45 insertions(+), 24 deletions(-) diff --git a/examples/react-component/package.json b/examples/react-component/package.json index 2a5eb45b..54f9f9f2 100644 --- a/examples/react-component/package.json +++ b/examples/react-component/package.json @@ -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", diff --git a/packages/babel/src/index.ts b/packages/babel/src/index.ts index 1accc9af..1cbdb5b2 100644 --- a/packages/babel/src/index.ts +++ b/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'; @@ -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, diff --git a/packages/babel/src/utils.ts b/packages/babel/src/utils.ts index 60f0b52f..1be6b013 100644 --- a/packages/babel/src/utils.ts +++ b/packages/babel/src/utils.ts @@ -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)(? 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'); } @@ -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`); }; diff --git a/packages/typescript/src/utils.ts b/packages/typescript/src/utils.ts index 4d213701..c9247a58 100644 --- a/packages/typescript/src/utils.ts +++ b/packages/typescript/src/utils.ts @@ -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`; @@ -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) => {