From 68aaf9201f1bdff94c970c43d4ebc90761b84fdb Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Wed, 16 Mar 2022 20:47:15 +0800 Subject: [PATCH] feat(core): allow plugin lifecycles to return relative paths (#6921) * feat(core): resolve plugin lifecycles returning relative paths * fix typo * fix tests * revert * rename path -> entryPath --- packages/docusaurus-plugin-debug/src/index.ts | 4 +-- .../src/index.ts | 3 +- .../src/index.ts | 3 +- .../src/index.ts | 6 ++-- packages/docusaurus-plugin-pwa/src/index.ts | 6 ++-- packages/docusaurus-plugin-pwa/src/options.ts | 3 +- .../docusaurus-theme-classic/src/index.ts | 9 +++-- .../src/index.ts | 7 ++-- .../src/index.ts | 4 +-- packages/docusaurus-types/src/index.d.ts | 4 +++ .../src/commands/swizzle/context.ts | 4 +-- .../docusaurus/src/commands/swizzle/themes.ts | 13 +++++-- .../__tests__/__fixtures__/plugin-empty.js | 1 + .../__tests__/__fixtures__/plugin-foo-bar.js | 1 + .../__fixtures__/plugin-hello-world.js | 1 + .../client-modules/__tests__/index.test.ts | 36 +++++++++---------- .../src/server/client-modules/index.ts | 11 ++++-- packages/docusaurus/src/server/index.ts | 7 +++- .../__snapshots__/index.test.ts.snap | 2 ++ .../docusaurus/src/server/plugins/init.ts | 20 ++++++++--- .../docusaurus/src/server/themes/index.ts | 5 ++- .../translations/translationsExtractor.ts | 2 +- website/_dogfooding/dogfooding.config.js | 4 +-- .../plugin-methods/extend-infrastructure.md | 8 ++--- website/docusaurus.config.js | 2 +- website/src/plugins/changelog/index.js | 2 +- 26 files changed, 100 insertions(+), 68 deletions(-) diff --git a/packages/docusaurus-plugin-debug/src/index.ts b/packages/docusaurus-plugin-debug/src/index.ts index 0094918b4c4d..01a9e0a4e03f 100644 --- a/packages/docusaurus-plugin-debug/src/index.ts +++ b/packages/docusaurus-plugin-debug/src/index.ts @@ -24,10 +24,10 @@ export default function pluginDebug({ name: 'docusaurus-plugin-debug', getThemePath() { - return path.resolve(__dirname, '../lib/theme'); + return '../lib/theme'; }, getTypeScriptThemePath() { - return path.resolve(__dirname, '../src/theme'); + return '../src/theme'; }, async contentLoaded({actions: {createData, addRoute}, allContent}) { diff --git a/packages/docusaurus-plugin-google-analytics/src/index.ts b/packages/docusaurus-plugin-google-analytics/src/index.ts index 3abe326da3ce..5e75e58b9929 100644 --- a/packages/docusaurus-plugin-google-analytics/src/index.ts +++ b/packages/docusaurus-plugin-google-analytics/src/index.ts @@ -5,7 +5,6 @@ * LICENSE file in the root directory of this source tree. */ -import path from 'path'; import {Joi} from '@docusaurus/utils-validation'; import type { LoadContext, @@ -28,7 +27,7 @@ export default function pluginGoogleAnalytics( name: 'docusaurus-plugin-google-analytics', getClientModules() { - return isProd ? [path.resolve(__dirname, './analytics')] : []; + return isProd ? ['./analytics'] : []; }, injectHtmlTags() { diff --git a/packages/docusaurus-plugin-google-gtag/src/index.ts b/packages/docusaurus-plugin-google-gtag/src/index.ts index ad66bca06929..238fe699a69a 100644 --- a/packages/docusaurus-plugin-google-gtag/src/index.ts +++ b/packages/docusaurus-plugin-google-gtag/src/index.ts @@ -5,7 +5,6 @@ * LICENSE file in the root directory of this source tree. */ -import path from 'path'; import {Joi} from '@docusaurus/utils-validation'; import type { LoadContext, @@ -32,7 +31,7 @@ export default function pluginGoogleGtag( }, getClientModules() { - return isProd ? [path.resolve(__dirname, './gtag')] : []; + return isProd ? ['./gtag'] : []; }, injectHtmlTags() { diff --git a/packages/docusaurus-plugin-ideal-image/src/index.ts b/packages/docusaurus-plugin-ideal-image/src/index.ts index 1630f46e597d..de28f087dfb3 100644 --- a/packages/docusaurus-plugin-ideal-image/src/index.ts +++ b/packages/docusaurus-plugin-ideal-image/src/index.ts @@ -15,8 +15,6 @@ import type {PluginOptions} from '@docusaurus/plugin-ideal-image'; import {Joi} from '@docusaurus/utils-validation'; import {readDefaultCodeTranslationMessages} from '@docusaurus/theme-translations'; -import path from 'path'; - export default function pluginIdealImage( context: LoadContext, options: PluginOptions, @@ -29,11 +27,11 @@ export default function pluginIdealImage( name: 'docusaurus-plugin-ideal-image', getThemePath() { - return path.resolve(__dirname, '../lib/theme'); + return '../lib/theme'; }, getTypeScriptThemePath() { - return path.resolve(__dirname, '../src/theme'); + return '../src/theme'; }, getDefaultCodeTranslationMessages() { diff --git a/packages/docusaurus-plugin-pwa/src/index.ts b/packages/docusaurus-plugin-pwa/src/index.ts index 368fc5e9cb11..e43af904e070 100644 --- a/packages/docusaurus-plugin-pwa/src/index.ts +++ b/packages/docusaurus-plugin-pwa/src/index.ts @@ -64,10 +64,10 @@ export default function pluginPWA( name: 'docusaurus-plugin-pwa', getThemePath() { - return path.resolve(__dirname, '../lib/theme'); + return '../lib/theme'; }, getTypeScriptThemePath() { - return path.resolve(__dirname, '../src/theme'); + return '../src/theme'; }, getClientModules() { @@ -138,7 +138,7 @@ export default function pluginPWA( const swSourceFileTest = /\.m?js$/; const swWebpackConfig: Configuration = { - entry: path.resolve(__dirname, 'sw.js'), + entry: require.resolve('./sw.js'), output: { path: outDir, filename: 'sw.js', diff --git a/packages/docusaurus-plugin-pwa/src/options.ts b/packages/docusaurus-plugin-pwa/src/options.ts index ae5242b0b6c5..418313fafb4f 100644 --- a/packages/docusaurus-plugin-pwa/src/options.ts +++ b/packages/docusaurus-plugin-pwa/src/options.ts @@ -5,7 +5,6 @@ * LICENSE file in the root directory of this source tree. */ -import path from 'path'; import {Joi} from '@docusaurus/utils-validation'; import type { ThemeConfig, @@ -24,7 +23,7 @@ const DEFAULT_OPTIONS = { injectManifestConfig: {}, pwaHead: [], swCustom: undefined, - swRegister: path.join(__dirname, 'registerSw.js'), + swRegister: './registerSw.js', reloadPopup: '@theme/PwaReloadPopup', }; diff --git a/packages/docusaurus-theme-classic/src/index.ts b/packages/docusaurus-theme-classic/src/index.ts index d5066c5d4f9a..a3904f3582a5 100644 --- a/packages/docusaurus-theme-classic/src/index.ts +++ b/packages/docusaurus-theme-classic/src/index.ts @@ -8,7 +8,6 @@ import type {LoadContext, Plugin, PostCssOptions} from '@docusaurus/types'; import type {ThemeConfig} from '@docusaurus/theme-common'; import {getTranslationFiles, translateThemeConfig} from './translations'; -import path from 'path'; import {createRequire} from 'module'; import type {Plugin as PostCssPlugin} from 'postcss'; import rtlcss from 'rtlcss'; @@ -112,11 +111,11 @@ export default function docusaurusThemeClassic( name: 'docusaurus-theme-classic', getThemePath() { - return path.join(__dirname, '../lib-next/theme'); + return '../lib-next/theme'; }, getTypeScriptThemePath() { - return path.resolve(__dirname, '../src/theme'); + return '../src/theme'; }, getTranslationFiles: async () => getTranslationFiles({themeConfig}), @@ -137,8 +136,8 @@ export default function docusaurusThemeClassic( getClientModules() { const modules = [ require.resolve(getInfimaCSSFile(direction)), - path.resolve(__dirname, './prism-include-languages'), - path.resolve(__dirname, './admonitions.css'), + './prism-include-languages', + './admonitions.css', ]; if (customCss) { diff --git a/packages/docusaurus-theme-live-codeblock/src/index.ts b/packages/docusaurus-theme-live-codeblock/src/index.ts index 67d239b6852e..0f02878c50c0 100644 --- a/packages/docusaurus-theme-live-codeblock/src/index.ts +++ b/packages/docusaurus-theme-live-codeblock/src/index.ts @@ -5,7 +5,6 @@ * LICENSE file in the root directory of this source tree. */ -import path from 'path'; import {readDefaultCodeTranslationMessages} from '@docusaurus/theme-translations'; import type {LoadContext, Plugin} from '@docusaurus/types'; @@ -18,10 +17,10 @@ export default function themeLiveCodeblock(context: LoadContext): Plugin { name: 'docusaurus-theme-live-codeblock', getThemePath() { - return path.resolve(__dirname, '../lib/theme'); + return '../lib/theme'; }, getTypeScriptThemePath() { - return path.resolve(__dirname, '../src/theme'); + return '../src/theme'; }, getDefaultCodeTranslationMessages() { @@ -35,7 +34,7 @@ export default function themeLiveCodeblock(context: LoadContext): Plugin { return { resolve: { alias: { - buble: path.resolve(__dirname, './custom-buble.js'), + buble: require.resolve('./custom-buble.js'), }, }, }; diff --git a/packages/docusaurus-theme-search-algolia/src/index.ts b/packages/docusaurus-theme-search-algolia/src/index.ts index 6c41d564f8c9..46f80161652c 100644 --- a/packages/docusaurus-theme-search-algolia/src/index.ts +++ b/packages/docusaurus-theme-search-algolia/src/index.ts @@ -47,10 +47,10 @@ export default function themeSearchAlgolia(context: LoadContext): Plugin { name: 'docusaurus-theme-search-algolia', getThemePath() { - return path.resolve(__dirname, '../lib/theme'); + return '../lib/theme'; }, getTypeScriptThemePath() { - return path.resolve(__dirname, '../src/theme'); + return '../src/theme'; }, getDefaultCodeTranslationMessages() { diff --git a/packages/docusaurus-types/src/index.d.ts b/packages/docusaurus-types/src/index.d.ts index f29f4d521dcb..cead1c6ed2d6 100644 --- a/packages/docusaurus-types/src/index.d.ts +++ b/packages/docusaurus-types/src/index.d.ts @@ -290,6 +290,10 @@ export interface Plugin { export type InitializedPlugin = Plugin & { readonly options: Required; readonly version: DocusaurusPluginVersionInformation; + /** + * The absolute path to the folder containing the entry point file. + */ + readonly path: string; }; export type LoadedPlugin = InitializedPlugin & { diff --git a/packages/docusaurus/src/commands/swizzle/context.ts b/packages/docusaurus/src/commands/swizzle/context.ts index f4ca176f09e0..7c06ef00c7f2 100644 --- a/packages/docusaurus/src/commands/swizzle/context.ts +++ b/packages/docusaurus/src/commands/swizzle/context.ts @@ -5,7 +5,6 @@ * LICENSE file in the root directory of this source tree. */ -import {createRequire} from 'module'; import {loadContext, loadPluginConfigs} from '../../server'; import initPlugins, {normalizePluginConfigs} from '../../server/plugins/init'; import type {InitializedPlugin} from '@docusaurus/types'; @@ -15,7 +14,6 @@ export async function initSwizzleContext( siteDir: string, ): Promise { const context = await loadContext(siteDir); - const pluginRequire = createRequire(context.siteConfigPath); const pluginConfigs = await loadPluginConfigs(context); const plugins: InitializedPlugin[] = await initPlugins({ @@ -25,7 +23,7 @@ export async function initSwizzleContext( const pluginsNormalized = await normalizePluginConfigs( pluginConfigs, - pluginRequire, + context.siteConfigPath, ); return { diff --git a/packages/docusaurus/src/commands/swizzle/themes.ts b/packages/docusaurus/src/commands/swizzle/themes.ts index ead1fc69994c..49381089dd18 100644 --- a/packages/docusaurus/src/commands/swizzle/themes.ts +++ b/packages/docusaurus/src/commands/swizzle/themes.ts @@ -6,6 +6,7 @@ */ import logger from '@docusaurus/logger'; +import path from 'path'; import leven from 'leven'; import _ from 'lodash'; import {askThemeName} from './prompts'; @@ -132,8 +133,16 @@ export function getThemePath({ }): string { const pluginInstance = getPluginByThemeName(plugins, themeName); const themePath = typescript - ? pluginInstance.instance.getTypeScriptThemePath?.() - : pluginInstance.instance.getThemePath?.(); + ? pluginInstance.instance.getTypeScriptThemePath && + path.resolve( + pluginInstance.instance.path, + pluginInstance.instance.getTypeScriptThemePath(), + ) + : pluginInstance.instance.getThemePath && + path.resolve( + pluginInstance.instance.path, + pluginInstance.instance.getThemePath(), + ); if (!themePath) { logger.warn( typescript diff --git a/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/plugin-empty.js b/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/plugin-empty.js index 6b8398c7d40e..def0cfe25df1 100644 --- a/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/plugin-empty.js +++ b/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/plugin-empty.js @@ -8,5 +8,6 @@ module.exports = function() { return { name: 'plugin-empty', + path: __dirname, }; }; diff --git a/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/plugin-foo-bar.js b/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/plugin-foo-bar.js index 52d26e15a638..94276570052e 100644 --- a/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/plugin-foo-bar.js +++ b/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/plugin-foo-bar.js @@ -8,6 +8,7 @@ module.exports = function() { return { name: 'plugin-foo-bar', + path: __dirname, getClientModules() { return ['foo', 'bar']; }, diff --git a/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/plugin-hello-world.js b/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/plugin-hello-world.js index 9fa02b19e68e..3047719c6d9e 100644 --- a/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/plugin-hello-world.js +++ b/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/plugin-hello-world.js @@ -8,6 +8,7 @@ module.exports = function() { return { plugin: 'plugin-hello-world', + path: __dirname, getClientModules() { return ['hello', 'world']; }, diff --git a/packages/docusaurus/src/server/client-modules/__tests__/index.test.ts b/packages/docusaurus/src/server/client-modules/__tests__/index.test.ts index 46744e2d3aa8..1eb43c28b933 100644 --- a/packages/docusaurus/src/server/client-modules/__tests__/index.test.ts +++ b/packages/docusaurus/src/server/client-modules/__tests__/index.test.ts @@ -21,8 +21,8 @@ describe('loadClientModules', () => { const clientModules = loadClientModules([pluginFooBar()]); expect(clientModules).toMatchInlineSnapshot(` [ - "foo", - "bar", + "/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/foo", + "/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/bar", ] `); }); @@ -34,10 +34,10 @@ describe('loadClientModules', () => { ]); expect(clientModules).toMatchInlineSnapshot(` [ - "foo", - "bar", - "hello", - "world", + "/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/foo", + "/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/bar", + "/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/hello", + "/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/world", ] `); }); @@ -49,10 +49,10 @@ describe('loadClientModules', () => { ]); expect(clientModules).toMatchInlineSnapshot(` [ - "hello", - "world", - "foo", - "bar", + "/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/hello", + "/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/world", + "/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/foo", + "/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/bar", ] `); }); @@ -65,10 +65,10 @@ describe('loadClientModules', () => { ]); expect(clientModules).toMatchInlineSnapshot(` [ - "hello", - "world", - "foo", - "bar", + "/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/hello", + "/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/world", + "/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/foo", + "/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/bar", ] `); }); @@ -81,10 +81,10 @@ describe('loadClientModules', () => { ]); expect(clientModules).toMatchInlineSnapshot(` [ - "hello", - "world", - "foo", - "bar", + "/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/hello", + "/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/world", + "/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/foo", + "/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/bar", ] `); }); diff --git a/packages/docusaurus/src/server/client-modules/index.ts b/packages/docusaurus/src/server/client-modules/index.ts index 68080ee72c9b..6a51d751a5e6 100644 --- a/packages/docusaurus/src/server/client-modules/index.ts +++ b/packages/docusaurus/src/server/client-modules/index.ts @@ -5,10 +5,15 @@ * LICENSE file in the root directory of this source tree. */ -import type {Plugin} from '@docusaurus/types'; +import path from 'path'; +import type {LoadedPlugin} from '@docusaurus/types'; export default function loadClientModules( - plugins: Plugin[], + plugins: LoadedPlugin[], ): string[] { - return plugins.flatMap((plugin) => plugin.getClientModules?.() ?? []); + return plugins.flatMap( + (plugin) => + plugin.getClientModules?.().map((p) => path.resolve(plugin.path, p)) ?? + [], + ); } diff --git a/packages/docusaurus/src/server/index.ts b/packages/docusaurus/src/server/index.ts index b58aa1c54b0a..678c5823c6af 100644 --- a/packages/docusaurus/src/server/index.ts +++ b/packages/docusaurus/src/server/index.ts @@ -176,8 +176,10 @@ export async function loadPluginConfigs( // - Resolve aliased theme components // - Inject scripts/stylesheets function createBootstrapPlugin({ + siteDir, siteConfig, }: { + siteDir: string; siteConfig: DocusaurusConfig; }): LoadedPlugin { const { @@ -192,6 +194,7 @@ function createBootstrapPlugin({ id: 'default', }, version: {type: 'synthetic'}, + path: siteDir, getClientModules() { return siteConfigClientModules; }, @@ -244,6 +247,8 @@ function createMDXFallbackPlugin({ id: 'default', }, version: {type: 'synthetic'}, + // Synthetic, the path doesn't matter much + path: '.', configureWebpack(config, isServer, {getJSLoader}) { // We need the mdx fallback loader to exclude files that were already // processed by content plugins mdx loaders. This works, but a bit @@ -336,7 +341,7 @@ export default ${JSON.stringify(siteConfig, null, 2)}; `, ); - plugins.push(createBootstrapPlugin({siteConfig})); + plugins.push(createBootstrapPlugin({siteDir, siteConfig})); plugins.push(createMDXFallbackPlugin({siteDir, siteConfig})); // Load client modules. diff --git a/packages/docusaurus/src/server/plugins/__tests__/__snapshots__/index.test.ts.snap b/packages/docusaurus/src/server/plugins/__tests__/__snapshots__/index.test.ts.snap index 57398999df4f..7435ca95c597 100644 --- a/packages/docusaurus/src/server/plugins/__tests__/__snapshots__/index.test.ts.snap +++ b/packages/docusaurus/src/server/plugins/__tests__/__snapshots__/index.test.ts.snap @@ -19,6 +19,7 @@ exports[`loadPlugins loads plugins 1`] = ` "options": { "id": "default", }, + "path": "/packages/docusaurus/src/server/plugins/__tests__/__fixtures__/site-with-plugin", "prop": "a", "version": { "type": "local", @@ -31,6 +32,7 @@ exports[`loadPlugins loads plugins 1`] = ` "options": { "id": "default", }, + "path": "/packages/docusaurus/src/server/plugins/__tests__/__fixtures__/site-with-plugin", "version": { "type": "local", }, diff --git a/packages/docusaurus/src/server/plugins/init.ts b/packages/docusaurus/src/server/plugins/init.ts index 6e4a600386f3..504071617a07 100644 --- a/packages/docusaurus/src/server/plugins/init.ts +++ b/packages/docusaurus/src/server/plugins/init.ts @@ -6,6 +6,7 @@ */ import {createRequire} from 'module'; +import path from 'path'; import importFresh from 'import-fresh'; import type { DocusaurusPluginVersionInformation, @@ -32,12 +33,18 @@ export type NormalizedPluginConfig = { path: string; module: ImportedPluginModule; }; + /** + * Different from pluginModule.path, this one is always an absolute path used + * to resolve relative paths returned from lifecycles + */ + entryPath: string; }; async function normalizePluginConfig( pluginConfig: PluginConfig, - pluginRequire: NodeRequire, + configPath: string, ): Promise { + const pluginRequire = createRequire(configPath); // plugins: ['./plugin'] if (typeof pluginConfig === 'string') { const pluginModuleImport = pluginConfig; @@ -50,6 +57,7 @@ async function normalizePluginConfig( path: pluginModuleImport, module: pluginModule, }, + entryPath: pluginPath, }; } @@ -58,6 +66,7 @@ async function normalizePluginConfig( return { plugin: pluginConfig, options: {}, + entryPath: configPath, }; } @@ -75,6 +84,7 @@ async function normalizePluginConfig( path: pluginModuleImport, module: pluginModule, }, + entryPath: pluginPath, }; } // plugins: [ @@ -83,16 +93,17 @@ async function normalizePluginConfig( return { plugin: pluginConfig[0], options: pluginConfig[1], + entryPath: configPath, }; } export async function normalizePluginConfigs( pluginConfigs: PluginConfig[], - pluginRequire: NodeRequire, + configPath: string, ): Promise { return Promise.all( pluginConfigs.map((pluginConfig) => - normalizePluginConfig(pluginConfig, pluginRequire), + normalizePluginConfig(pluginConfig, configPath), ), ); } @@ -135,7 +146,7 @@ export default async function initPlugins({ const pluginRequire = createRequire(context.siteConfigPath); const pluginConfigsNormalized = await normalizePluginConfigs( pluginConfigs, - pluginRequire, + context.siteConfigPath, ); async function doGetPluginVersion( @@ -206,6 +217,7 @@ export default async function initPlugins({ ...pluginInstance, options: pluginOptions, version: pluginVersion, + path: path.dirname(normalizedPluginConfig.entryPath), }; } diff --git a/packages/docusaurus/src/server/themes/index.ts b/packages/docusaurus/src/server/themes/index.ts index b90ec7c497bb..071acd007a40 100644 --- a/packages/docusaurus/src/server/themes/index.ts +++ b/packages/docusaurus/src/server/themes/index.ts @@ -51,7 +51,10 @@ export function loadPluginsThemeAliases({ plugins: LoadedPlugin[]; }): Promise { const pluginThemes: string[] = plugins - .map((plugin) => plugin.getThemePath?.()) + .map( + (plugin) => + plugin.getThemePath && path.resolve(plugin.path, plugin.getThemePath()), + ) .filter((x): x is string => Boolean(x)); const userTheme = path.resolve(siteDir, THEME_PATH); return loadThemeAliases([ThemeFallbackDir, ...pluginThemes], [userTheme]); diff --git a/packages/docusaurus/src/server/translations/translationsExtractor.ts b/packages/docusaurus/src/server/translations/translationsExtractor.ts index d626645de784..d68b1a3f8863 100644 --- a/packages/docusaurus/src/server/translations/translationsExtractor.ts +++ b/packages/docusaurus/src/server/translations/translationsExtractor.ts @@ -56,7 +56,7 @@ function getPluginSourceCodeFilePaths(plugin: InitializedPlugin): string[] { codePaths.push(themePath); } - return codePaths; + return codePaths.map((p) => nodePath.resolve(plugin.path, p)); } export async function globSourceCodeFilePaths( diff --git a/website/_dogfooding/dogfooding.config.js b/website/_dogfooding/dogfooding.config.js index 5b58c9aaa91d..5d7cea7670ee 100644 --- a/website/_dogfooding/dogfooding.config.js +++ b/website/_dogfooding/dogfooding.config.js @@ -6,7 +6,6 @@ */ const fs = require('fs'); -const path = require('path'); /** @type {import('@docusaurus/types').PluginConfig[]} */ const dogfoodingThemeInstances = [ @@ -14,8 +13,7 @@ const dogfoodingThemeInstances = [ function swizzleThemeTests() { return { name: 'swizzle-theme-tests', - getThemePath: () => - path.join(__dirname, '_swizzle_theme_tests/src/theme'), + getThemePath: () => './_swizzle_theme_tests/src/theme', }; }, ]; diff --git a/website/docs/api/plugin-methods/extend-infrastructure.md b/website/docs/api/plugin-methods/extend-infrastructure.md index f4a5bc89db3a..1885129b7007 100644 --- a/website/docs/api/plugin-methods/extend-infrastructure.md +++ b/website/docs/api/plugin-methods/extend-infrastructure.md @@ -61,7 +61,7 @@ module.exports = function (context, options) { ## `getThemePath()` {#getThemePath} -Returns the path to the directory where the theme components can be found. When your users call `swizzle`, `getThemePath` is called and its returned path is used to find your theme components. +Returns the path to the directory where the theme components can be found. When your users call `swizzle`, `getThemePath` is called and its returned path is used to find your theme components. Relative paths are resolved against the folder containing the entry point. For example, your `getThemePath` can be: @@ -73,7 +73,7 @@ module.exports = function (context, options) { name: 'my-theme', // highlight-start getThemePath() { - return path.resolve(__dirname, './theme'); + return './theme'; }, // highlight-end }; @@ -103,11 +103,11 @@ module.exports = function (context, options) { // highlight-start getThemePath() { // Where compiled JavaScript output lives - return path.join(__dirname, '../lib/theme'); + return '../lib/theme'; }, getTypeScriptThemePath() { // Where TypeScript source code lives - return path.resolve(__dirname, '../src/theme'); + return '../src/theme'; }, // highlight-end }; diff --git a/website/docusaurus.config.js b/website/docusaurus.config.js index 437a83bbb403..7d029dbe2082 100644 --- a/website/docusaurus.config.js +++ b/website/docusaurus.config.js @@ -205,7 +205,7 @@ const config = { 'queryString', ], // swRegister: false, - swCustom: path.resolve(__dirname, 'src/sw.js'), + swCustom: require.resolve('./src/sw.js'), pwaHead: [ { tagName: 'link', diff --git a/website/src/plugins/changelog/index.js b/website/src/plugins/changelog/index.js index 310b1ebc3647..c6116d0c5851 100644 --- a/website/src/plugins/changelog/index.js +++ b/website/src/plugins/changelog/index.js @@ -146,7 +146,7 @@ async function ChangelogPlugin(context, options) { return config; }, getThemePath() { - return path.join(__dirname, './theme'); + return './theme'; }, getPathsToWatch() { // Don't watch the generated dir