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
Gatsby does not support ESM. See gatsbyjs/gatsby#23705 and linked discussions for more information.
In particular, when running npm start midway through migrating the whole code to ESM, I was getting errors similar to the following:
[frontend] TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension "" for C:...\workspace\shields.cache\tmp-5248-0orlJ0tI72FY
npm run build was also producing different kinds of errors.
Current workaround
The solution was to break the frontend down into a separate module with its own frontend/package.json. Unlike Shields' top-level package.json, frontend/package.json does not contain the mention "type": "module", in other words it is still a CommonJS module.
In addition to mandating the extra frontend/package.json, the solution has the following caveats:
the Babel configuration must be specified in frontend/package.json.
the gatsby dependency must be specified in both package.json and frontend/package.json, Gatsby will otherwise complain that the frontend package is not a Gatsby project.
the start and build scripts from package.json have an extra cd frontend step so that Gatsby is run from frontend/package.json.
in line with the directory where Gatsby is now run, gatsby-browser.js, gatsby-config.js and gatsby-node.js have been moved to the frontend directory.
the supported-features.json and service-definitions.yml files are now produced in the frontend directory. This is only a minor detail to make relative paths simpler.
the result of the build script, i.e. the public folder, is moved up one level back into the project's root. Gatsby produces public in the frontend directory where it's run and that can't be changed (Configurable output folder gatsbyjs/gatsby#1878), and Heroku expects public to be in the project's root and that can't be changed ( Support running an app from a subdirectory heroku/heroku-buildpack-nodejs#385). Additionally, our Cypress end-to-end setup will expect public to be in the root as well.
the require-hacker dependency and the mocha-ignore-pngs.js file were removed, as not compatible with ESM. These probably aren't needed anymore, as the ESM loader ignores PNGs anyway if my understanding is correct.
the import statements in make-badge-url.js must be carefully tested, as they are used both by the ESM loader and Typescript loader, which have slightly different behaviours. For example, import { URL } from 'url' won't work there.
Future work
Once Gatsby becomes compatible with ESM, we would simply need to revert all aforementioned workarounds (apart from the require-hacker one), and potentially convert gatsby-browser.js, gatsby-config.js and gatsby-node.js to ESM formats as well. Additionally, it is my understanding that we will need to add js file extensions to the existing relative imports in the frontend code.
The text was updated successfully, but these errors were encountered:
PyvesB
added
frontend
The Docusaurus app serving the docs site
core
Server, BaseService, GitHub auth, Shared helpers
labels
Jul 9, 2021
This issue is kind of still relevant but has changed substantially.
Now that we are off of Gatsby, all the detail above has changed, but having migrated to Docusaurus, we do still have some CommonJS frontend code (but a lot less). See #9014 (comment)
This is related to #6651, in particular #6651 (comment).
Problem
Gatsby does not support ESM. See gatsbyjs/gatsby#23705 and linked discussions for more information.
In particular, when running
npm start
midway through migrating the whole code to ESM, I was getting errors similar to the following:npm run build
was also producing different kinds of errors.Current workaround
The solution was to break the frontend down into a separate module with its own
frontend/package.json
. Unlike Shields' top-levelpackage.json
,frontend/package.json
does not contain the mention"type": "module"
, in other words it is still a CommonJS module.In addition to mandating the extra
frontend/package.json
, the solution has the following caveats:frontend/package.json
.gatsby
dependency must be specified in bothpackage.json
andfrontend/package.json
, Gatsby will otherwise complain that the frontend package is not a Gatsby project.start
andbuild
scripts frompackage.json
have an extracd frontend
step so that Gatsby is run fromfrontend/package.json
.gatsby-browser.js
,gatsby-config.js
andgatsby-node.js
have been moved to thefrontend
directory.supported-features.json
andservice-definitions.yml
files are now produced in thefrontend
directory. This is only a minor detail to make relative paths simpler.build
script, i.e. thepublic
folder, is moved up one level back into the project's root. Gatsby producespublic
in thefrontend
directory where it's run and that can't be changed (Configurable output folder gatsbyjs/gatsby#1878), and Heroku expectspublic
to be in the project's root and that can't be changed (Support running an app from a subdirectory heroku/heroku-buildpack-nodejs#385). Additionally, our Cypress end-to-end setup will expect
public
to be in the root as well.require-hacker
dependency and themocha-ignore-pngs.js
file were removed, as not compatible with ESM. These probably aren't needed anymore, as the ESM loader ignores PNGs anyway if my understanding is correct.import
statements inmake-badge-url.js
must be carefully tested, as they are used both by the ESM loader and Typescript loader, which have slightly different behaviours. For example,import { URL } from 'url'
won't work there.Future work
Once Gatsby becomes compatible with ESM, we would simply need to revert all aforementioned workarounds (apart from the
require-hacker
one), and potentially convertgatsby-browser.js
,gatsby-config.js
andgatsby-node.js
to ESM formats as well. Additionally, it is my understanding that we will need to addjs
file extensions to the existing relative imports in the frontend code.The text was updated successfully, but these errors were encountered: