-
-
Notifications
You must be signed in to change notification settings - Fork 529
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
feat: add support for ESM config files #987
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// this file is imported by import-helper to detect whether dynamic imports are supported. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
async function supportsDynamicImport() { | ||
try { | ||
// imports are cached. | ||
// no need to worry about perf here. | ||
// Don't remove .js: extension must be included for ESM imports! | ||
await import('./dummy-file.js'); | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* Imports a JSON, CommonJS or ESM module | ||
* based on feature detection. | ||
* | ||
* @param modulePath path to the module to import | ||
* @returns {Promise<unknown>} the imported module. | ||
*/ | ||
async function importModule(modulePath) { | ||
// JSON modules are still behind a flag. Fallback to require for now. | ||
// https://nodejs.org/api/esm.html#json-modules | ||
if (!modulePath.endsWith('.json') && (await supportsDynamicImport())) { | ||
return import(modulePath); | ||
} | ||
|
||
// mimics what `import()` would return for | ||
// cjs modules. | ||
return { default: require(modulePath) }; | ||
} | ||
|
||
module.exports = { | ||
supportsDynamicImport, | ||
importModule, | ||
}; |
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
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.
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.
This assumes that the module only has a default export, and is not compatible with named exports.
For the purposes of the config that's fine, because the config is exported on the default export when is ESM compatible, but this is not true for all cases.
Which makes me feel like it would be better to invoke
supportsDynamicImport
inconfig-helper.js
and either use the import helper, or straight up require.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'm not sure I understand what you mean.
If the user is using named exports then the platform is compatible with ESM and
import()
will be used, which supports named exports.If they're using CJS, there are no named-exports.
Native
import()
of a CJS file already returns{ default: module.exports }
.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 understand what you mean, I think I didn't fully understand what you were saying in #982
But basically you wanted to write a node 10 compatible
importModule
function, while I was under the impression that you were going to drop support for node 10.EDIT: by you, I mean the sequelize team
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.
We'll indeed drop Node 10 for v7, but I don't think we want to wait with fixing this until the core sequelize package is ready for that
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.
Yea, makes sense, I think we can resolve this then.
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.
Great, so that's an approval from you? Then I think we can merge and set up a new release
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.
Yes exactly :) The point was to replicate
import()
usingrequire
, the code that usesimport-helper
is then responsible for properly using the moduleThere 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.
For sequelize v7, will this solution be dropped and using the native
import
be supported instead? Along with.sequelizerc
no longer havingmodule.exports
? Should I open an issue for this?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.
Yes, the next major will drop support for node 10 so
import()
will always be used.Supporting ESM in
.sequelizerc
could already be done in this version though. It should be a small change too.Simply need to make this line use the new import helper
cli/src/core/yargs.js
Line 9 in 54ec29d
.sequelizerc
+.sequelizerc.json
+.sequelizerc.cjs
+.sequelizerc.mjs
+.sequelizerc.js
.(feel free to open an issue)
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.
Thanks! I've openeded #989 , which is only for v7.
I think current version is fine as is, would prefer focusing on v7 since sequelize v7 is almost ready.