You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I set up a new website and added "type": "module" to the root package.json. Unsurprisingly, the build failed with a multitude of reasons. Some of them are related to docusaurus.config.js and sidebars.js being seen as ES modules, which will be fixed in #7371. However, there are some generated files that use require but are seen as ES modules, therefore triggering "require is not defined in ES module scope" error. To fix this, we simply need to make them .cjs, and use CJS-exclusive APIs instead. This is recommended practice because it makes the module type unambiguous.
In addition, I've added .cjs to the resolvable extensions list.
Test Plan
I applied these changes to a locally set up site and it works. The same changes should be reflected in the deploy preview as well.
When locally serving a production build, I noticed a flash of "cannot find module" banner from the serve handler. We will need to check that.
Oh, I think I hit the same issue as in #7238 (comment) 😭 I don't know how to fix this... The server build can't find the assets as soon as you do non-trivial changes to the module system... What's interesting here is that the server build doesn't fail, it only silently outputs a "module not found" banner... I don't know what's causing that behavior It's because the theme-fallback/Loading component catches the error and displays the error banner. Maybe we should throw the error up during SSR, so that it becomes visible? This is almost impossible to happen in userland, though.
OK, this is caused by the same problem as #7238 (comment), but in a different way. Because we need babel-plugin-dynamic-import-node to transform dynamic imports to require, so that Webpack does not code-split, we need to include .cjs extension in the JS loader regex as well. I forgot about that.
CLA SignedSigned Facebook CLApr: polishThis PR adds a very minor behavior improvement that users will enjoy.
3 participants
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pre-flight checklist
Motivation
Related: #6520
I set up a new website and added
"type": "module"
to the root package.json. Unsurprisingly, the build failed with a multitude of reasons. Some of them are related todocusaurus.config.js
andsidebars.js
being seen as ES modules, which will be fixed in #7371. However, there are some generated files that userequire
but are seen as ES modules, therefore triggering "require is not defined in ES module scope" error. To fix this, we simply need to make them.cjs
, and use CJS-exclusive APIs instead. This is recommended practice because it makes the module type unambiguous.In addition, I've added
.cjs
to the resolvable extensions list.Test Plan
I applied these changes to a locally set up site and it works. The same changes should be reflected in the deploy preview as well.
When locally serving a production build, I noticed a flash of "cannot find module" banner from the serve handler. We will need to check that.
Test links
Deploy preview: https://deploy-preview-_____--docusaurus-2.netlify.app/
Related issues/PRs