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
Implement import defer
proposal transform support
#15878
Merged
nicolo-ribaudo
merged 8 commits into
babel:feat-7.23.0/imports
from
nicolo-ribaudo:defer-import-transform
Sep 18, 2023
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e4a8dc4
[helper-module-transforms] Support customizing require&reference
nicolo-ribaudo 12d05c7
Support hooks in commonjs plugin, and implement `lazy` as a hook
nicolo-ribaudo 1901f6b
Add `@babel/plugin-syntax-import-defer`
nicolo-ribaudo 6141a92
Implement `import defer` proposal transform support
nicolo-ribaudo ace7ee1
Add to standalone
nicolo-ribaudo 691d725
Review
nicolo-ribaudo 39581c4
Remove from Stage 2 preset in standalone
nicolo-ribaudo ed3e000
Reduce output size
nicolo-ribaudo File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
37 changes: 37 additions & 0 deletions
37
packages/babel-helper-module-transforms/src/lazy-modules.ts
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,37 @@ | ||
// TODO: Move `lazy` implementation logic into the CommonJS plugin, since other | ||
// modules systems do not support `lazy`. | ||
|
||
import { types as t } from "@babel/core"; | ||
import { | ||
type SourceModuleMetadata, | ||
isSideEffectImport, | ||
} from "./normalize-and-load-metadata.ts"; | ||
|
||
export type Lazy = boolean | string[] | ((source: string) => boolean); | ||
|
||
export function toGetWrapperPayload(lazy: Lazy) { | ||
return (source: string, metadata: SourceModuleMetadata): null | "lazy" => { | ||
if (lazy === false) return null; | ||
if (isSideEffectImport(metadata) || metadata.reexportAll) return null; | ||
if (lazy === true) { | ||
// 'true' means that local relative files are eagerly loaded and | ||
// dependency modules are loaded lazily. | ||
return /\./.test(source) ? null : "lazy"; | ||
} | ||
if (Array.isArray(lazy)) { | ||
return lazy.indexOf(source) === -1 ? null : "lazy"; | ||
} | ||
if (typeof lazy === "function") { | ||
return lazy(source) ? "lazy" : null; | ||
} | ||
throw new Error(`.lazy must be a boolean, string array, or function`); | ||
}; | ||
} | ||
|
||
export function wrapReference( | ||
ref: t.Identifier, | ||
payload: unknown, | ||
): t.Expression | null { | ||
if (payload === "lazy") return t.callExpression(ref, []); | ||
return null; | ||
} |
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.
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.
Nit: can we make this interface generic? So that TS knows that the
payload
passed intowrapReference
must conforms to type of the return ofgetWrapperPayload
.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.
Unfortunately,
payload
might be unknown because it might be generated by the hook of another plugin. The best we can do iswhich does not improve types (
T | unknown
isunknown
) but it might improve autocompletion.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.
Good point. That seems to be similar to the
PluginPass
's behaviour: Any plugin can augment thePluginPass
but there is no guarantee that thePluginPass
must not be changed by other plugins within the same pass.