-
Notifications
You must be signed in to change notification settings - Fork 93
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
feat: loader support custom extension #156
Conversation
Codecov Report
@@ Coverage Diff @@
## master #156 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 17 17
Lines 887 893 +6
=====================================
+ Hits 887 893 +6
Continue to review full report at Codecov.
|
lib/loader/file_loader.js
Outdated
@@ -23,6 +23,7 @@ const defaults = { | |||
override: false, | |||
inject: undefined, | |||
filter: null, | |||
extensions: [ '.js', '.ts' ], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里支持 !.d.ts
?
let files = this.options.match || [ '**/*.js' ]; | ||
files = Array.isArray(files) ? files : [ files ]; | ||
let files = this.options.match; | ||
if (!files) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里不能用原来的逻辑?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
如果用了 defaultMatch,就不需要再做 isArray 的判断了吧,因为就已经是 Array 了
lib/loader/file_loader.js
Outdated
files = Array.isArray(files) ? files : [ files ]; | ||
let files = this.options.match; | ||
if (!files) { | ||
files = this.defaultMatch; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
如果这样的话,是不是干脆配置下默认的 match 就好了,不用 extensions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
大概想扩展吧,比如 jsx 啥的
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
那也是放到默认的 match 吧,现在这样子不就是根据 extensions 指定生成 match 么
那还不如直接让开发者写 match 覆盖即可
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我想了又想,也觉得不该用 require.extensions,毕竟是已经弃用了。。。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
可以的,改成 typescript
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
在 application 中提供一个 extensions 可供配置允许加载的后缀名会不会更好一些?方便拓展 jsx、mjs 等
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
要么就根据配置的后缀加载所有文件,然后根据是否扩展 require.extension 判断是否使用这个文件,比如 ts 通过 egg-cluster 开起 ts extension,这时就会加载 ts 了,而运行时如果只使用 js 就去除 扩展 extension
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
你看他上面那个链接就是 require.extensions
, 这个方式还行,我只是担心这个 api 已经被标注为废弃
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
如果可以用 require.extensions,我就回滚到此前的 commit.(7c202b2)
没测试 |
@popomore 测试还没加好,所以还是 wip,先发上来给你们看看 |
可以在 application 加个 typescript 参数,开始这个后缀 |
单元测试已经补充,还是改回了通过 require.extensions 的方式。。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
如果这样的话, typescript: true
都不用了。
不加参数的话怕不兼容,有些 ssr 可能做了处理 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
需要测试 ts 和 js 同时存在的场景
package.json
Outdated
@@ -35,6 +35,7 @@ | |||
"devDependencies": { | |||
"autod": "^3.0.1", | |||
"coffee": "^4.1.0", | |||
"egg": "^2.5.0", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
实现在载 ts 就好,不要依赖 egg
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个是写 fixtures 的时候引入了 egg 的 types...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我看一下怎么去掉先
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
如果去掉的话,就只能按照 js 的方式来写 ts 了...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
测试的时候自己加一个 ts 的 extension 好了,断言转后的是否符合预期。
这里只要测 loader 就好了,另外可以在 egg 里加个用例
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK,我改一下
你指那些加了 babel register 的? |
我不确定,暂时我就支持 ts 吧,也不需要做的太扩展。 |
嗯,所以这里的修改意见是?
|
现在修改的地方只有两个
这两个地方应该保持一致,所以遍历的文件的地方可以改成互斥的,如果 typescript: true 只加载 ts 文件。require 的时候需要确保定义了 extension,可以加个断言,�判断倒是还可以用 require.extensions |
你指的是优先加 ts? 我觉得如果有同名的话,现在他的处理是优先加 js,这样才对 |
是只加载 ts,不加载 js |
只加载 ts ?那引入的那些用 js 写的插件呢,如果使用 require.extensions 就是,只有在使用了 ts node 的情况下,才会去加载 ts,否则根本都不会去 load ts |
已经改成使用 |
@@ -28,6 +28,7 @@ class EggCore extends KoaApplication { | |||
* @param {Object} options - options | |||
* @param {String} [options.baseDir=process.cwd()] - the directory of application | |||
* @param {String} [options.type=application|agent] - whether it's running in app worker or agent worker | |||
* @param {Boolean} [options.typescript] - whether support typescript |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
egg 的 d.ts 有没有这个的,回头要顺便加下
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个 typescript 会加载吧,不需要 loader 吧
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个 typescript 会加载吧,不需要 loader 吧
这个意思是?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个文件不需要我们去 require
lib/loader/file_loader.js
Outdated
@@ -48,6 +50,7 @@ class FileLoader { | |||
constructor(options) { | |||
assert(options.directory, 'options.directory is required'); | |||
assert(options.target, 'options.target is required'); | |||
assert(!options.typescript || (options.typescript && require.extensions['.ts']), 'require.extensions should contains .ts while options.typescript was true'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
require.extensions / options.typescript 用 ` 引起来
test/egg-ts.test.js
Outdated
}); | ||
|
||
it('should support load ts,js files', async () => { | ||
require.extensions['.ts'] = require.extensions['.js']; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
为啥不放 afterEach
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
其实在这个文件里面, 可以放 before 和 after 里面, 一次性即可
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
可以,我改一下
@popomore 再看看 |
lib/loader/file_loader.js
Outdated
@@ -48,6 +50,7 @@ class FileLoader { | |||
constructor(options) { | |||
assert(options.directory, 'options.directory is required'); | |||
assert(options.target, 'options.target is required'); | |||
assert(!options.typescript || (options.typescript && require.extensions['.ts']), 'require.extensions should contains .ts while options.typescript was true'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (options.typescript) assert(require.extensions['.ts'])
这样?如果太长可以换行
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK,我改一下
其他 +1 |
已更新 |
因为在 mixin 中很多是直接调用 还有一个更改就是 这样改后,就是当 options.typescript 为 你看看这个处理合理不? |
lib/loader/egg_loader.js
Outdated
@@ -287,6 +287,10 @@ class EggLoader { | |||
return null; | |||
} | |||
|
|||
if (!this.options.typescript && filepath.endsWith('.ts')) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这样看是不是在 resolveModule 处理比较好
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolveModule 是 utils 里的方法,要拿到 options 比较麻烦,得每一个调用都去传参,或者我把 resolveModule 移到 egg_loader 这边来?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
会用到 resolveModule 的都是 mixin 里的方法,感觉可以移到 egg_loader 上
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
可以的
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这样下面那个判断可以去掉了
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK,我改一下
其他同意 |
ci 挂 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
可以了
这个发版本没? |
|
* feat: loader support custom extension * feat: remove require.extensions * refactor: refactor to require.extensions * test: add unittest for ts * chore: remove tsconfig.json * test: add d.ts * test: test loadCustomApp and loadCustomAgent * test: add more test for custom extend * fix: spelling mistake * feat: add typescript options * docs: update typescript opt to docs * chore: update comment * test: add more unittest for ts * refactor: code optimization * chore: update error msg * test: change beforeEach/afterEach to before/after * feat: add ts check in loadFile * feat: move resolveModule to egg_loader * fix: lint fix * refactor: code optimization
Checklist
npm test
passesAffected core subsystem(s)
Description of change
loader 很多地方都是直接 hardcode 写死了加载 js,现在改成根据 require.extension 来决定加载哪些代码文件。
从而能够支持 ts-node 这类拓展 require.extension 来实现编译文件不落地的模块。