Skip to content

Commit 1f0747e

Browse files
committedMar 29, 2023
perf: optimize babel transform file.
1 parent fa0d90c commit 1f0747e

File tree

1 file changed

+43
-56
lines changed

1 file changed

+43
-56
lines changed
 

‎packages/babel/src/index.ts

+43-56
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import path from 'node:path';
33
import { Log, __dirname } from '@tsbb/typescript';
44
import { TransformOptions } from '@babel/core';
55
import babelPluginJsx from '@vue/babel-plugin-jsx';
6+
// import { TransformOptions } from '@babel/core';
67
import { transform } from './transform.js';
78
import { getOutputPath } from './utils.js';
89
import { getCjsTransformOption, getESMTransformOption } from './config.js';
@@ -89,34 +90,7 @@ export default async function compile(fileName: string, options: BabelCompileOpt
8990
esmBabelOptions.cwd = dt.projectDirectory;
9091

9192
if (typeof esm === 'string') {
92-
esmBabelOptions.sourceFileName = path.relative(path.dirname(dt.esm.path), fileName);
93-
transform(fileName, { ...esmBabelOptions })
94-
.then((result) => {
95-
fs.ensureFileSync(dt.esm.path);
96-
fs.writeFile(dt.esm.path, result?.code || '');
97-
log.icon('🐶').success(`┈┈▶ \x1b[32;1m${dt.folderFilePath}\x1b[0m => \x1b[34;1m${dt.esm.fileName}\x1b[0m`);
98-
if (esmBabelOptions.sourceMaps === 'both' || esmBabelOptions.sourceMaps) {
99-
if (result?.map) {
100-
const sourceMapPath = path.join(dt.esm.path + '.map');
101-
fs.writeFileSync(sourceMapPath, JSON.stringify(result?.map, null, 2));
102-
log
103-
.icon('🐶')
104-
.success(
105-
`┈┈▶ \x1b[32;1m${dt.folderFilePath}\x1b[0m => \x1b[34;1m${path.relative(
106-
dt.projectDirectory,
107-
sourceMapPath,
108-
)}\x1b[0m`,
109-
);
110-
}
111-
}
112-
})
113-
.catch((error) => {
114-
if (error instanceof Error) {
115-
log.icon('\n🚨').error(`\x1b[33;1m ${error.message}\x1b[0m\n`);
116-
} else {
117-
log.icon('\n🚨').error(`\x1b[33;1m ${JSON.stringify(error)}\x1b[0m\n`);
118-
}
119-
});
93+
transformFile(fileName, dt.esm.path, dt.folderFilePath, dt.projectDirectory, dt.esm.fileName, esmBabelOptions);
12094
}
12195

12296
let cjsBabelOptions = getCjsTransformOption();
@@ -133,33 +107,46 @@ export default async function compile(fileName: string, options: BabelCompileOpt
133107
cjsBabelOptions.cwd = dt.projectDirectory;
134108

135109
if (typeof cjs === 'string') {
136-
cjsBabelOptions.sourceFileName = path.relative(path.dirname(dt.cjs.path), fileName);
137-
transform(fileName, { ...cjsBabelOptions })
138-
.then((result) => {
139-
fs.ensureFileSync(dt.cjs.path);
140-
fs.writeFile(dt.cjs.path, result?.code || '');
141-
log.icon('🐶').success(`┈┈▶ \x1b[33;1m${dt.folderFilePath}\x1b[0m => \x1b[33;1m${dt.cjs.fileName}\x1b[0m`);
142-
if (cjsBabelOptions.sourceMaps === 'both' || cjsBabelOptions.sourceMaps) {
143-
if (result?.map) {
144-
const sourceMapPath = path.join(dt.cjs.path + '.map');
145-
fs.writeFileSync(sourceMapPath, JSON.stringify(result?.map, null, 2));
146-
log
147-
.icon('🐶')
148-
.success(
149-
`┈┈▶ \x1b[33;1m${dt.folderFilePath}\x1b[0m => \x1b[33;1m${path.relative(
150-
dt.projectDirectory,
151-
sourceMapPath,
152-
)}\x1b[0m`,
153-
);
154-
}
155-
}
156-
})
157-
.catch((error) => {
158-
if (error instanceof Error) {
159-
log.icon('\n🚨').error(`\x1b[33;1m ${error.message}\x1b[0m\n`);
160-
} else {
161-
log.icon('\n🚨').error(`\x1b[33;1m ${JSON.stringify(error)}\x1b[0m\n`);
162-
}
163-
});
110+
transformFile(fileName, dt.cjs.path, dt.folderFilePath, dt.projectDirectory, dt.cjs.fileName, cjsBabelOptions);
164111
}
165112
}
113+
114+
function transformFile(
115+
fileName: string,
116+
outputFile: string,
117+
folderFilePath: string,
118+
projectDirectory: string,
119+
outFileName: string,
120+
options: TransformOptions,
121+
) {
122+
const log = new Log();
123+
log.name();
124+
options.sourceFileName = path.relative(path.dirname(outputFile), fileName);
125+
transform(fileName, { ...options })
126+
.then((result) => {
127+
fs.ensureFileSync(outputFile);
128+
fs.writeFile(outputFile, result?.code || '');
129+
log.icon('🐶').success(`┈┈▶ \x1b[33;1m${folderFilePath}\x1b[0m => \x1b[33;1m${outFileName}\x1b[0m`);
130+
if (options.sourceMaps === 'both' || options.sourceMaps) {
131+
if (result?.map) {
132+
const sourceMapPath = path.join(outputFile + '.map');
133+
fs.writeFileSync(sourceMapPath, JSON.stringify(result?.map, null, 2));
134+
log
135+
.icon('🐶')
136+
.success(
137+
`┈┈▶ \x1b[33;1m${folderFilePath}\x1b[0m => \x1b[33;1m${path.relative(
138+
projectDirectory,
139+
sourceMapPath,
140+
)}\x1b[0m`,
141+
);
142+
}
143+
}
144+
})
145+
.catch((error) => {
146+
if (error instanceof Error) {
147+
log.icon('\n🚨').error(`\x1b[33;1m ${error.message}\x1b[0m\n`);
148+
} else {
149+
log.icon('\n🚨').error(`\x1b[33;1m ${JSON.stringify(error)}\x1b[0m\n`);
150+
}
151+
});
152+
}

0 commit comments

Comments
 (0)
Please sign in to comment.