Skip to content

Commit 44d877b

Browse files
committed
feat: resolve local plugin, close #336, close #338
1 parent 2a0cb7d commit 44d877b

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/cli.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import updateNotifier from 'update-notifier';
99
import posthtml from 'posthtml';
1010
import outResolve from './out-resolve';
1111
import cfgResolve from './cfg-resolve';
12+
import pluginResolve from './plugin-resolve';
1213

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

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

9899
const config = cfgResolve(cli);
99100

src/plugin-resolve.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import path from 'path';
2+
3+
const pluginExist = (pluginPath) => {
4+
try {
5+
require(pluginPath);
6+
return true;
7+
} catch {}
8+
9+
return false;
10+
}
11+
12+
export default (pluginName, root = './') => {
13+
if (pluginExist(pluginName)) {
14+
return pluginName;
15+
}
16+
17+
const pluginResolvePath = path.resolve(path.join(root, pluginName));
18+
if (pluginExist(pluginResolvePath)) {
19+
return pluginResolvePath;
20+
}
21+
22+
return pluginName;
23+
}

0 commit comments

Comments
 (0)