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

[DO NOT MERGE] code review #10

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ Load config/plugin.js

#### loadConfig

// config.default.js
Load config/config.js and config/{serverEnv}.js

#### loadController
Expand Down
1 change: 1 addition & 0 deletions lib/egg.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class EggCore extends KoaApplication {
* @member {Object} EggCore#options
* @since 1.0.0
*/
// 跟 jsdoc 不符, 如果暴露给外部的, 那是不是直接挂在 this.options 就好了?
this._options = options;

/**
Expand Down
3 changes: 3 additions & 0 deletions lib/loader/egg_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ class EggLoader {
*
* ```
* // lib/xx.js
* // 这里需要修改示例, egg-core ?
Copy link
Member

Choose a reason for hiding this comment

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

const EggCore = require('egg-core').EggCore;

class XxApplication extends EggCore {
  constructor(options) {
    super(options);
  }

  get [Symbol.for('egg#eggPath')]() {
    return path.join(__dirname, '..');
  }
}

* const egg = require('egg');
* class XxApplication extends egg.Application {
* constructor(options) {
Expand Down Expand Up @@ -190,6 +191,7 @@ class EggLoader {
return null;
}

// 这里调用的是 utils.loadFile, 但同名了, 会让人以为是递归
const ret = loadFile(filepath);
Copy link
Member

Choose a reason for hiding this comment

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

这是内部实现,因为多处使用了

Copy link
Member Author

Choose a reason for hiding this comment

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

建议是上面引入 utils, 这里 utils.loadFile

Copy link
Member

Choose a reason for hiding this comment

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

看不出有啥差异

Copy link
Member Author

@atian25 atian25 Aug 17, 2016

Choose a reason for hiding this comment

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

loadFile(filepath) {
  //...
  loadFile(filepath); // utils.loadFile
}

Copy link
Member

Choose a reason for hiding this comment

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

没问题吧。。。小白也不会来看这个仓库吧

Copy link
Member Author

Choose a reason for hiding this comment

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

有点洁癖, 不太喜欢同名覆盖, 担心踩坑. @_@

// function(arg1, args, ...) {}
let inject = Array.prototype.slice.call(arguments, 1);
Expand Down Expand Up @@ -221,6 +223,7 @@ class EggLoader {

if (this.orderPlugins) {
for (const plugin of this.orderPlugins) {
// 插件是在哪里定义的, 这样要不要暴露出去?
Copy link
Member

Choose a reason for hiding this comment

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

这个可以做,更友好的出错信息

Copy link
Member

Choose a reason for hiding this comment

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

这个单独做吧,改动有点多

dirs.push({
path: plugin.path,
type: 'plugin',
Expand Down
1 change: 1 addition & 0 deletions lib/loader/file_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const path = require('path');
const globby = require('globby');
const is = require('is-type-of');
const loadFile = require('../utils').loadFile;
// Symbol.for ?
Copy link
Member

Choose a reason for hiding this comment

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

内部实现,尽量少用全局的

const FULLPATH = Symbol('EGG_LOADER_ITEM_FULLPATH');
const EXPORTS = Symbol('EGG_LOADER_ITEM_EXPORTS');

Expand Down
1 change: 1 addition & 0 deletions lib/loader/mixin/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ module.exports = {
this.config = target;
},

// config 文件会加载两次, 这个到时候文档里面最好注明下
Copy link
Member

Choose a reason for hiding this comment

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

不会影响吧,有 require 缓存

Copy link
Member

Choose a reason for hiding this comment

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

可以改改优化下

Copy link
Member Author

Choose a reason for hiding this comment

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

module.exports = (appInfo, appConfig) {} 这是函数, 会执行两遍的, 不会缓存. 到时文档要提醒下, 避免他们在里面执行一些代码踩坑

_preloadAppConfig() {
const names = [
'config.default.js',
Expand Down
1 change: 1 addition & 0 deletions lib/loader/mixin/extend.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ module.exports = {
const filepaths = this.getLoadUnits()
.map(unit => {
let pluginExtendsPath = path.join(unit.path, 'app/extend');
// 还需要兼容 app 目录?
Copy link
Member

Choose a reason for hiding this comment

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

这个可以去掉

if (!fs.existsSync(pluginExtendsPath)) {
pluginExtendsPath = path.join(unit.path, 'app');
}
Expand Down
1 change: 1 addition & 0 deletions lib/loader/mixin/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module.exports = {
override: true,
lowercaseFirst: true,
}, opt);
// 感觉很多地方都会执行 map, 是不是加个辅助方法? getLoadDirs('app/middleware')
Copy link
Member

Choose a reason for hiding this comment

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

不想增加太多方法,loader 对性能的要求没那么高

const middlewarePaths = this.getLoadUnits().map(unit => join(unit.path, 'app/middleware'));
this.loadToApp(middlewarePaths, 'middlewares', opt);

Expand Down
2 changes: 2 additions & 0 deletions lib/loader/mixin/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ module.exports = {
debug('Got plugins %j after sequencify', result);

// catch error when result.sequence is empty
// 场景: plugin 是在应用层引入的, 开发期的 plugin 期望配置为应用的 devDeps, 当 npm install --prod 时不会被安装, 导致报错
// 建议: 当 plugin 是在应用层 plugin.js 中定义的, 且当前 env 下无需用到该插件, 则 skip 掉.
if (!result.sequence.length) {
Copy link
Member

@popomore popomore Aug 17, 2016

Choose a reason for hiding this comment

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

不建议这样做,插件应该显式依赖,多安装一个模块速度很快的

Copy link
Member Author

Choose a reason for hiding this comment

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

也行吧. 不过那是对我们而言, 我们有 tnpm. 对于外部那些还在用 npm 且没用cnpm源的, 装哪些带有 webpack / fis 这类构建工具的插件, 估计会很慢.

Copy link
Member

Choose a reason for hiding this comment

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

如果加 dev 更慢,还有一堆测试库

Copy link
Member Author

Choose a reason for hiding this comment

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

仅在开发期的 plugin, 放在 app 的 devDeps 里面.
线上包是 npm install -- prod 不会安装 dev 库包括测试库, 不会慢.

我们速度快是因为:

  • tnpm
  • ci 预先打包上线

但外面不一定有这种底层支撑.

Copy link
Member

Choose a reason for hiding this comment

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

静态文件打包和安装不是一起的?现在都是在 dependencies 里面的

Copy link
Member Author

Choose a reason for hiding this comment

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

举例可能有点不恰当, webpack 和 fis 确实在构建的时候有需要同时安装.

还有, 就是 webpack 和相关插件多大? 会不会导致 zip 包太大了?

可以先无视我吧, 后面开源后在看看有没有被挑战吧

const err = new Error(`sequencify plugins has problem, missing: [${result.missingTasks}], recursive: [${result.recursiveDependencies}]`);
// find plugins which is required by the missing plugin
Expand Down
1 change: 1 addition & 0 deletions lib/loader/mixin/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module.exports = {
app.use(router.middleware());

// 加载 router.js
// 增加对 app/router/index.js 的支持? 当 router 比较多的时候有分文件的需求
Copy link
Member

Choose a reason for hiding this comment

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

我觉得这里可以自己 require

Copy link
Member Author

Choose a reason for hiding this comment

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

router.js 和 router 目录? 两个在一起有点怪怪的.

Copy link
Member

Choose a reason for hiding this comment

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

这个是自己选择的,放 app 下也可以,router 代码放一个查询起来更方便

Copy link
Member Author

Choose a reason for hiding this comment

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

ok

this.loadFile(path.join(this.options.baseDir, 'app/router.js'));
},
};