Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(v2): site client modules #3545

Merged
merged 2 commits into from
Oct 7, 2020
Merged

feat(v2): site client modules #3545

merged 2 commits into from
Oct 7, 2020

Conversation

slorber
Copy link
Collaborator

@slorber slorber commented Oct 6, 2020

Motivation

As of today, it's a bit boilerplaty to add some global JS to a site, as you need to create a local site plugin to implement getClientModules.

The following option makes it more idiomatic as you can directly pass the client modules as a config attribute:

siteConfig.clientModules = [myGlobalJsPath, myGlobalCssPath]

See also RN website PR: react-native-website-migration/react-native-website@b75a0e6#diff-20b869a62a5a74fb46c979edc17980f6R1-R19

Have you read the Contributing Guidelines on pull requests?

Yes

@slorber slorber requested a review from lex111 as a code owner October 6, 2020 17:46
@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Oct 6, 2020
@slorber slorber added the pr: new feature This PR adds a new API or behavior. label Oct 6, 2020
@docusaurus-bot
Copy link
Contributor

docusaurus-bot commented Oct 6, 2020

Deploy preview for docusaurus-2 ready!

Built with commit 2f835d6

https://deploy-preview-3545--docusaurus-2.netlify.app

const mod = clientModule.__esModule ? clientModule.default : clientModule;
if (mod && mod[lifecycleAction]) {
mod[lifecycleAction](...args);
const lifecycleFunction =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not familiar with this code, but surely removing the clientModule.__esModule won't affect it in any way? It was kind of related to Yarn 2 support (not sure)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a behavior change that shouldn't break anything, but allows you to write client lifecycle methods as named exports.

Before we had to use this syntax to add the lifecycle to default expor

export default {
    onRouteUpdate({location}) {
      console.log('onRouteUpdate', {location});
    },
};

Now we can also write the following:

export function onRouteUpdate({location}: {location: Location}) {
  console.log('onRouteUpdate', {location});
}

Note it's not a documented feature (yet) and kind of broken, so I plan to improve this as part of #3399

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW both versions still work, and it's the default export that is picked in priority if both are used, so it should be retrocompatible in all cases even if it's not very likely that such code exist in userland

export function onRouteUpdate({location}: {location: Location}) {
  console.log('onRouteUpdate', {location});
}

export default {
  onRouteUpdate: function onRouteUpdate({location}: {location: Location}) {
    console.log('default onRouteUpdate', {location});
  },
};

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation, so this is safe for merge.

website/docs/api/docusaurus.config.js.md Outdated Show resolved Hide resolved
Co-authored-by: Alexey Pyltsyn <lex61rus@gmail.com>
@lex111 lex111 merged commit 5b79f2e into master Oct 7, 2020
@lex111 lex111 deleted the slorber/site-client-modules branch October 7, 2020 12:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Signed Facebook CLA pr: new feature This PR adds a new API or behavior.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants