forked from janus-idp/backstage-showcase
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix : fix a bug in the upstream CommonJSModuleLoader (janus-idp#600)
* fix : fix a bug in the upstream CommonJSLoader that prevented laoding modules from embedded `node_modules` folders of private packages located in the plugin `node_modules` folder. Signed-off-by: David Festal <dfestal@redhat.com> * Add a comment, as requested in review. Signed-off-by: David Festal <dfestal@redhat.com> --------- Signed-off-by: David Festal <dfestal@redhat.com>
- Loading branch information
1 parent
80376b4
commit 0961437
Showing
3 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'backend': patch | ||
--- | ||
|
||
Fix a bug in the upstream CommonJSLoader, which prevented laoding modules from embedded node_modules folders of private packages located in the plugin node_modules folder. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright 2023 The Backstage Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { ModuleLoader } from '@backstage/backend-plugin-manager'; | ||
|
||
import path from 'path'; | ||
import { Logger } from 'winston'; | ||
|
||
export class CommonJSModuleLoader implements ModuleLoader { | ||
constructor(public readonly logger: Logger) {} | ||
|
||
async bootstrap( | ||
backstageRoot: string, | ||
dynamicPluginsPaths: string[], | ||
): Promise<void> { | ||
const backstageRootNodeModulesPath = `${backstageRoot}/node_modules`; | ||
const dynamicNodeModulesPaths = [ | ||
...dynamicPluginsPaths.map(p => path.resolve(p, 'node_modules')), | ||
]; | ||
const Module = require('module'); | ||
const oldNodeModulePaths = Module._nodeModulePaths; | ||
Module._nodeModulePaths = (from: string): string[] => { | ||
const result: string[] = oldNodeModulePaths(from); | ||
if (!dynamicPluginsPaths.some(p => from.startsWith(p))) { | ||
return result; | ||
} | ||
const filtered = result.filter(nodeModulePath => { | ||
return ( | ||
nodeModulePath === backstageRootNodeModulesPath || | ||
dynamicNodeModulesPaths.some(p => nodeModulePath.startsWith(p)) | ||
); | ||
}); | ||
this.logger.debug( | ||
`Overriding node_modules search path for dynamic plugin ${from} to: ${filtered}`, | ||
); | ||
return filtered; | ||
}; | ||
} | ||
|
||
async load(packagePath: string): Promise<any> { | ||
return await import(/* webpackIgnore: true */ packagePath); | ||
} | ||
} |