-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement
import defer
proposal transform support (#15878)
- Loading branch information
1 parent
edfac24
commit a65f18e
Showing
57 changed files
with
819 additions
and
74 deletions.
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.