Skip to content
Open
Changes from all commits
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
4 changes: 2 additions & 2 deletions lib/load_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ module.exports = app => {
continue;
}

if (stat.isFile() && path.extname(name) === '.js') {
if (stat.isFile() && [ '.js', '.ts' ].includes(path.extname(name))) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

这里感觉有问题,如果项目下刚好同时存在同名的 .js 和 .ts 文件,但是又没有使用 ts-node ,下面的 require 就会出问题

Copy link
Copy Markdown
Author

@sabakugaara sabakugaara Apr 23, 2020

Choose a reason for hiding this comment

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

有改法建议吗?@whxaxes

let handler = require(filepath);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

这里直接 require 也会有问题,ts compile 出来的 js 是 esm ,模块内容是挂在 default 中的

// support `module.exports = function (app) { return exports; }`
if (typeof handler === 'function') {
handler = handler(app);
}
let objectNames = path.basename(name, '.js');
let objectNames = path.basename(name, path.extname(name));
// api/sites/index.js => GET /sites
if (level >= 1 && objectNames === 'index') {
objectNames = path.basename(dir);
Expand Down