Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: 将@celljs/cli-common中使用了ts-node的地方,改为使用importx-tsup包进行动态require #211 #212

Merged
merged 6 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion dev-packages/cli-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
"temp": "^0.9.3",
"traverse": "^0.6.7",
"ts-node": "^10.9.1",
"typescript": "~5.4.5"
"typescript": "~5.4.5",
"importx-tsup": "^0.5.2"
},
"devDependencies": {
"@celljs/component": "3.0.0",
Expand Down
23 changes: 14 additions & 9 deletions dev-packages/cli-common/src/hook/hook-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,25 @@ export class HookExecutor {
return current === false ? false : true;
}

getModule(obj: any): any {
if (typeof obj === 'function') {
return obj;
} else if (typeof obj === 'object' && typeof obj.default === 'object' && typeof obj.default.default === 'function') {
return obj.default;
} else if (typeof obj === 'object' && typeof obj.default === 'function') {
return obj;
} else {return obj; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

} else {return obj; } 不建议放成一行

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{return obj; } 缺少空格 { return obj; }

}

protected async doRequire(context: CliContext, path: string, stage: HookStage = HookStage.on) {
const obj = require(path);
if (stage in obj) {
return obj[stage](context);
const obj = await import('importx-tsup').then(x => x.import(path, __filename));
const mod = this.getModule(obj);
if (mod[stage] && typeof mod[stage] === 'function') {
return mod[stage](context);
}
}

protected async doExecuteHooks(modules: Module[], context: CliContext, hookName: string, stage?: HookStage): Promise<any[]> {
const { REGISTER_INSTANCE, register } = require('ts-node');
// Avoid duplicate registrations
if (!(process as any)[REGISTER_INSTANCE]) {
register({ transpileOnly: true, compilerOptions: { module: 'commonjs'} });
}

const result: any = [];

for (const m of modules) {
Expand Down
3 changes: 3 additions & 0 deletions examples/backend-app/src/webpack-hook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default async () => {
console.log('webpack-hook');
}
Loading