-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
52 additions
and
102 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
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,40 +1,48 @@ | ||
import type { ConfigAPI } from "@babel/core"; | ||
import { types as t } from "@babel/core"; | ||
import { declare } from "@babel/helper-plugin-utils"; | ||
import babelTemplate from "@babel/template"; | ||
import { getDependencyPolyfills } from "./dependency"; | ||
import type { Context, GetPreludeModules } from "./types"; | ||
|
||
/** | ||
* Ideally, we'd need something between `serializer.getPolyfills` and | ||
* `serializer.getModulesRunBeforeMainModule`. The former does not have access | ||
* to `require`, while the latter requires that the listed modules are | ||
* explicitly used in the bundle itself (see | ||
* https://github.com/facebook/metro/issues/850). For now, we will use this fact | ||
* to simply list all prelude modules. | ||
*/ | ||
function defaultModules({ projectRoot }: Context): string[] { | ||
const platforms = [ | ||
"react-native", | ||
"react-native-macos", | ||
"react-native-windows", | ||
]; | ||
const options = { paths: [projectRoot] }; | ||
|
||
const modules = []; | ||
for (const platform of platforms) { | ||
const core = `${platform}/Libraries/Core/InitializeCore`; | ||
try { | ||
modules.push(require.resolve(core, options)); | ||
} catch (_) { | ||
// ignore | ||
} | ||
} | ||
|
||
return modules; | ||
} | ||
|
||
export const getPreludeModules: GetPreludeModules = () => { | ||
const context = { projectRoot: process.cwd() }; | ||
const modules = defaultModules(context); | ||
const dependencyPolyfills = getDependencyPolyfills(context); | ||
return modules.concat(dependencyPolyfills); | ||
}; | ||
|
||
export default getPreludeModules; | ||
|
||
module.exports = declare((api: ConfigAPI) => { | ||
api.assertVersion(7); | ||
|
||
const pluginName = "@react-native-webapis/polyfills"; | ||
|
||
let isPolyfilled: string | null = null; | ||
|
||
return { | ||
name: pluginName, | ||
visitor: { | ||
Program: (path, context) => { | ||
const leadingComments = path.node.body[0]?.leadingComments; | ||
const codegen = leadingComments?.some((comment) => { | ||
const normalizedComment = comment.value.trim().split(" ")[0].trim(); | ||
return normalizedComment.startsWith("@react-native-webapis"); | ||
}); | ||
|
||
if (!codegen) { | ||
return; | ||
} | ||
|
||
if (isPolyfilled != null) { | ||
throw new Error( | ||
`'${pluginName}' is already applied to ${isPolyfilled}` | ||
); | ||
} | ||
|
||
isPolyfilled = context.file.opts.filename ?? "<unnamed module>"; | ||
|
||
const polyfills = getDependencyPolyfills({ projectRoot: context.cwd }); | ||
const importPolyfill = babelTemplate(`import %%source%%;`); | ||
|
||
for (const polyfill of polyfills) { | ||
path.unshiftContainer( | ||
"body", | ||
importPolyfill({ source: t.stringLiteral(polyfill) }) | ||
); | ||
} | ||
}, | ||
}, | ||
}; | ||
}); |