fix: 修复在创建模块时更新configuration/compiler.js会出现报错的问题

This commit is contained in:
Pan Qiancheng 2025-11-04 10:01:56 +08:00
parent 49b46c44a9
commit 9a50ce03d3
5 changed files with 22 additions and 8 deletions

View File

@ -234,8 +234,10 @@ async function create(dirName, cmd) {
(0, file_handle_1.checkFileExistsAndCreate)(tsConfigMpJsonPath, tsConfigMpJson, enum_1.checkFileExistsAndCreateType.FILE);
// 创建tsconfig.web.json
(0, file_handle_1.checkFileExistsAndCreate)(tsConfigWebJsonPath, tsConfigWebJson, enum_1.checkFileExistsAndCreateType.FILE);
// 更新configuration/compiler.js
(0, template_1.updateCompilerJsContent)(rootPath, deps);
// 更新configuration/compiler.js (仅在非模块化模式下)
if (!isModule) {
(0, template_1.updateCompilerJsContent)(rootPath, deps);
}
(0, tip_style_1.Success)(`${(0, tip_style_1.success)(`Successfully created project ${(0, tip_style_1.primary)(name)}, directory name is ${(0, tip_style_1.primary)(dirName)}`)}`);
shelljs_1.default.cd(dirName);
if (deps.length > 0) {

View File

@ -37,7 +37,7 @@ export declare function writeFile(path: string | PathLike, data: any): void;
export declare function readFile(path: string | PathLike, options?: {
encoding?: null | undefined;
flag?: string | undefined;
} | null): Buffer<ArrayBufferLike> | undefined;
} | null): NonSharedBuffer | undefined;
/**
* @name
* @export

View File

@ -636,8 +636,13 @@ function oakConfigContentWithWeb() {
}
function updateCompilerJsContent(directory, deps) {
const compilerJsPath = (0, path_1.join)(directory, 'configuration', 'compiler.js');
(0, assert_1.default)((0, fs_1.existsSync)(compilerJsPath));
// 只有在有依赖项时才需要修改 compiler.js
if (deps.length > 0) {
// 检查文件是否存在
if (!(0, fs_1.existsSync)(compilerJsPath)) {
console.warn(`Warning: ${compilerJsPath} does not exist, skipping compiler.js update`);
return;
}
const { ast } = (0, core_1.transformFileSync)(compilerJsPath, { ast: true });
const { program } = ast;
const { body } = program;

View File

@ -356,8 +356,10 @@ export async function create(dirName: string, cmd: any) {
tsConfigWebJson,
checkFileExistsAndCreateType.FILE
);
// 更新configuration/compiler.js
updateCompilerJsContent(rootPath, deps);
// 更新configuration/compiler.js (仅在非模块化模式下)
if (!isModule) {
updateCompilerJsContent(rootPath, deps);
}
Success(
`${success(
`Successfully created project ${primary(

View File

@ -663,9 +663,14 @@ export function oakConfigContentWithWeb() {
export function updateCompilerJsContent(directory: string, deps: string[]) {
const compilerJsPath = join(directory, 'configuration', 'compiler.js');
assert(existsSync(compilerJsPath));
// 只有在有依赖项时才需要修改 compiler.js
if (deps.length > 0) {
// 检查文件是否存在
if (!existsSync(compilerJsPath)) {
console.warn(`Warning: ${compilerJsPath} does not exist, skipping compiler.js update`);
return;
}
const { ast } = transformFileSync(compilerJsPath, { ast: true })!;
const { program } = ast!;
const { body } = program!;