Skip to content

Commit fa0d90c

Browse files
committedMar 29, 2023
fix: fix babel compile cjs issue.
1 parent 6b53cb8 commit fa0d90c

File tree

3 files changed

+30
-25
lines changed

3 files changed

+30
-25
lines changed
 

‎packages/babel/src/index.ts

+29-14
Original file line numberDiff line numberDiff line change
@@ -74,27 +74,28 @@ export default async function compile(fileName: string, options: BabelCompileOpt
7474
if ((options.sourceMaps as string) === 'true') {
7575
options.sourceMaps = true;
7676
}
77-
let babelOptions =
78-
typeof esm === 'string' ? getESMTransformOption() : typeof cjs === 'string' ? getCjsTransformOption() : {};
77+
78+
let esmBabelOptions = getESMTransformOption();
7979
if (useVue) {
8080
// @ts-ignore
81-
babelOptions.plugins?.push(babelPluginJsx.default);
82-
babelOptions.sourceMaps = options.sourceMaps || babelOptions.sourceMaps;
81+
esmBabelOptions.plugins?.push(babelPluginJsx.default);
8382
}
84-
babelOptions.babelrc = true;
85-
babelOptions.cwd = dt.projectDirectory;
8683
if (envName) {
87-
babelOptions = {};
88-
babelOptions.envName = envName;
84+
esmBabelOptions = {};
85+
esmBabelOptions.envName = envName;
8986
}
87+
esmBabelOptions.sourceMaps = options.sourceMaps || esmBabelOptions.sourceMaps;
88+
esmBabelOptions.babelrc = true;
89+
esmBabelOptions.cwd = dt.projectDirectory;
90+
9091
if (typeof esm === 'string') {
91-
babelOptions.sourceFileName = path.relative(path.dirname(dt.esm.path), fileName);
92-
transform(fileName, { ...babelOptions })
92+
esmBabelOptions.sourceFileName = path.relative(path.dirname(dt.esm.path), fileName);
93+
transform(fileName, { ...esmBabelOptions })
9394
.then((result) => {
9495
fs.ensureFileSync(dt.esm.path);
9596
fs.writeFile(dt.esm.path, result?.code || '');
9697
log.icon('🐶').success(`┈┈▶ \x1b[32;1m${dt.folderFilePath}\x1b[0m => \x1b[34;1m${dt.esm.fileName}\x1b[0m`);
97-
if (babelOptions.sourceMaps === 'both' || babelOptions.sourceMaps) {
98+
if (esmBabelOptions.sourceMaps === 'both' || esmBabelOptions.sourceMaps) {
9899
if (result?.map) {
99100
const sourceMapPath = path.join(dt.esm.path + '.map');
100101
fs.writeFileSync(sourceMapPath, JSON.stringify(result?.map, null, 2));
@@ -117,14 +118,28 @@ export default async function compile(fileName: string, options: BabelCompileOpt
117118
}
118119
});
119120
}
121+
122+
let cjsBabelOptions = getCjsTransformOption();
123+
if (useVue) {
124+
// @ts-ignore
125+
cjsBabelOptions.plugins?.push(babelPluginJsx.default);
126+
}
127+
if (envName) {
128+
cjsBabelOptions = {};
129+
cjsBabelOptions.envName = envName;
130+
}
131+
cjsBabelOptions.sourceMaps = options.sourceMaps || cjsBabelOptions.sourceMaps;
132+
cjsBabelOptions.babelrc = true;
133+
cjsBabelOptions.cwd = dt.projectDirectory;
134+
120135
if (typeof cjs === 'string') {
121-
babelOptions.sourceFileName = path.relative(path.dirname(dt.cjs.path), fileName);
122-
transform(fileName, { ...babelOptions })
136+
cjsBabelOptions.sourceFileName = path.relative(path.dirname(dt.cjs.path), fileName);
137+
transform(fileName, { ...cjsBabelOptions })
123138
.then((result) => {
124139
fs.ensureFileSync(dt.cjs.path);
125140
fs.writeFile(dt.cjs.path, result?.code || '');
126141
log.icon('🐶').success(`┈┈▶ \x1b[33;1m${dt.folderFilePath}\x1b[0m => \x1b[33;1m${dt.cjs.fileName}\x1b[0m`);
127-
if (babelOptions.sourceMaps === 'both' || babelOptions.sourceMaps) {
142+
if (cjsBabelOptions.sourceMaps === 'both' || cjsBabelOptions.sourceMaps) {
128143
if (result?.map) {
129144
const sourceMapPath = path.join(dt.cjs.path + '.map');
130145
fs.writeFileSync(sourceMapPath, JSON.stringify(result?.map, null, 2));

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export function babelTransform(options: CompileOptions = {}) {
6262
onWriteFile,
6363
});
6464
}
65-
watcherCopyFiles(entry, { isWatch: options.watch, onFilesChange, onReady });
65+
watcherCopyFiles(entry, { isWatch: options.watch, onFilesChange, onReady, rootDirsRelative });
6666
}
6767

6868
const writeFile = (to: string, target: string, fileName: string, content: string, writeByteOrderMark?: boolean) => {

‎packages/typescript/src/index.ts

-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import ts from 'typescript';
22
import path from 'node:path';
3-
import fs from 'fs-extra';
43
import {
54
writeFile,
65
getSourceFile,
@@ -28,13 +27,6 @@ export interface CopyFilesOptions {
2827
* @example ['src', 'demo']
2928
*/
3029
rootDirsRelative?: string[];
31-
onFilesChange?: (
32-
eventName: 'add' | 'addDir' | 'change' | 'unlink' | 'unlinkDir',
33-
path: string,
34-
stats?: fs.Stats,
35-
) => void;
36-
onError?: (error: any) => void;
37-
onReady?: () => void;
3830
}
3931

4032
export interface TsCompileOptions {
@@ -123,7 +115,6 @@ export default async function compile(options: TsCompileOptions = {}) {
123115
ts.createWatchProgram(host);
124116
if (isCopyFiles && onCopyFiles) {
125117
await onCopyFiles(rootDirs, { isWatch: options.watch, outputDir, currentDir, rootDirsRelative });
126-
// await watcherCopyFiles(rootDirs, { isWatch: options.watch, outputDir, currentDir, rootDirsRelative });
127118
}
128119
} else {
129120
const compilerHost = ts.createCompilerHost(compilerOptions, true);
@@ -149,7 +140,6 @@ export default async function compile(options: TsCompileOptions = {}) {
149140
diagnostics.forEach(reportDiagnostic);
150141
if (isCopyFiles && onCopyFiles) {
151142
await onCopyFiles(rootDirs, { isWatch: options.watch, outputDir, currentDir, rootDirsRelative });
152-
// await watcherCopyFiles(rootDirs, { isWatch: options.watch, outputDir, currentDir, rootDirsRelative });
153143
}
154144
if (!options.emitDeclarationOnly && onWriteFile) {
155145
if (emitResult.emitSkipped) {

0 commit comments

Comments
 (0)
Please sign in to comment.