Skip to content

Commit 070c34b

Browse files
authored
Merge pull request #6261 from mnapoli/import-relative-file
#6017 Allow to load plugin from path
2 parents 5b3fe25 + 1019939 commit 070c34b

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

docs/providers/aws/guide/plugins.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ custom:
6363

6464
## Service local plugin
6565

66-
If you are working on a plugin or have a plugin that is just designed for one project they can be loaded from the local folder. Local plugins can be added in the `plugins` array in `serverless.yml`.
67-
68-
By default local plugins can be added to the `.serverless_plugins` directory at the root of your service, and in the `plugins` array in `serverless.yml`.
66+
If you are working on a plugin or have a plugin that is just designed for one project, it can be loaded from the local `.serverless_plugins` folder at the root of your service. Local plugins can be added in the `plugins` array in `serverless.yml`.
6967
```yml
7068
plugins:
7169
- custom-serverless-plugin
@@ -78,10 +76,19 @@ plugins:
7876
modules:
7977
- custom-serverless-plugin
8078
```
81-
The `custom-serverless-plugin` will be loaded from the `custom_serverless_plugins` directory at the root of your service. If the `localPath` is not provided or empty `.serverless_plugins` directory will be taken as the `localPath`.
79+
The `custom-serverless-plugin` will be loaded from the `custom_serverless_plugins` directory at the root of your service. If the `localPath` is not provided or empty, the `.serverless_plugins` directory will be used.
8280

8381
The plugin will be loaded based on being named `custom-serverless-plugin.js` or `custom-serverless-plugin\index.js` in the root of `localPath` folder (`.serverless_plugins` by default).
8482

83+
If you want to load a plugin from a specific directory without affecting other plugins, you can also specify a path relative to the root of your service:
84+
```yaml
85+
plugins:
86+
# This plugin will be loaded from the `.serverless_plugins/` or `node_modules/` directories
87+
- custom-serverless-plugin
88+
# This plugin will be loaded from the `sub/directory/` directory
89+
- ./sub/directory/another-custom-plugin
90+
```
91+
8592
### Load Order
8693
8794
Keep in mind that the order you define your plugins matters. When Serverless loads all the core plugins and then the custom plugins in the order you've defined them.

lib/classes/PluginManager.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ class PluginManager {
100100
loadPlugins(plugins) {
101101
plugins.forEach((plugin) => {
102102
try {
103-
const Plugin = require(plugin); // eslint-disable-line global-require
103+
const servicePath = this.serverless.config.servicePath;
104+
const pluginPath = plugin.startsWith('./') ? path.join(servicePath, plugin) : plugin;
105+
const Plugin = require(pluginPath); // eslint-disable-line global-require
104106

105107
this.addPlugin(Plugin);
106108
} catch (error) {

lib/classes/PluginManager.test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -803,11 +803,13 @@ describe('PluginManager', () => {
803803
describe('#loadServicePlugins()', () => {
804804
beforeEach(function () { // eslint-disable-line prefer-arrow-callback
805805
mockRequire('ServicePluginMock1', ServicePluginMock1);
806-
mockRequire('ServicePluginMock2', ServicePluginMock2);
806+
// Plugins loaded via a relative path should be required relative to the service path
807+
const servicePath = pluginManager.serverless.config.servicePath;
808+
mockRequire(`${servicePath}/RelativePath/ServicePluginMock2`, ServicePluginMock2);
807809
});
808810

809811
it('should load the service plugins', () => {
810-
const servicePlugins = ['ServicePluginMock1', 'ServicePluginMock2'];
812+
const servicePlugins = ['ServicePluginMock1', './RelativePath/ServicePluginMock2'];
811813
pluginManager.loadServicePlugins(servicePlugins);
812814

813815
expect(pluginManager.plugins

0 commit comments

Comments
 (0)