Skip to content

Commit

Permalink
feat: resolve local plugin, close #336, close #338
Browse files Browse the repository at this point in the history
  • Loading branch information
Scrum committed Jun 11, 2021
1 parent 2a0cb7d commit 44d877b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/cli.js
Expand Up @@ -9,6 +9,7 @@ import updateNotifier from 'update-notifier';
import posthtml from 'posthtml';
import outResolve from './out-resolve';
import cfgResolve from './cfg-resolve';
import pluginResolve from './plugin-resolve';

const package_ = require('../package.json');
updateNotifier({pkg: package_}).notify();
Expand Down Expand Up @@ -93,7 +94,7 @@ const read = file => new Promise(resolve => {
const interopRequire = object => object && object.__esModule ? object.default : object;

const getPlugins = config => Object.keys(config.plugins || {})
.map(plugin => interopRequire(require(plugin))(config.plugins[plugin]));
.map(plugin => interopRequire(require(pluginResolve(plugin, config.root)))(config.plugins[plugin]));

const config = cfgResolve(cli);

Expand Down
23 changes: 23 additions & 0 deletions src/plugin-resolve.js
@@ -0,0 +1,23 @@
import path from 'path';

const pluginExist = (pluginPath) => {
try {
require(pluginPath);
return true;
} catch {}

return false;
}

export default (pluginName, root = './') => {
if (pluginExist(pluginName)) {
return pluginName;
}

const pluginResolvePath = path.resolve(path.join(root, pluginName));
if (pluginExist(pluginResolvePath)) {
return pluginResolvePath;
}

return pluginName;
}

0 comments on commit 44d877b

Please sign in to comment.