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
This commit is contained in:
Joshua Chen 2022-03-16 20:47:15 +08:00 committed by GitHub
parent 8d1c1954c1
commit 68aaf9201f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
26 changed files with 100 additions and 68 deletions

View file

@ -24,10 +24,10 @@ export default function pluginDebug({
name: 'docusaurus-plugin-debug', name: 'docusaurus-plugin-debug',
getThemePath() { getThemePath() {
return path.resolve(__dirname, '../lib/theme'); return '../lib/theme';
}, },
getTypeScriptThemePath() { getTypeScriptThemePath() {
return path.resolve(__dirname, '../src/theme'); return '../src/theme';
}, },
async contentLoaded({actions: {createData, addRoute}, allContent}) { async contentLoaded({actions: {createData, addRoute}, allContent}) {

View file

@ -5,7 +5,6 @@
* LICENSE file in the root directory of this source tree. * LICENSE file in the root directory of this source tree.
*/ */
import path from 'path';
import {Joi} from '@docusaurus/utils-validation'; import {Joi} from '@docusaurus/utils-validation';
import type { import type {
LoadContext, LoadContext,
@ -28,7 +27,7 @@ export default function pluginGoogleAnalytics(
name: 'docusaurus-plugin-google-analytics', name: 'docusaurus-plugin-google-analytics',
getClientModules() { getClientModules() {
return isProd ? [path.resolve(__dirname, './analytics')] : []; return isProd ? ['./analytics'] : [];
}, },
injectHtmlTags() { injectHtmlTags() {

View file

@ -5,7 +5,6 @@
* LICENSE file in the root directory of this source tree. * LICENSE file in the root directory of this source tree.
*/ */
import path from 'path';
import {Joi} from '@docusaurus/utils-validation'; import {Joi} from '@docusaurus/utils-validation';
import type { import type {
LoadContext, LoadContext,
@ -32,7 +31,7 @@ export default function pluginGoogleGtag(
}, },
getClientModules() { getClientModules() {
return isProd ? [path.resolve(__dirname, './gtag')] : []; return isProd ? ['./gtag'] : [];
}, },
injectHtmlTags() { injectHtmlTags() {

View file

@ -15,8 +15,6 @@ import type {PluginOptions} from '@docusaurus/plugin-ideal-image';
import {Joi} from '@docusaurus/utils-validation'; import {Joi} from '@docusaurus/utils-validation';
import {readDefaultCodeTranslationMessages} from '@docusaurus/theme-translations'; import {readDefaultCodeTranslationMessages} from '@docusaurus/theme-translations';
import path from 'path';
export default function pluginIdealImage( export default function pluginIdealImage(
context: LoadContext, context: LoadContext,
options: PluginOptions, options: PluginOptions,
@ -29,11 +27,11 @@ export default function pluginIdealImage(
name: 'docusaurus-plugin-ideal-image', name: 'docusaurus-plugin-ideal-image',
getThemePath() { getThemePath() {
return path.resolve(__dirname, '../lib/theme'); return '../lib/theme';
}, },
getTypeScriptThemePath() { getTypeScriptThemePath() {
return path.resolve(__dirname, '../src/theme'); return '../src/theme';
}, },
getDefaultCodeTranslationMessages() { getDefaultCodeTranslationMessages() {

View file

@ -64,10 +64,10 @@ export default function pluginPWA(
name: 'docusaurus-plugin-pwa', name: 'docusaurus-plugin-pwa',
getThemePath() { getThemePath() {
return path.resolve(__dirname, '../lib/theme'); return '../lib/theme';
}, },
getTypeScriptThemePath() { getTypeScriptThemePath() {
return path.resolve(__dirname, '../src/theme'); return '../src/theme';
}, },
getClientModules() { getClientModules() {
@ -138,7 +138,7 @@ export default function pluginPWA(
const swSourceFileTest = /\.m?js$/; const swSourceFileTest = /\.m?js$/;
const swWebpackConfig: Configuration = { const swWebpackConfig: Configuration = {
entry: path.resolve(__dirname, 'sw.js'), entry: require.resolve('./sw.js'),
output: { output: {
path: outDir, path: outDir,
filename: 'sw.js', filename: 'sw.js',

View file

@ -5,7 +5,6 @@
* LICENSE file in the root directory of this source tree. * LICENSE file in the root directory of this source tree.
*/ */
import path from 'path';
import {Joi} from '@docusaurus/utils-validation'; import {Joi} from '@docusaurus/utils-validation';
import type { import type {
ThemeConfig, ThemeConfig,
@ -24,7 +23,7 @@ const DEFAULT_OPTIONS = {
injectManifestConfig: {}, injectManifestConfig: {},
pwaHead: [], pwaHead: [],
swCustom: undefined, swCustom: undefined,
swRegister: path.join(__dirname, 'registerSw.js'), swRegister: './registerSw.js',
reloadPopup: '@theme/PwaReloadPopup', reloadPopup: '@theme/PwaReloadPopup',
}; };

View file

@ -8,7 +8,6 @@
import type {LoadContext, Plugin, PostCssOptions} from '@docusaurus/types'; import type {LoadContext, Plugin, PostCssOptions} from '@docusaurus/types';
import type {ThemeConfig} from '@docusaurus/theme-common'; import type {ThemeConfig} from '@docusaurus/theme-common';
import {getTranslationFiles, translateThemeConfig} from './translations'; import {getTranslationFiles, translateThemeConfig} from './translations';
import path from 'path';
import {createRequire} from 'module'; import {createRequire} from 'module';
import type {Plugin as PostCssPlugin} from 'postcss'; import type {Plugin as PostCssPlugin} from 'postcss';
import rtlcss from 'rtlcss'; import rtlcss from 'rtlcss';
@ -112,11 +111,11 @@ export default function docusaurusThemeClassic(
name: 'docusaurus-theme-classic', name: 'docusaurus-theme-classic',
getThemePath() { getThemePath() {
return path.join(__dirname, '../lib-next/theme'); return '../lib-next/theme';
}, },
getTypeScriptThemePath() { getTypeScriptThemePath() {
return path.resolve(__dirname, '../src/theme'); return '../src/theme';
}, },
getTranslationFiles: async () => getTranslationFiles({themeConfig}), getTranslationFiles: async () => getTranslationFiles({themeConfig}),
@ -137,8 +136,8 @@ export default function docusaurusThemeClassic(
getClientModules() { getClientModules() {
const modules = [ const modules = [
require.resolve(getInfimaCSSFile(direction)), require.resolve(getInfimaCSSFile(direction)),
path.resolve(__dirname, './prism-include-languages'), './prism-include-languages',
path.resolve(__dirname, './admonitions.css'), './admonitions.css',
]; ];
if (customCss) { if (customCss) {

View file

@ -5,7 +5,6 @@
* LICENSE file in the root directory of this source tree. * LICENSE file in the root directory of this source tree.
*/ */
import path from 'path';
import {readDefaultCodeTranslationMessages} from '@docusaurus/theme-translations'; import {readDefaultCodeTranslationMessages} from '@docusaurus/theme-translations';
import type {LoadContext, Plugin} from '@docusaurus/types'; import type {LoadContext, Plugin} from '@docusaurus/types';
@ -18,10 +17,10 @@ export default function themeLiveCodeblock(context: LoadContext): Plugin {
name: 'docusaurus-theme-live-codeblock', name: 'docusaurus-theme-live-codeblock',
getThemePath() { getThemePath() {
return path.resolve(__dirname, '../lib/theme'); return '../lib/theme';
}, },
getTypeScriptThemePath() { getTypeScriptThemePath() {
return path.resolve(__dirname, '../src/theme'); return '../src/theme';
}, },
getDefaultCodeTranslationMessages() { getDefaultCodeTranslationMessages() {
@ -35,7 +34,7 @@ export default function themeLiveCodeblock(context: LoadContext): Plugin {
return { return {
resolve: { resolve: {
alias: { alias: {
buble: path.resolve(__dirname, './custom-buble.js'), buble: require.resolve('./custom-buble.js'),
}, },
}, },
}; };

View file

@ -47,10 +47,10 @@ export default function themeSearchAlgolia(context: LoadContext): Plugin<void> {
name: 'docusaurus-theme-search-algolia', name: 'docusaurus-theme-search-algolia',
getThemePath() { getThemePath() {
return path.resolve(__dirname, '../lib/theme'); return '../lib/theme';
}, },
getTypeScriptThemePath() { getTypeScriptThemePath() {
return path.resolve(__dirname, '../src/theme'); return '../src/theme';
}, },
getDefaultCodeTranslationMessages() { getDefaultCodeTranslationMessages() {

View file

@ -290,6 +290,10 @@ export interface Plugin<Content = unknown> {
export type InitializedPlugin<Content = unknown> = Plugin<Content> & { export type InitializedPlugin<Content = unknown> = Plugin<Content> & {
readonly options: Required<PluginOptions>; readonly options: Required<PluginOptions>;
readonly version: DocusaurusPluginVersionInformation; readonly version: DocusaurusPluginVersionInformation;
/**
* The absolute path to the folder containing the entry point file.
*/
readonly path: string;
}; };
export type LoadedPlugin<Content = unknown> = InitializedPlugin<Content> & { export type LoadedPlugin<Content = unknown> = InitializedPlugin<Content> & {

View file

@ -5,7 +5,6 @@
* LICENSE file in the root directory of this source tree. * LICENSE file in the root directory of this source tree.
*/ */
import {createRequire} from 'module';
import {loadContext, loadPluginConfigs} from '../../server'; import {loadContext, loadPluginConfigs} from '../../server';
import initPlugins, {normalizePluginConfigs} from '../../server/plugins/init'; import initPlugins, {normalizePluginConfigs} from '../../server/plugins/init';
import type {InitializedPlugin} from '@docusaurus/types'; import type {InitializedPlugin} from '@docusaurus/types';
@ -15,7 +14,6 @@ export async function initSwizzleContext(
siteDir: string, siteDir: string,
): Promise<SwizzleContext> { ): Promise<SwizzleContext> {
const context = await loadContext(siteDir); const context = await loadContext(siteDir);
const pluginRequire = createRequire(context.siteConfigPath);
const pluginConfigs = await loadPluginConfigs(context); const pluginConfigs = await loadPluginConfigs(context);
const plugins: InitializedPlugin[] = await initPlugins({ const plugins: InitializedPlugin[] = await initPlugins({
@ -25,7 +23,7 @@ export async function initSwizzleContext(
const pluginsNormalized = await normalizePluginConfigs( const pluginsNormalized = await normalizePluginConfigs(
pluginConfigs, pluginConfigs,
pluginRequire, context.siteConfigPath,
); );
return { return {

View file

@ -6,6 +6,7 @@
*/ */
import logger from '@docusaurus/logger'; import logger from '@docusaurus/logger';
import path from 'path';
import leven from 'leven'; import leven from 'leven';
import _ from 'lodash'; import _ from 'lodash';
import {askThemeName} from './prompts'; import {askThemeName} from './prompts';
@ -132,8 +133,16 @@ export function getThemePath({
}): string { }): string {
const pluginInstance = getPluginByThemeName(plugins, themeName); const pluginInstance = getPluginByThemeName(plugins, themeName);
const themePath = typescript const themePath = typescript
? pluginInstance.instance.getTypeScriptThemePath?.() ? pluginInstance.instance.getTypeScriptThemePath &&
: pluginInstance.instance.getThemePath?.(); path.resolve(
pluginInstance.instance.path,
pluginInstance.instance.getTypeScriptThemePath(),
)
: pluginInstance.instance.getThemePath &&
path.resolve(
pluginInstance.instance.path,
pluginInstance.instance.getThemePath(),
);
if (!themePath) { if (!themePath) {
logger.warn( logger.warn(
typescript typescript

View file

@ -8,5 +8,6 @@
module.exports = function() { module.exports = function() {
return { return {
name: 'plugin-empty', name: 'plugin-empty',
path: __dirname,
}; };
}; };

View file

@ -8,6 +8,7 @@
module.exports = function() { module.exports = function() {
return { return {
name: 'plugin-foo-bar', name: 'plugin-foo-bar',
path: __dirname,
getClientModules() { getClientModules() {
return ['foo', 'bar']; return ['foo', 'bar'];
}, },

View file

@ -8,6 +8,7 @@
module.exports = function() { module.exports = function() {
return { return {
plugin: 'plugin-hello-world', plugin: 'plugin-hello-world',
path: __dirname,
getClientModules() { getClientModules() {
return ['hello', 'world']; return ['hello', 'world'];
}, },

View file

@ -21,8 +21,8 @@ describe('loadClientModules', () => {
const clientModules = loadClientModules([pluginFooBar()]); const clientModules = loadClientModules([pluginFooBar()]);
expect(clientModules).toMatchInlineSnapshot(` expect(clientModules).toMatchInlineSnapshot(`
[ [
"foo", "<PROJECT_ROOT>/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/foo",
"bar", "<PROJECT_ROOT>/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/bar",
] ]
`); `);
}); });
@ -34,10 +34,10 @@ describe('loadClientModules', () => {
]); ]);
expect(clientModules).toMatchInlineSnapshot(` expect(clientModules).toMatchInlineSnapshot(`
[ [
"foo", "<PROJECT_ROOT>/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/foo",
"bar", "<PROJECT_ROOT>/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/bar",
"hello", "<PROJECT_ROOT>/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/hello",
"world", "<PROJECT_ROOT>/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/world",
] ]
`); `);
}); });
@ -49,10 +49,10 @@ describe('loadClientModules', () => {
]); ]);
expect(clientModules).toMatchInlineSnapshot(` expect(clientModules).toMatchInlineSnapshot(`
[ [
"hello", "<PROJECT_ROOT>/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/hello",
"world", "<PROJECT_ROOT>/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/world",
"foo", "<PROJECT_ROOT>/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/foo",
"bar", "<PROJECT_ROOT>/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/bar",
] ]
`); `);
}); });
@ -65,10 +65,10 @@ describe('loadClientModules', () => {
]); ]);
expect(clientModules).toMatchInlineSnapshot(` expect(clientModules).toMatchInlineSnapshot(`
[ [
"hello", "<PROJECT_ROOT>/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/hello",
"world", "<PROJECT_ROOT>/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/world",
"foo", "<PROJECT_ROOT>/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/foo",
"bar", "<PROJECT_ROOT>/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/bar",
] ]
`); `);
}); });
@ -81,10 +81,10 @@ describe('loadClientModules', () => {
]); ]);
expect(clientModules).toMatchInlineSnapshot(` expect(clientModules).toMatchInlineSnapshot(`
[ [
"hello", "<PROJECT_ROOT>/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/hello",
"world", "<PROJECT_ROOT>/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/world",
"foo", "<PROJECT_ROOT>/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/foo",
"bar", "<PROJECT_ROOT>/packages/docusaurus/src/server/client-modules/__tests__/__fixtures__/bar",
] ]
`); `);
}); });

View file

@ -5,10 +5,15 @@
* LICENSE file in the root directory of this source tree. * 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( export default function loadClientModules(
plugins: Plugin<unknown>[], plugins: LoadedPlugin<unknown>[],
): string[] { ): string[] {
return plugins.flatMap((plugin) => plugin.getClientModules?.() ?? []); return plugins.flatMap(
(plugin) =>
plugin.getClientModules?.().map((p) => path.resolve(plugin.path, p)) ??
[],
);
} }

View file

@ -176,8 +176,10 @@ export async function loadPluginConfigs(
// - Resolve aliased theme components // - Resolve aliased theme components
// - Inject scripts/stylesheets // - Inject scripts/stylesheets
function createBootstrapPlugin({ function createBootstrapPlugin({
siteDir,
siteConfig, siteConfig,
}: { }: {
siteDir: string;
siteConfig: DocusaurusConfig; siteConfig: DocusaurusConfig;
}): LoadedPlugin { }): LoadedPlugin {
const { const {
@ -192,6 +194,7 @@ function createBootstrapPlugin({
id: 'default', id: 'default',
}, },
version: {type: 'synthetic'}, version: {type: 'synthetic'},
path: siteDir,
getClientModules() { getClientModules() {
return siteConfigClientModules; return siteConfigClientModules;
}, },
@ -244,6 +247,8 @@ function createMDXFallbackPlugin({
id: 'default', id: 'default',
}, },
version: {type: 'synthetic'}, version: {type: 'synthetic'},
// Synthetic, the path doesn't matter much
path: '.',
configureWebpack(config, isServer, {getJSLoader}) { configureWebpack(config, isServer, {getJSLoader}) {
// We need the mdx fallback loader to exclude files that were already // We need the mdx fallback loader to exclude files that were already
// processed by content plugins mdx loaders. This works, but a bit // 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})); plugins.push(createMDXFallbackPlugin({siteDir, siteConfig}));
// Load client modules. // Load client modules.

View file

@ -19,6 +19,7 @@ exports[`loadPlugins loads plugins 1`] = `
"options": { "options": {
"id": "default", "id": "default",
}, },
"path": "<PROJECT_ROOT>/packages/docusaurus/src/server/plugins/__tests__/__fixtures__/site-with-plugin",
"prop": "a", "prop": "a",
"version": { "version": {
"type": "local", "type": "local",
@ -31,6 +32,7 @@ exports[`loadPlugins loads plugins 1`] = `
"options": { "options": {
"id": "default", "id": "default",
}, },
"path": "<PROJECT_ROOT>/packages/docusaurus/src/server/plugins/__tests__/__fixtures__/site-with-plugin",
"version": { "version": {
"type": "local", "type": "local",
}, },

View file

@ -6,6 +6,7 @@
*/ */
import {createRequire} from 'module'; import {createRequire} from 'module';
import path from 'path';
import importFresh from 'import-fresh'; import importFresh from 'import-fresh';
import type { import type {
DocusaurusPluginVersionInformation, DocusaurusPluginVersionInformation,
@ -32,12 +33,18 @@ export type NormalizedPluginConfig = {
path: string; path: string;
module: ImportedPluginModule; 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( async function normalizePluginConfig(
pluginConfig: PluginConfig, pluginConfig: PluginConfig,
pluginRequire: NodeRequire, configPath: string,
): Promise<NormalizedPluginConfig> { ): Promise<NormalizedPluginConfig> {
const pluginRequire = createRequire(configPath);
// plugins: ['./plugin'] // plugins: ['./plugin']
if (typeof pluginConfig === 'string') { if (typeof pluginConfig === 'string') {
const pluginModuleImport = pluginConfig; const pluginModuleImport = pluginConfig;
@ -50,6 +57,7 @@ async function normalizePluginConfig(
path: pluginModuleImport, path: pluginModuleImport,
module: pluginModule, module: pluginModule,
}, },
entryPath: pluginPath,
}; };
} }
@ -58,6 +66,7 @@ async function normalizePluginConfig(
return { return {
plugin: pluginConfig, plugin: pluginConfig,
options: {}, options: {},
entryPath: configPath,
}; };
} }
@ -75,6 +84,7 @@ async function normalizePluginConfig(
path: pluginModuleImport, path: pluginModuleImport,
module: pluginModule, module: pluginModule,
}, },
entryPath: pluginPath,
}; };
} }
// plugins: [ // plugins: [
@ -83,16 +93,17 @@ async function normalizePluginConfig(
return { return {
plugin: pluginConfig[0], plugin: pluginConfig[0],
options: pluginConfig[1], options: pluginConfig[1],
entryPath: configPath,
}; };
} }
export async function normalizePluginConfigs( export async function normalizePluginConfigs(
pluginConfigs: PluginConfig[], pluginConfigs: PluginConfig[],
pluginRequire: NodeRequire, configPath: string,
): Promise<NormalizedPluginConfig[]> { ): Promise<NormalizedPluginConfig[]> {
return Promise.all( return Promise.all(
pluginConfigs.map((pluginConfig) => pluginConfigs.map((pluginConfig) =>
normalizePluginConfig(pluginConfig, pluginRequire), normalizePluginConfig(pluginConfig, configPath),
), ),
); );
} }
@ -135,7 +146,7 @@ export default async function initPlugins({
const pluginRequire = createRequire(context.siteConfigPath); const pluginRequire = createRequire(context.siteConfigPath);
const pluginConfigsNormalized = await normalizePluginConfigs( const pluginConfigsNormalized = await normalizePluginConfigs(
pluginConfigs, pluginConfigs,
pluginRequire, context.siteConfigPath,
); );
async function doGetPluginVersion( async function doGetPluginVersion(
@ -206,6 +217,7 @@ export default async function initPlugins({
...pluginInstance, ...pluginInstance,
options: pluginOptions, options: pluginOptions,
version: pluginVersion, version: pluginVersion,
path: path.dirname(normalizedPluginConfig.entryPath),
}; };
} }

View file

@ -51,7 +51,10 @@ export function loadPluginsThemeAliases({
plugins: LoadedPlugin[]; plugins: LoadedPlugin[];
}): Promise<ThemeAliases> { }): Promise<ThemeAliases> {
const pluginThemes: string[] = plugins 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)); .filter((x): x is string => Boolean(x));
const userTheme = path.resolve(siteDir, THEME_PATH); const userTheme = path.resolve(siteDir, THEME_PATH);
return loadThemeAliases([ThemeFallbackDir, ...pluginThemes], [userTheme]); return loadThemeAliases([ThemeFallbackDir, ...pluginThemes], [userTheme]);

View file

@ -56,7 +56,7 @@ function getPluginSourceCodeFilePaths(plugin: InitializedPlugin): string[] {
codePaths.push(themePath); codePaths.push(themePath);
} }
return codePaths; return codePaths.map((p) => nodePath.resolve(plugin.path, p));
} }
export async function globSourceCodeFilePaths( export async function globSourceCodeFilePaths(

View file

@ -6,7 +6,6 @@
*/ */
const fs = require('fs'); const fs = require('fs');
const path = require('path');
/** @type {import('@docusaurus/types').PluginConfig[]} */ /** @type {import('@docusaurus/types').PluginConfig[]} */
const dogfoodingThemeInstances = [ const dogfoodingThemeInstances = [
@ -14,8 +13,7 @@ const dogfoodingThemeInstances = [
function swizzleThemeTests() { function swizzleThemeTests() {
return { return {
name: 'swizzle-theme-tests', name: 'swizzle-theme-tests',
getThemePath: () => getThemePath: () => './_swizzle_theme_tests/src/theme',
path.join(__dirname, '_swizzle_theme_tests/src/theme'),
}; };
}, },
]; ];

View file

@ -61,7 +61,7 @@ module.exports = function (context, options) {
## `getThemePath()` {#getThemePath} ## `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: For example, your `getThemePath` can be:
@ -73,7 +73,7 @@ module.exports = function (context, options) {
name: 'my-theme', name: 'my-theme',
// highlight-start // highlight-start
getThemePath() { getThemePath() {
return path.resolve(__dirname, './theme'); return './theme';
}, },
// highlight-end // highlight-end
}; };
@ -103,11 +103,11 @@ module.exports = function (context, options) {
// highlight-start // highlight-start
getThemePath() { getThemePath() {
// Where compiled JavaScript output lives // Where compiled JavaScript output lives
return path.join(__dirname, '../lib/theme'); return '../lib/theme';
}, },
getTypeScriptThemePath() { getTypeScriptThemePath() {
// Where TypeScript source code lives // Where TypeScript source code lives
return path.resolve(__dirname, '../src/theme'); return '../src/theme';
}, },
// highlight-end // highlight-end
}; };

View file

@ -205,7 +205,7 @@ const config = {
'queryString', 'queryString',
], ],
// swRegister: false, // swRegister: false,
swCustom: path.resolve(__dirname, 'src/sw.js'), swCustom: require.resolve('./src/sw.js'),
pwaHead: [ pwaHead: [
{ {
tagName: 'link', tagName: 'link',

View file

@ -146,7 +146,7 @@ async function ChangelogPlugin(context, options) {
return config; return config;
}, },
getThemePath() { getThemePath() {
return path.join(__dirname, './theme'); return './theme';
}, },
getPathsToWatch() { getPathsToWatch() {
// Don't watch the generated dir // Don't watch the generated dir