mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-06 04:42:40 +02:00
fix(v2): Resolve plugins relative to siteDir (#2789)
This commit is contained in:
parent
a7925f28be
commit
faf48e9b16
1 changed files with 12 additions and 2 deletions
|
@ -5,6 +5,7 @@
|
||||||
* LICENSE file in the root directory of this source tree.
|
* LICENSE file in the root directory of this source tree.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import Module from 'module';
|
||||||
import importFresh from 'import-fresh';
|
import importFresh from 'import-fresh';
|
||||||
import {LoadContext, Plugin, PluginConfig} from '@docusaurus/types';
|
import {LoadContext, Plugin, PluginConfig} from '@docusaurus/types';
|
||||||
|
|
||||||
|
@ -15,9 +16,16 @@ export function initPlugins({
|
||||||
pluginConfigs: PluginConfig[];
|
pluginConfigs: PluginConfig[];
|
||||||
context: LoadContext;
|
context: LoadContext;
|
||||||
}): Plugin<any>[] {
|
}): Plugin<any>[] {
|
||||||
|
// We need to resolve plugins from the perspective of the siteDir, since the siteDir's package.json
|
||||||
|
// declares the dependency on these plugins.
|
||||||
|
// We need to fallback to createRequireFromPath since createRequire is only available in node v12.
|
||||||
|
// See: https://nodejs.org/api/modules.html#modules_module_createrequire_filename
|
||||||
|
const createRequire = Module.createRequire || Module.createRequireFromPath;
|
||||||
|
const pluginRequire = createRequire(context.siteDir);
|
||||||
|
|
||||||
const plugins: Plugin<any>[] = pluginConfigs
|
const plugins: Plugin<any>[] = pluginConfigs
|
||||||
.map((pluginItem) => {
|
.map((pluginItem) => {
|
||||||
let pluginModuleImport;
|
let pluginModuleImport: string | undefined;
|
||||||
let pluginOptions = {};
|
let pluginOptions = {};
|
||||||
|
|
||||||
if (!pluginItem) {
|
if (!pluginItem) {
|
||||||
|
@ -37,7 +45,9 @@ export function initPlugins({
|
||||||
|
|
||||||
// The pluginModuleImport value is any valid
|
// The pluginModuleImport value is any valid
|
||||||
// module identifier - npm package or locally-resolved path.
|
// module identifier - npm package or locally-resolved path.
|
||||||
const pluginModule: any = importFresh(pluginModuleImport);
|
const pluginModule: any = importFresh(
|
||||||
|
pluginRequire.resolve(pluginModuleImport),
|
||||||
|
);
|
||||||
return (pluginModule.default || pluginModule)(context, pluginOptions);
|
return (pluginModule.default || pluginModule)(context, pluginOptions);
|
||||||
})
|
})
|
||||||
.filter(Boolean);
|
.filter(Boolean);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue