Skip to content

Commit

Permalink
feat: loader support custom extension
Browse files Browse the repository at this point in the history
  • Loading branch information
whxaxes committed Mar 20, 2018
1 parent 9370f86 commit 7c202b2
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 28 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ coverage
.logs
npm-debug.log
.vscode
.DS_Store
.DS_Store
yarn.lock
10 changes: 5 additions & 5 deletions lib/loader/egg_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ class EggLoader {
* @since 1.0.0
*/
loadFile(filepath, ...inject) {
if (!fs.existsSync(filepath)) {
if (!filepath || !fs.existsSync(filepath)) {
return null;
}

Expand Down Expand Up @@ -391,12 +391,12 @@ class EggLoader {
}

getTypeFiles(filename) {
const files = [ `${filename}.default.js` ];
if (this.serverScope) files.push(`${filename}.${this.serverScope}.js`);
const files = [ `${filename}.default` ];
if (this.serverScope) files.push(`${filename}.${this.serverScope}`);
if (this.serverEnv === 'default') return files;

files.push(`${filename}.${this.serverEnv}.js`);
if (this.serverScope) files.push(`${filename}.${this.serverScope}_${this.serverEnv}.js`);
files.push(`${filename}.${this.serverEnv}`);
if (this.serverScope) files.push(`${filename}.${this.serverScope}_${this.serverEnv}`);
return files;
}
}
Expand Down
12 changes: 10 additions & 2 deletions lib/loader/file_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,16 @@ class FileLoader {
* @since 1.0.0
*/
parse() {
let files = this.options.match || [ '**/*.js' ];
files = Array.isArray(files) ? files : [ files ];
let files = this.options.match;
if (!files) {
const extensions = Object.keys(require.extensions).map(ext => ext.substring(1));
files = [ '**/*.(' + extensions.join('|') + ')' ];
if (extensions.includes('ts')) {
files.push('!**/*.d.ts');
}
} else {
files = Array.isArray(files) ? files : [ files ];
}

let ignore = this.options.ignore;
if (ignore) {
Expand Down
13 changes: 7 additions & 6 deletions lib/loader/mixin/config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';

const debug = require('debug')('egg-core:config');
const fs = require('fs');
const path = require('path');
const extend = require('extend2');
const assert = require('assert');
const utils = require('../../utils');

const SET_CONFIG_META = Symbol('Loader#setConfigMeta');

Expand Down Expand Up @@ -55,8 +55,8 @@ module.exports = {

_preloadAppConfig() {
const names = [
'config.default.js',
`config.${this.serverEnv}.js`,
'config.default',
`config.${this.serverEnv}`,
];
const target = {};
for (const filename of names) {
Expand All @@ -70,12 +70,13 @@ module.exports = {
const isPlugin = type === 'plugin';
const isApp = type === 'app';

let filepath = path.join(dirpath, 'config', filename);
let filepath = utils.resolveModule(path.join(dirpath, 'config', filename));
// let config.js compatible
if (filename === 'config.default.js' && !fs.existsSync(filepath)) {
filepath = path.join(dirpath, 'config/config.js');
if (filename === 'config.default' && !filepath) {
filepath = utils.resolveModule(path.join(dirpath, 'config/config'));
}
const config = this.loadFile(filepath, this.appInfo, extraInject);

if (!config) return null;

if (isPlugin || isApp) {
Expand Down
5 changes: 3 additions & 2 deletions lib/loader/mixin/custom.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const path = require('path');
const utils = require('../../utils');

module.exports = {

Expand All @@ -22,15 +23,15 @@ module.exports = {
*/
loadCustomApp() {
this.getLoadUnits()
.forEach(unit => this.loadFile(path.join(unit.path, 'app.js')));
.forEach(unit => this.loadFile(utils.resolveModule(path.join(unit.path, 'app'))));
},

/**
* Load agent.js, same as {@link EggLoader#loadCustomApp}
*/
loadCustomAgent() {
this.getLoadUnits()
.forEach(unit => this.loadFile(path.join(unit.path, 'agent.js')));
.forEach(unit => this.loadFile(utils.resolveModule(path.join(unit.path, 'agent'))));
},

};
4 changes: 2 additions & 2 deletions lib/loader/mixin/extend.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ module.exports = {
const isAddUnittest = 'EGG_MOCK_SERVER_ENV' in process.env && this.serverEnv !== 'unittest';
for (let i = 0, l = filepaths.length; i < l; i++) {
const filepath = filepaths[i];
filepaths.push(filepath + `.${this.serverEnv}.js`);
if (isAddUnittest) filepaths.push(filepath + '.unittest.js');
filepaths.push(filepath + `.${this.serverEnv}`);
if (isAddUnittest) filepaths.push(filepath + '.unittest');
}

const mergeRecord = new Map();
Expand Down
19 changes: 11 additions & 8 deletions lib/loader/mixin/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const path = require('path');
const debug = require('debug')('egg-core:plugin');
const sequencify = require('../../utils/sequencify');
const loadFile = require('../../utils').loadFile;
const utils = require('../../utils');

module.exports = {

Expand Down Expand Up @@ -56,11 +57,11 @@ module.exports = {
*/
loadPlugin() {
// loader plugins from application
const appPlugins = this.readPluginConfigs(path.join(this.options.baseDir, 'config/plugin.default.js'));
const appPlugins = this.readPluginConfigs(path.join(this.options.baseDir, 'config/plugin.default'));
debug('Loaded app plugins: %j', Object.keys(appPlugins));

// loader plugins from framework
const eggPluginConfigPaths = this.eggPaths.map(eggPath => path.join(eggPath, 'config/plugin.default.js'));
const eggPluginConfigPaths = this.eggPaths.map(eggPath => path.join(eggPath, 'config/plugin.default'));
const eggPlugins = this.readPluginConfigs(eggPluginConfigPaths);
debug('Loaded egg plugins: %j', Object.keys(eggPlugins));

Expand Down Expand Up @@ -159,20 +160,22 @@ module.exports = {
}

const plugins = {};
for (let configPath of newConfigPaths) {
for (const configPath of newConfigPaths) {
let filepath = utils.resolveModule(configPath);

// let plugin.js compatible
if (configPath.endsWith('plugin.default.js') && !fs.existsSync(configPath)) {
configPath = configPath.replace(/plugin\.default\.js$/, 'plugin.js');
if (configPath.endsWith('plugin.default') && !filepath) {
filepath = utils.resolveModule(configPath.replace(/plugin\.default$/, 'plugin'));
}

if (!fs.existsSync(configPath)) {
if (!filepath) {
continue;
}

const config = loadFile(configPath);
const config = loadFile(filepath);

for (const name in config) {
this.normalizePluginConfig(config, name, configPath);
this.normalizePluginConfig(config, name, filepath);
}

this._extendPlugins(plugins, config);
Expand Down
3 changes: 2 additions & 1 deletion lib/loader/mixin/router.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const path = require('path');
const utils = require('../../utils');

module.exports = {

Expand All @@ -12,6 +13,6 @@ module.exports = {
*/
loadRouter() {
// 加载 router.js
this.loadFile(path.join(this.options.baseDir, 'app/router.js'));
this.loadFile(utils.resolveModule(path.join(this.options.baseDir, 'app/router')));
},
};
2 changes: 1 addition & 1 deletion lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = {
try {
// if not js module, just return content buffer
const extname = path.extname(filepath);
if (![ '.js', '.node', '.json', '' ].includes(extname)) {
if (extname && !require.extensions[extname]) {
return fs.readFileSync(filepath);
}
// require js module
Expand Down

0 comments on commit 7c202b2

Please sign in to comment.