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

fix: can't get appConfig in appConfig #14

Merged
merged 1 commit into from
Aug 18, 2016
Merged
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
7 changes: 3 additions & 4 deletions lib/loader/egg_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ const isFunction = require('is-type-of').function;
const debug = require('debug')('egg-core');
const FileLoader = require('./file_loader');
const ContextLoader = require('./context_loader');
const loadFile = require('../utils').loadFile;
const getHomedir = require('../utils').getHomedir;
const utils = require('../utils');
const EggCore = require('../egg');

class EggLoader {
Expand Down Expand Up @@ -57,7 +56,7 @@ class EggLoader {
name: this.getAppname(),
baseDir: this.options.baseDir,
env: this.serverEnv,
HOME: getHomedir(),
HOME: utils.getHomedir(),
pkg: this.pkg,
};
}
Expand Down Expand Up @@ -190,7 +189,7 @@ class EggLoader {
return null;
}

const ret = loadFile(filepath);
const ret = utils.loadFile(filepath);
// function(arg1, args, ...) {}
let inject = Array.prototype.slice.call(arguments, 1);
if (inject.length === 0) inject = [ this.app ];
Expand Down
4 changes: 2 additions & 2 deletions lib/loader/file_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const debug = require('debug')('egg-core:loader');
const path = require('path');
const globby = require('globby');
const is = require('is-type-of');
const loadFile = require('../utils').loadFile;
const utils = require('../utils');
const FULLPATH = Symbol('EGG_LOADER_ITEM_FULLPATH');
const EXPORTS = Symbol('EGG_LOADER_ITEM_EXPORTS');

Expand Down Expand Up @@ -115,7 +115,7 @@ function getProperties(filepath, lowercaseFirst) {
// Get exports from filepath
// If exports is null/undefined, it will be ignored
function getExports(fullpath, initializer, isCall, inject) {
let exports = loadFile(fullpath);
let exports = utils.loadFile(fullpath);

// process exports as you like
if (initializer) {
Expand Down
3 changes: 2 additions & 1 deletion lib/loader/mixin/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ module.exports = {
// app config.{env}
for (const filename of names) {
for (const unit of this.getLoadUnits()) {
const config = this._loadConfig(unit.path, filename, appConfig, unit.type);
const isApp = unit.type === 'app';
const config = this._loadConfig(unit.path, filename, isApp ? undefined : appConfig, unit.type);
Copy link
Member

Choose a reason for hiding this comment

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

喔, 明白了. 我之前理解错了, 应该是:

  • {app_root}/config/config.default.js 会先执行一次, 且只会执行一次
  • 然后在 plugin / framework 中的 config 执行, 传递上面的结果为第二个参数


if (!config) {
continue;
Expand Down
5 changes: 3 additions & 2 deletions test/fixtures/preload-app-config/config/config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
'use strict';

module.exports = function(antx) {
module.exports = function(antx, appConfig) {
return {
app: {
sub: {
val: 1
},
val: 2,
}
},
appInApp: appConfig != null,
};
};
1 change: 1 addition & 0 deletions test/loader/mixin/load_config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,6 @@ describe('test/loader/mixin/load_config.test.js', function() {
loader.config.plugin.val.should.eql(2);
loader.config.plugin.val.should.eql(2);
loader.config.plugin.sub.should.not.equal(loader.config.app.sub);
loader.config.appInApp.should.false();
Copy link
Member

Choose a reason for hiding this comment

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

每次看到 should 这种写法,我都怀疑到底有没判断正确。

Copy link
Member

Choose a reason for hiding this comment

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

搞个PR全换power assert咋样

});
});