Skip to content

Commit

Permalink
feat(core): allow plugin lifecycles to return relative paths (#6921)
Browse files Browse the repository at this point in the history
* feat(core): resolve plugin lifecycles returning relative paths

* fix typo

* fix tests

* revert

* rename path -> entryPath
  • Loading branch information
Josh-Cena authored Mar 16, 2022
1 parent 8d1c195 commit 68aaf92
Show file tree
Hide file tree
Showing 26 changed files with 100 additions and 68 deletions.
4 changes: 2 additions & 2 deletions packages/docusaurus-plugin-debug/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}) {
Expand Down
3 changes: 1 addition & 2 deletions packages/docusaurus-plugin-google-analytics/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -28,7 +27,7 @@ export default function pluginGoogleAnalytics(
name: 'docusaurus-plugin-google-analytics',

getClientModules() {
return isProd ? [path.resolve(__dirname, './analytics')] : [];
return isProd ? ['./analytics'] : [];
},

injectHtmlTags() {
Expand Down
3 changes: 1 addition & 2 deletions packages/docusaurus-plugin-google-gtag/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -32,7 +31,7 @@ export default function pluginGoogleGtag(
},

getClientModules() {
return isProd ? [path.resolve(__dirname, './gtag')] : [];
return isProd ? ['./gtag'] : [];
},

injectHtmlTags() {
Expand Down
6 changes: 2 additions & 4 deletions packages/docusaurus-plugin-ideal-image/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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() {
Expand Down
6 changes: 3 additions & 3 deletions packages/docusaurus-plugin-pwa/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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',
Expand Down
3 changes: 1 addition & 2 deletions packages/docusaurus-plugin-pwa/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -24,7 +23,7 @@ const DEFAULT_OPTIONS = {
injectManifestConfig: {},
pwaHead: [],
swCustom: undefined,
swRegister: path.join(__dirname, 'registerSw.js'),
swRegister: './registerSw.js',
reloadPopup: '@theme/PwaReloadPopup',
};

Expand Down
9 changes: 4 additions & 5 deletions packages/docusaurus-theme-classic/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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}),
Expand All @@ -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) {
Expand Down
7 changes: 3 additions & 4 deletions packages/docusaurus-theme-live-codeblock/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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() {
Expand All @@ -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'),
},
},
};
Expand Down
4 changes: 2 additions & 2 deletions packages/docusaurus-theme-search-algolia/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ export default function themeSearchAlgolia(context: LoadContext): Plugin<void> {
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() {
Expand Down
4 changes: 4 additions & 0 deletions packages/docusaurus-types/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,10 @@ export interface Plugin<Content = unknown> {
export type InitializedPlugin<Content = unknown> = Plugin<Content> & {
readonly options: Required<PluginOptions>;
readonly version: DocusaurusPluginVersionInformation;
/**
* The absolute path to the folder containing the entry point file.
*/
readonly path: string;
};

export type LoadedPlugin<Content = unknown> = InitializedPlugin<Content> & {
Expand Down
4 changes: 1 addition & 3 deletions packages/docusaurus/src/commands/swizzle/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -15,7 +14,6 @@ export async function initSwizzleContext(
siteDir: string,
): Promise<SwizzleContext> {
const context = await loadContext(siteDir);
const pluginRequire = createRequire(context.siteConfigPath);

const pluginConfigs = await loadPluginConfigs(context);
const plugins: InitializedPlugin[] = await initPlugins({
Expand All @@ -25,7 +23,7 @@ export async function initSwizzleContext(

const pluginsNormalized = await normalizePluginConfigs(
pluginConfigs,
pluginRequire,
context.siteConfigPath,
);

return {
Expand Down
13 changes: 11 additions & 2 deletions packages/docusaurus/src/commands/swizzle/themes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import logger from '@docusaurus/logger';
import path from 'path';
import leven from 'leven';
import _ from 'lodash';
import {askThemeName} from './prompts';
Expand Down Expand Up @@ -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
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ describe('loadClientModules', () => {
const clientModules = loadClientModules([pluginFooBar()]);
expect(clientModules).toMatchInlineSnapshot(`
[
"foo",
"bar",
"<PROJECT_ROOT>/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/foo",
"<PROJECT_ROOT>/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/bar",
]
`);
});
Expand All @@ -34,10 +34,10 @@ describe('loadClientModules', () => {
]);
expect(clientModules).toMatchInlineSnapshot(`
[
"foo",
"bar",
"hello",
"world",
"<PROJECT_ROOT>/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/foo",
"<PROJECT_ROOT>/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/bar",
"<PROJECT_ROOT>/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/hello",
"<PROJECT_ROOT>/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/world",
]
`);
});
Expand All @@ -49,10 +49,10 @@ describe('loadClientModules', () => {
]);
expect(clientModules).toMatchInlineSnapshot(`
[
"hello",
"world",
"foo",
"bar",
"<PROJECT_ROOT>/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/hello",
"<PROJECT_ROOT>/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/world",
"<PROJECT_ROOT>/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/foo",
"<PROJECT_ROOT>/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/bar",
]
`);
});
Expand All @@ -65,10 +65,10 @@ describe('loadClientModules', () => {
]);
expect(clientModules).toMatchInlineSnapshot(`
[
"hello",
"world",
"foo",
"bar",
"<PROJECT_ROOT>/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/hello",
"<PROJECT_ROOT>/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/world",
"<PROJECT_ROOT>/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/foo",
"<PROJECT_ROOT>/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/bar",
]
`);
});
Expand All @@ -81,10 +81,10 @@ describe('loadClientModules', () => {
]);
expect(clientModules).toMatchInlineSnapshot(`
[
"hello",
"world",
"foo",
"bar",
"<PROJECT_ROOT>/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/hello",
"<PROJECT_ROOT>/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/world",
"<PROJECT_ROOT>/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/foo",
"<PROJECT_ROOT>/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/bar",
]
`);
});
Expand Down
11 changes: 8 additions & 3 deletions packages/docusaurus/src/server/client-modules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<unknown>[],
plugins: LoadedPlugin<unknown>[],
): string[] {
return plugins.flatMap((plugin) => plugin.getClientModules?.() ?? []);
return plugins.flatMap(
(plugin) =>
plugin.getClientModules?.().map((p) => path.resolve(plugin.path, p)) ??
[],
);
}
7 changes: 6 additions & 1 deletion packages/docusaurus/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -192,6 +194,7 @@ function createBootstrapPlugin({
id: 'default',
},
version: {type: 'synthetic'},
path: siteDir,
getClientModules() {
return siteConfigClientModules;
},
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
Loading

0 comments on commit 68aaf92

Please sign in to comment.