Skip to content

Commit 13f4deb

Browse files
authored
fix: Plugin resolution for relative paths (#1194)
Resolve plugin modules relative to the current path instead of the internal path. Fixes #1188
1 parent 02e6a0e commit 13f4deb

File tree

4 files changed

+58
-1
lines changed

4 files changed

+58
-1
lines changed

src/lib/utils/plugins.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class PluginHost extends AbstractComponent<Application> {
2020
*/
2121
load(): boolean {
2222
const logger = this.application.logger;
23-
const plugins = this.plugins.length ? this.plugins : this.discoverNpmPlugins();
23+
const plugins = this.plugins.length ? this.resolvePluginPaths(this.plugins) : this.discoverNpmPlugins();
2424

2525
if (plugins.some(plugin => plugin.toLowerCase() === 'none')) {
2626
return true;
@@ -133,4 +133,29 @@ export class PluginHost extends AbstractComponent<Application> {
133133
return false;
134134
}
135135
}
136+
137+
/**
138+
* Resolves plugin paths to absolute paths from the current working directory
139+
* (`process.cwd()`).
140+
*
141+
* ```txt
142+
* ./plugin -> resolve
143+
* ../plugin -> resolve
144+
* plugin -> don't resolve (module resolution)
145+
* /plugin -> don't resolve (already absolute path)
146+
* c:\plugin -> don't resolve (already absolute path)
147+
* ```
148+
*
149+
* @param plugins
150+
*/
151+
private resolvePluginPaths(plugins: string[]) {
152+
const cwd = process.cwd();
153+
return plugins.map(plugin => {
154+
// treat plugins that start with `.` as relative, requiring resolution
155+
if (plugin.startsWith('.')) {
156+
return Path.resolve(cwd, plugin);
157+
}
158+
return plugin;
159+
});
160+
}
136161
}

src/test/plugin-host.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Application } from '..';
22
import Assert = require('assert');
33
import * as mockery from 'mockery';
4+
import * as path from 'path';
45

56
describe('PluginHost', function () {
67
before (function () {
@@ -27,4 +28,27 @@ describe('PluginHost', function () {
2728
'typedoc-plugin-2'
2829
]);
2930
});
31+
32+
it('loads a plugin with relative path', function () {
33+
const app = new Application();
34+
app.bootstrap({
35+
plugin: ['./dist/test/plugins/relative']
36+
});
37+
38+
Assert.deepEqual(app.plugins.plugins, [
39+
'./dist/test/plugins/relative'
40+
]);
41+
});
42+
43+
it('loads a plugin with absolute path', function () {
44+
const app = new Application();
45+
const absolutePath = path.resolve(__dirname, './plugins/absolute');
46+
app.bootstrap({
47+
plugin: [absolutePath]
48+
});
49+
50+
Assert.deepEqual(app.plugins.plugins, [
51+
absolutePath
52+
]);
53+
});
3054
});

src/test/plugins/absolute.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { Application } from '../..';
2+
3+
module.exports = (pluginHost: Application) => {
4+
};

src/test/plugins/relative.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { Application } from '../..';
2+
3+
module.exports = (pluginHost: Application) => {
4+
};

0 commit comments

Comments
 (0)