-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Support esm config files #936
Conversation
This adds support for loading `svelte.config.js` - config files in ESM format, which is now supported by language-tools. It tries to load the `.js` format first and falls back to `.cjs` to ensure backwards compatibility; some people may also want to keep using cjs config files due to things like easier imports of JSON files. This is also the reason why there's no change in the starter template: its config contains an import of `package.json`, and JSON imports are hidden behind an experimental node flag for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The flag mentioned is --experimental-json-modules
: https://nodejs.org/api/esm.html#esm_json_modules
It looks like it was added in Node 12: https://nodejs.medium.com/announcing-a-new-experimental-modules-1be8d2d6c2ff
The package.json
import is used to import dependencies
:
noExternal: Object.keys(pkg.dependencies || {}) |
The need to do that may change in the future? I'm not quite sure. Some discussion about it here: #904 (comment)
Anyway, this seems like a reasonable default for now I think
Thanks so much for getting this to be supported!! 🎉
Converting this to a draft because I just noticed that this also needs an update in |
This is now ready to review. I'd like to get a manual test of this from a linux/mac user. I tested this on windows and it worked for me. To test, I scaffolded the starter template and replaced the cjs config with an esm config. I did not change the starter template even if it doesn't have the |
Would |
Maybe? If that is the suggested way by node, then yeah, probably. But this is a thing the user then has to do in his file, there's no way to do this automatically
The only way to load ESM in CJS is to do the dynamic |
Right, which is why I was saying their CJS config would have no choice but exporting a promise resolving to their config. Do we want to plan for that by having an extra await as appropriate where we load the config? Or do we want to push |
const config_file_esm = path.join(cwd, 'svelte.config.js'); | ||
const config_file = fs.existsSync(config_file_esm) | ||
? config_file_esm | ||
: path.join(cwd, 'svelte.config.cjs'); | ||
const config = await import(url.pathToFileURL(config_file).href); | ||
const validated = validate_config(config.default); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think an extra await here would be all that would be needed if we wanted to support files that exported a promise resolving to the configuration.
I think having the config being asynchronous should be okay for Kit, the language-tools and the vite plugin. Since all of them use Either way, I want this to go into separate PRs. This one makes it possible to use ESM configs while not changing any defaults, and it is still possible to use CJS. |
This adds support for loading
svelte.config.js
- config files in ESM format, which is now supported by language-tools. It tries to load the.js
format first and falls back to.cjs
to ensure backwards compatibility; some people may also want to keep using cjs config files due to things like easier imports of JSON files. This is also the reason why there's no change in the starter template: its config contains an import ofpackage.json
, and JSON imports are hidden behind an experimental node flag for now.