mirror of
https://github.com/facebook/docusaurus.git
synced 2025-08-06 10:20:09 +02:00
refactor(ganalytics, gtag): move options out of themeConfig (#5832)
* refactor(ganalytics, gtag): move options out of themeConfig * Forbid themeConfig options * Add PR link * Add key names to error message * Fix? * Doc updates Co-authored-by: Sébastien Lorber <slorber@users.noreply.github.com>
This commit is contained in:
parent
f5732e7589
commit
ac88d979f1
14 changed files with 274 additions and 106 deletions
|
@ -29,9 +29,19 @@ export default function preset(
|
|||
): Preset {
|
||||
const {siteConfig} = context;
|
||||
const {themeConfig} = siteConfig;
|
||||
const {algolia, googleAnalytics, gtag} = themeConfig as Partial<ThemeConfig>;
|
||||
const {algolia} = themeConfig as Partial<ThemeConfig>;
|
||||
const isProd = process.env.NODE_ENV === 'production';
|
||||
const {debug, docs, blog, pages, sitemap, theme, ...rest} = opts;
|
||||
const {
|
||||
debug,
|
||||
docs,
|
||||
blog,
|
||||
pages,
|
||||
sitemap,
|
||||
theme,
|
||||
googleAnalytics,
|
||||
gtag,
|
||||
...rest
|
||||
} = opts;
|
||||
|
||||
const themes: PluginConfig[] = [];
|
||||
themes.push(makePluginConfig('@docusaurus/theme-classic', theme));
|
||||
|
@ -50,13 +60,15 @@ export default function preset(
|
|||
plugins.push(makePluginConfig('@docusaurus/plugin-content-pages', pages));
|
||||
}
|
||||
if (isProd && googleAnalytics) {
|
||||
plugins.push(require.resolve('@docusaurus/plugin-google-analytics'));
|
||||
plugins.push(
|
||||
makePluginConfig('@docusaurus/plugin-google-analytics', googleAnalytics),
|
||||
);
|
||||
}
|
||||
if (debug || (debug === undefined && !isProd)) {
|
||||
plugins.push(require.resolve('@docusaurus/plugin-debug'));
|
||||
}
|
||||
if (isProd && gtag) {
|
||||
plugins.push(require.resolve('@docusaurus/plugin-google-gtag'));
|
||||
plugins.push(makePluginConfig('@docusaurus/plugin-google-gtag', gtag));
|
||||
}
|
||||
if (isProd && sitemap !== false) {
|
||||
plugins.push(makePluginConfig('@docusaurus/plugin-sitemap', sitemap));
|
||||
|
@ -65,7 +77,7 @@ export default function preset(
|
|||
throw new Error(
|
||||
`Unrecognized keys ${Object.keys(rest).join(
|
||||
', ',
|
||||
)} found in preset-classic configuration. The allowed keys are debug, docs, blog, pages, sitemap, theme. Check the documentation: https://docusaurus.io/docs/presets#docusauruspreset-classic for more information on how to configure individual plugins.`,
|
||||
)} found in preset-classic configuration. The allowed keys are debug, docs, blog, pages, sitemap, theme, googleAnalytics, gtag. Check the documentation: https://docusaurus.io/docs/presets#docusauruspreset-classic for more information on how to configure individual plugins.`,
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -12,13 +12,11 @@ export type Options = {
|
|||
pages?: false | import('@docusaurus/plugin-content-pages').Options;
|
||||
sitemap?: false | import('@docusaurus/plugin-sitemap').Options;
|
||||
theme?: import('@docusaurus/theme-classic').Options;
|
||||
googleAnalytics?: import('@docusaurus/plugin-google-analytics').Options;
|
||||
gtag?: import('@docusaurus/plugin-google-gtag').Options;
|
||||
};
|
||||
|
||||
export type ThemeConfig = import('@docusaurus/types').ThemeConfig &
|
||||
import('@docusaurus/theme-common').UserThemeConfig &
|
||||
// Those plugins themeConfigs should rather be moved to preset/plugin options
|
||||
// Plugin data can be made available to browser thank to the globalData api
|
||||
import('@docusaurus/plugin-google-analytics').ThemeConfig &
|
||||
import('@docusaurus/plugin-google-gtag').ThemeConfig & {
|
||||
import('@docusaurus/theme-common').UserThemeConfig & {
|
||||
algolia?: unknown; // TODO type plugin
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue