-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
chore(gatsby): upgrade latest-version #36434
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1b367c0
chore(gatsby): upgrade latest-version
pieh 0d5c926
use latest of latest-version, now that we have higher minimal node ve…
pieh 2215c65
use dynamic import to be able to import esm from file that we convert…
pieh 66dd212
allow keeping dynamic imports in selected files and apply this to fee…
pieh 2dc7748
no end line package.json
pieh 2aefe78
typo
pieh 906e491
add tests for override
pieh 05a177e
revert accidental change
pieh 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
54 changes: 54 additions & 0 deletions
54
packages/babel-preset-gatsby-package/lib/babel-trasnform-mark-to-keep-dynamic-import.js
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,54 @@ | ||
const path = require(`path`) | ||
|
||
/** | ||
* @typedef {import('@babel/core').NodePath} NodePath | ||
* @typedef {import('@babel/core').PluginObj} PluginObj | ||
* @typedef {import('@babel/core').PluginPass} PluginPass | ||
* @typedef {import('@babel/core').types} BabelTypes | ||
* @typedef {import('@babel/core').types.Identifier} Identifier | ||
* @typedef {import('@babel/core').types.MemberExpression} MemberExpression | ||
*/ | ||
|
||
/** | ||
* @typedef {Object} IPluginOptions | ||
* @property {Array<string>} keepDynamicImports | ||
*/ | ||
|
||
/** | ||
* | ||
* @param {{ types: BabelTypes }} _unused | ||
* @param {Partial<IPluginOptions>} opts | ||
* @returns {PluginObj} | ||
*/ | ||
module.exports = function keepDynamicImports( | ||
_unused, | ||
opts | ||
) { | ||
if (!opts.keepDynamicImports) { | ||
throw new Error(`keepDynamicImports option needs to be set`) | ||
} else if (!Array.isArray(opts.keepDynamicImports)) { | ||
throw new Error(`keepDynamicImports option needs to be an array`) | ||
} | ||
|
||
const absolutePaths = opts.keepDynamicImports.map(p => path.resolve(p)) | ||
|
||
return { | ||
name: `babel-transform-mark-to-keep-dynamic-import`, | ||
visitor: { | ||
Program() { | ||
const filename = this.file?.opts?.filename | ||
if (!filename) { | ||
return | ||
} | ||
|
||
if (absolutePaths.includes(filename)) { | ||
// this is big hack - it relies on some babel plugins internal to basically | ||
// do early return ( https://github.com/babel/babel/blob/3526b79c87863052f1c61ec0c49c0fc287ba32e6/packages/babel-plugin-transform-modules-commonjs/src/index.ts#L174 ) | ||
// on top of that `BabelFile` doesn't expose delete for the metadata, | ||
// so we reach into internal `_map` to delete it | ||
this.file._map.delete("@babel/plugin-proposal-dynamic-import") | ||
} | ||
} | ||
}, | ||
} | ||
} |
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
Oops, something went wrong.
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 PR adds a way to mark individual files to not transform
await import("")
to
and instead keep dynamic import
We have plenty of dynamic imports in our codebase /
gatsby
package and switching it everywhere (through already existingesm: true
option thatbabel-preset-gatsby-package
) doesn't seem like good idea and definitely would require solid testing.This approach allows us to address individual modules and we can over-time migrate more things if needed