Skip to content

Commit

Permalink
fix: fix babel compile cjs issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Mar 29, 2023
1 parent 6b53cb8 commit fa0d90c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
43 changes: 29 additions & 14 deletions packages/babel/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,27 +74,28 @@ export default async function compile(fileName: string, options: BabelCompileOpt
if ((options.sourceMaps as string) === 'true') {
options.sourceMaps = true;
}
let babelOptions =
typeof esm === 'string' ? getESMTransformOption() : typeof cjs === 'string' ? getCjsTransformOption() : {};

let esmBabelOptions = getESMTransformOption();
if (useVue) {
// @ts-ignore
babelOptions.plugins?.push(babelPluginJsx.default);
babelOptions.sourceMaps = options.sourceMaps || babelOptions.sourceMaps;
esmBabelOptions.plugins?.push(babelPluginJsx.default);
}
babelOptions.babelrc = true;
babelOptions.cwd = dt.projectDirectory;
if (envName) {
babelOptions = {};
babelOptions.envName = envName;
esmBabelOptions = {};
esmBabelOptions.envName = envName;
}
esmBabelOptions.sourceMaps = options.sourceMaps || esmBabelOptions.sourceMaps;
esmBabelOptions.babelrc = true;
esmBabelOptions.cwd = dt.projectDirectory;

if (typeof esm === 'string') {
babelOptions.sourceFileName = path.relative(path.dirname(dt.esm.path), fileName);
transform(fileName, { ...babelOptions })
esmBabelOptions.sourceFileName = path.relative(path.dirname(dt.esm.path), fileName);
transform(fileName, { ...esmBabelOptions })
.then((result) => {
fs.ensureFileSync(dt.esm.path);
fs.writeFile(dt.esm.path, result?.code || '');
log.icon('🐶').success(`┈┈▶ \x1b[32;1m${dt.folderFilePath}\x1b[0m => \x1b[34;1m${dt.esm.fileName}\x1b[0m`);
if (babelOptions.sourceMaps === 'both' || babelOptions.sourceMaps) {
if (esmBabelOptions.sourceMaps === 'both' || esmBabelOptions.sourceMaps) {
if (result?.map) {
const sourceMapPath = path.join(dt.esm.path + '.map');
fs.writeFileSync(sourceMapPath, JSON.stringify(result?.map, null, 2));
Expand All @@ -117,14 +118,28 @@ export default async function compile(fileName: string, options: BabelCompileOpt
}
});
}

let cjsBabelOptions = getCjsTransformOption();
if (useVue) {
// @ts-ignore
cjsBabelOptions.plugins?.push(babelPluginJsx.default);
}
if (envName) {
cjsBabelOptions = {};
cjsBabelOptions.envName = envName;
}
cjsBabelOptions.sourceMaps = options.sourceMaps || cjsBabelOptions.sourceMaps;
cjsBabelOptions.babelrc = true;
cjsBabelOptions.cwd = dt.projectDirectory;

if (typeof cjs === 'string') {
babelOptions.sourceFileName = path.relative(path.dirname(dt.cjs.path), fileName);
transform(fileName, { ...babelOptions })
cjsBabelOptions.sourceFileName = path.relative(path.dirname(dt.cjs.path), fileName);
transform(fileName, { ...cjsBabelOptions })
.then((result) => {
fs.ensureFileSync(dt.cjs.path);
fs.writeFile(dt.cjs.path, result?.code || '');
log.icon('🐶').success(`┈┈▶ \x1b[33;1m${dt.folderFilePath}\x1b[0m => \x1b[33;1m${dt.cjs.fileName}\x1b[0m`);
if (babelOptions.sourceMaps === 'both' || babelOptions.sourceMaps) {
if (cjsBabelOptions.sourceMaps === 'both' || cjsBabelOptions.sourceMaps) {
if (result?.map) {
const sourceMapPath = path.join(dt.cjs.path + '.map');
fs.writeFileSync(sourceMapPath, JSON.stringify(result?.map, null, 2));
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/watcher/babelTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function babelTransform(options: CompileOptions = {}) {
onWriteFile,
});
}
watcherCopyFiles(entry, { isWatch: options.watch, onFilesChange, onReady });
watcherCopyFiles(entry, { isWatch: options.watch, onFilesChange, onReady, rootDirsRelative });
}

const writeFile = (to: string, target: string, fileName: string, content: string, writeByteOrderMark?: boolean) => {
Expand Down
10 changes: 0 additions & 10 deletions packages/typescript/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import ts from 'typescript';
import path from 'node:path';
import fs from 'fs-extra';
import {
writeFile,
getSourceFile,
Expand Down Expand Up @@ -28,13 +27,6 @@ export interface CopyFilesOptions {
* @example ['src', 'demo']
*/
rootDirsRelative?: string[];
onFilesChange?: (
eventName: 'add' | 'addDir' | 'change' | 'unlink' | 'unlinkDir',
path: string,
stats?: fs.Stats,
) => void;
onError?: (error: any) => void;
onReady?: () => void;
}

export interface TsCompileOptions {
Expand Down Expand Up @@ -123,7 +115,6 @@ export default async function compile(options: TsCompileOptions = {}) {
ts.createWatchProgram(host);
if (isCopyFiles && onCopyFiles) {
await onCopyFiles(rootDirs, { isWatch: options.watch, outputDir, currentDir, rootDirsRelative });
// await watcherCopyFiles(rootDirs, { isWatch: options.watch, outputDir, currentDir, rootDirsRelative });
}
} else {
const compilerHost = ts.createCompilerHost(compilerOptions, true);
Expand All @@ -149,7 +140,6 @@ export default async function compile(options: TsCompileOptions = {}) {
diagnostics.forEach(reportDiagnostic);
if (isCopyFiles && onCopyFiles) {
await onCopyFiles(rootDirs, { isWatch: options.watch, outputDir, currentDir, rootDirsRelative });
// await watcherCopyFiles(rootDirs, { isWatch: options.watch, outputDir, currentDir, rootDirsRelative });
}
if (!options.emitDeclarationOnly && onWriteFile) {
if (emitResult.emitSkipped) {
Expand Down

0 comments on commit fa0d90c

Please sign in to comment.