@rootnative/web 0.0.0-alpha.2 → 0.0.0-alpha.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +66 -1
- package/dist/apis/AccessibilityInfo/index.d.ts +30 -0
- package/dist/apis/AccessibilityInfo/index.js +84 -0
- package/dist/apis/AccessibilityInfo/index.js.map +1 -0
- package/dist/apis/Alert/index.d.ts +22 -0
- package/dist/apis/Alert/index.js +141 -0
- package/dist/apis/Alert/index.js.map +1 -0
- package/dist/apis/AppState/index.d.ts +21 -0
- package/dist/apis/AppState/index.js +86 -0
- package/dist/apis/AppState/index.js.map +1 -0
- package/dist/apis/Appearance/index.d.ts +14 -0
- package/dist/apis/Appearance/index.js +39 -0
- package/dist/apis/Appearance/index.js.map +1 -0
- package/dist/apis/BackHandler/index.d.ts +10 -0
- package/dist/apis/BackHandler/index.js +16 -0
- package/dist/apis/BackHandler/index.js.map +1 -0
- package/dist/apis/Clipboard/index.d.ts +9 -0
- package/dist/apis/Clipboard/index.js +45 -0
- package/dist/apis/Clipboard/index.js.map +1 -0
- package/dist/apis/Dimensions/index.d.ts +19 -0
- package/dist/apis/Dimensions/index.js +66 -0
- package/dist/apis/Dimensions/index.js.map +1 -0
- package/dist/apis/I18nManager/index.d.ts +14 -0
- package/dist/apis/I18nManager/index.js +33 -0
- package/dist/apis/I18nManager/index.js.map +1 -0
- package/dist/apis/Keyboard/index.d.ts +36 -0
- package/dist/apis/Keyboard/index.js +149 -0
- package/dist/apis/Keyboard/index.js.map +1 -0
- package/dist/apis/Linking/index.d.ts +29 -0
- package/dist/apis/Linking/index.js +86 -0
- package/dist/apis/Linking/index.js.map +1 -0
- package/dist/apis/PanResponder/index.d.ts +50 -0
- package/dist/apis/PanResponder/index.js +239 -0
- package/dist/apis/PanResponder/index.js.map +1 -0
- package/dist/apis/PixelRatio/index.d.ts +8 -0
- package/dist/apis/PixelRatio/index.js +15 -0
- package/dist/apis/PixelRatio/index.js.map +1 -0
- package/dist/apis/Platform/index.d.ts +4 -0
- package/dist/apis/Platform/index.js +28 -0
- package/dist/apis/Platform/index.js.map +1 -0
- package/dist/apis/Platform/types.d.ts +19 -0
- package/dist/apis/Share/index.d.ts +30 -0
- package/dist/apis/Share/index.js +34 -0
- package/dist/apis/Share/index.js.map +1 -0
- package/dist/apis/StyleSheet/index.d.ts +23 -0
- package/dist/apis/StyleSheet/index.js +29 -0
- package/dist/apis/StyleSheet/index.js.map +1 -0
- package/dist/apis/StyleSheet/types.d.ts +328 -0
- package/dist/apis/Vibration/index.d.ts +9 -0
- package/dist/apis/Vibration/index.js +40 -0
- package/dist/apis/Vibration/index.js.map +1 -0
- package/dist/apis/processColor/index.d.ts +3 -0
- package/dist/apis/processColor/index.js +12 -0
- package/dist/apis/processColor/index.js.map +1 -0
- package/dist/apis/useColorScheme/index.d.ts +3 -0
- package/dist/apis/useColorScheme/index.js +13 -0
- package/dist/apis/useColorScheme/index.js.map +1 -0
- package/dist/apis/useWindowDimensions/index.d.ts +3 -0
- package/dist/apis/useWindowDimensions/index.js +19 -0
- package/dist/apis/useWindowDimensions/index.js.map +1 -0
- package/dist/assets-registry/index.d.ts +18 -0
- package/dist/assets-registry/index.js +12 -0
- package/dist/assets-registry/index.js.map +1 -0
- package/dist/bundler/index.js +30 -0
- package/dist/bundler/index.js.map +1 -0
- package/dist/bundler/inlinePlatformOS.js +121 -0
- package/dist/bundler/inlinePlatformOS.js.map +1 -0
- package/dist/bundler/worklets.js +61 -0
- package/dist/bundler/worklets.js.map +1 -0
- package/dist/compat/TurboModuleRegistry/index.d.ts +16 -0
- package/dist/compat/TurboModuleRegistry/index.js +14 -0
- package/dist/compat/TurboModuleRegistry/index.js.map +1 -0
- package/dist/compat/createElement/index.d.ts +9 -0
- package/dist/compat/createElement/index.js +40 -0
- package/dist/compat/createElement/index.js.map +1 -0
- package/dist/compat/findNodeHandle/index.d.ts +7 -0
- package/dist/compat/findNodeHandle/index.js +19 -0
- package/dist/compat/findNodeHandle/index.js.map +1 -0
- package/dist/components/ActivityIndicator/index.d.ts +5 -0
- package/dist/components/ActivityIndicator/index.js +74 -0
- package/dist/components/ActivityIndicator/index.js.map +1 -0
- package/dist/components/ActivityIndicator/types.d.ts +8 -0
- package/dist/components/Button/index.d.ts +5 -0
- package/dist/components/Button/index.js +48 -0
- package/dist/components/Button/index.js.map +1 -0
- package/dist/components/Button/types.d.ts +31 -0
- package/dist/components/FlatList/index.d.ts +5 -0
- package/dist/components/FlatList/index.js +433 -0
- package/dist/components/FlatList/index.js.map +1 -0
- package/dist/components/FlatList/types.d.ts +138 -0
- package/dist/components/FlatList/viewability.js +86 -0
- package/dist/components/FlatList/viewability.js.map +1 -0
- package/dist/components/Image/index.d.ts +7 -0
- package/dist/components/Image/index.js +301 -0
- package/dist/components/Image/index.js.map +1 -0
- package/dist/components/Image/source.js +97 -0
- package/dist/components/Image/source.js.map +1 -0
- package/dist/components/Image/types.d.ts +96 -0
- package/dist/components/KeyboardAvoidingView/index.d.ts +10 -0
- package/dist/components/KeyboardAvoidingView/index.js +118 -0
- package/dist/components/KeyboardAvoidingView/index.js.map +1 -0
- package/dist/components/KeyboardAvoidingView/types.d.ts +14 -0
- package/dist/components/Modal/index.d.ts +9 -0
- package/dist/components/Modal/index.js +271 -0
- package/dist/components/Modal/index.js.map +1 -0
- package/dist/components/Modal/types.d.ts +29 -0
- package/dist/components/Pressable/index.d.ts +5 -0
- package/dist/components/Pressable/index.js +106 -0
- package/dist/components/Pressable/index.js.map +1 -0
- package/dist/components/Pressable/types.d.ts +44 -0
- package/dist/components/RefreshControl/index.d.ts +5 -0
- package/dist/components/RefreshControl/index.js +176 -0
- package/dist/components/RefreshControl/index.js.map +1 -0
- package/dist/components/RefreshControl/pull.js +16 -0
- package/dist/components/RefreshControl/pull.js.map +1 -0
- package/dist/components/RefreshControl/types.d.ts +14 -0
- package/dist/components/ScrollView/index.d.ts +5 -0
- package/dist/components/ScrollView/index.js +318 -0
- package/dist/components/ScrollView/index.js.map +1 -0
- package/dist/components/ScrollView/snap.js +38 -0
- package/dist/components/ScrollView/snap.js.map +1 -0
- package/dist/components/ScrollView/types.d.ts +115 -0
- package/dist/components/StatusBar/index.d.ts +26 -0
- package/dist/components/StatusBar/index.js +113 -0
- package/dist/components/StatusBar/index.js.map +1 -0
- package/dist/components/Switch/index.d.ts +5 -0
- package/dist/components/Switch/index.js +89 -0
- package/dist/components/Switch/index.js.map +1 -0
- package/dist/components/Switch/types.d.ts +25 -0
- package/dist/components/Text/TextAncestorContext.d.ts +3 -0
- package/dist/components/Text/TextAncestorContext.js +6 -0
- package/dist/components/Text/TextAncestorContext.js.map +1 -0
- package/dist/components/Text/index.d.ts +6 -0
- package/dist/components/Text/index.js +133 -0
- package/dist/components/Text/index.js.map +1 -0
- package/dist/components/Text/types.d.ts +38 -0
- package/dist/components/TextInput/autocomplete.js +102 -0
- package/dist/components/TextInput/autocomplete.js.map +1 -0
- package/dist/components/TextInput/index.d.ts +8 -0
- package/dist/components/TextInput/index.js +290 -0
- package/dist/components/TextInput/index.js.map +1 -0
- package/dist/components/TextInput/types.d.ts +130 -0
- package/dist/components/TouchableHighlight/index.d.ts +5 -0
- package/dist/components/TouchableHighlight/index.js +109 -0
- package/dist/components/TouchableHighlight/index.js.map +1 -0
- package/dist/components/TouchableHighlight/types.d.ts +15 -0
- package/dist/components/TouchableNativeFeedback/index.d.ts +11 -0
- package/dist/components/TouchableNativeFeedback/index.js +29 -0
- package/dist/components/TouchableNativeFeedback/index.js.map +1 -0
- package/dist/components/TouchableNativeFeedback/types.d.ts +19 -0
- package/dist/components/TouchableOpacity/index.d.ts +5 -0
- package/dist/components/TouchableOpacity/index.js +122 -0
- package/dist/components/TouchableOpacity/index.js.map +1 -0
- package/dist/components/TouchableOpacity/types.d.ts +11 -0
- package/dist/components/TouchableWithoutFeedback/index.d.ts +5 -0
- package/dist/components/TouchableWithoutFeedback/index.js +70 -0
- package/dist/components/TouchableWithoutFeedback/index.js.map +1 -0
- package/dist/components/TouchableWithoutFeedback/types.d.ts +40 -0
- package/dist/components/View/index.d.ts +5 -0
- package/dist/components/View/index.js +50 -0
- package/dist/components/View/index.js.map +1 -0
- package/dist/components/View/types.d.ts +98 -0
- package/dist/dom/Head.d.ts +21 -0
- package/dist/dom/Head.js +53 -0
- package/dist/dom/Head.js.map +1 -0
- package/dist/dom/index.d.ts +2 -22
- package/dist/dom/index.js +1 -53
- package/dist/events/press/Pressability.d.ts +2 -0
- package/dist/events/press/Pressability.js +366 -0
- package/dist/events/press/Pressability.js.map +1 -0
- package/dist/events/press/cursorStyle.js +5 -0
- package/dist/events/press/cursorStyle.js.map +1 -0
- package/dist/events/press/index.d.ts +4 -0
- package/dist/events/press/mergeHandlers.js +30 -0
- package/dist/events/press/mergeHandlers.js.map +1 -0
- package/dist/events/press/types.d.ts +48 -0
- package/dist/events/press/usePressability.d.ts +4 -0
- package/dist/events/press/usePressability.js +18 -0
- package/dist/events/press/usePressability.js.map +1 -0
- package/dist/events/responderEvent.js +50 -0
- package/dist/events/responderEvent.js.map +1 -0
- package/dist/events/types.d.ts +33 -0
- package/dist/index.d.ts +66 -1009
- package/dist/index.js +43 -3872
- package/dist/reactNativeVersion.d.ts +7 -0
- package/dist/reactNativeVersion.js +10 -0
- package/dist/reactNativeVersion.js.map +1 -0
- package/dist/render/domProps.js +66 -0
- package/dist/render/domProps.js.map +1 -0
- package/dist/render/hitSlop.js +21 -0
- package/dist/render/hitSlop.js.map +1 -0
- package/dist/render/hostMethods.js +44 -0
- package/dist/render/hostMethods.js.map +1 -0
- package/dist/render/resolveTag.js +64 -0
- package/dist/render/resolveTag.js.map +1 -0
- package/dist/render/useElementLayout.js +25 -0
- package/dist/render/useElementLayout.js.map +1 -0
- package/dist/server/index.d.ts +2 -23
- package/dist/server/index.js +8 -4
- package/dist/server/index.js.map +1 -1
- package/dist/style/StyleRegistryContext.js +8 -0
- package/dist/style/StyleRegistryContext.js.map +1 -0
- package/dist/style/color.js +16 -0
- package/dist/style/color.js.map +1 -0
- package/dist/style/compile.js +72 -0
- package/dist/style/compile.js.map +1 -0
- package/dist/style/direction.js +33 -0
- package/dist/style/direction.js.map +1 -0
- package/dist/style/index.d.ts +25 -0
- package/dist/style/index.js +38 -0
- package/dist/style/index.js.map +1 -0
- package/dist/style/registry.d.ts +27 -0
- package/dist/{StyleRegistryContext-CAD45r4n.js → style/registry.js} +32 -10
- package/dist/style/registry.js.map +1 -0
- package/dist/style/resolveStyle.js +181 -0
- package/dist/style/resolveStyle.js.map +1 -0
- package/dist/style/shadow.js +87 -0
- package/dist/style/shadow.js.map +1 -0
- package/dist/style/splitLayoutProps.js +52 -0
- package/dist/style/splitLayoutProps.js.map +1 -0
- package/dist/style/transform.js +35 -0
- package/dist/style/transform.js.map +1 -0
- package/dist/style/types.d.ts +4 -0
- package/dist/style/units.js +21 -0
- package/dist/style/units.js.map +1 -0
- package/dist/style/webStyle.js +9 -0
- package/dist/style/webStyle.js.map +1 -0
- package/dist/{env-DK5Mt9yX.js → utils/env.js} +2 -2
- package/dist/utils/env.js.map +1 -0
- package/dist/utils/scrollLock.js +28 -0
- package/dist/utils/scrollLock.js.map +1 -0
- package/dist/utils/warn.js +11 -0
- package/dist/utils/warn.js.map +1 -0
- package/dist/vite/index.d.ts +9 -1
- package/dist/vite/index.js +71 -174
- package/dist/vite/index.js.map +1 -1
- package/llms.txt +203 -22
- package/package.json +16 -3
- package/dist/StyleRegistryContext-CAD45r4n.js.map +0 -1
- package/dist/dom/index.js.map +0 -1
- package/dist/env-DK5Mt9yX.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/types-BxTWbdj9.d.ts +0 -5
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// src/utils/scrollLock.ts
|
|
2
|
+
let locks = 0;
|
|
3
|
+
let pageOverflow = "";
|
|
4
|
+
let pageX = 0;
|
|
5
|
+
let pageY = 0;
|
|
6
|
+
const keepPlace = () => {
|
|
7
|
+
const viewport = window.visualViewport;
|
|
8
|
+
if (viewport !== null && viewport.scale === 1 && viewport.offsetTop > 0) window.scrollTo(pageX, pageY);
|
|
9
|
+
};
|
|
10
|
+
const lockScroll = () => {
|
|
11
|
+
const root = document.documentElement;
|
|
12
|
+
if (locks++ === 0) {
|
|
13
|
+
pageOverflow = root.style.overflow;
|
|
14
|
+
pageX = window.scrollX;
|
|
15
|
+
pageY = window.scrollY;
|
|
16
|
+
root.style.overflow = "hidden";
|
|
17
|
+
window.visualViewport?.addEventListener("scroll", keepPlace);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
const unlockScroll = () => {
|
|
21
|
+
if (--locks === 0) {
|
|
22
|
+
document.documentElement.style.overflow = pageOverflow;
|
|
23
|
+
window.visualViewport?.removeEventListener("scroll", keepPlace);
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
export { lockScroll, unlockScroll };
|
|
27
|
+
|
|
28
|
+
//# sourceMappingURL=scrollLock.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scrollLock.js","names":[],"sources":["../../src/utils/scrollLock.ts"],"sourcesContent":["// The page under a modal or an alert doesn't scroll, as on native. A counter, so the last one to\n// close gives the page its scroll back.\nlet locks = 0\nlet pageOverflow = ''\nlet pageX = 0\nlet pageY = 0\n\n// Safari on iOS moves the visual viewport down a locked page to show a focused field above the\n// keyboard, so the top of a modal leaves the screen. A scroll to the place of the page at the\n// lock brings the viewport back. A pinch zoom moves the viewport too, and keeps its place.\nconst keepPlace = (): void => {\n const viewport = window.visualViewport\n if (viewport !== null && viewport.scale === 1 && viewport.offsetTop > 0) {\n window.scrollTo(pageX, pageY)\n }\n}\n\nexport const lockScroll = (): void => {\n const root = document.documentElement\n if (locks++ === 0) {\n pageOverflow = root.style.overflow\n pageX = window.scrollX\n pageY = window.scrollY\n root.style.overflow = 'hidden'\n window.visualViewport?.addEventListener('scroll', keepPlace)\n }\n}\n\nexport const unlockScroll = (): void => {\n if (--locks === 0) {\n document.documentElement.style.overflow = pageOverflow\n window.visualViewport?.removeEventListener('scroll', keepPlace)\n }\n}\n"],"mappings":";AAEA,IAAI,QAAQ;AACZ,IAAI,eAAe;AACnB,IAAI,QAAQ;AACZ,IAAI,QAAQ;AAKZ,MAAM,kBAAwB;CAC5B,MAAM,WAAW,OAAO;CACxB,IAAI,aAAa,QAAQ,SAAS,UAAU,KAAK,SAAS,YAAY,GACpE,OAAO,SAAS,OAAO,KAAK;AAEhC;AAEA,MAAa,mBAAyB;CACpC,MAAM,OAAO,SAAS;CACtB,IAAI,YAAY,GAAG;EACjB,eAAe,KAAK,MAAM;EAC1B,QAAQ,OAAO;EACf,QAAQ,OAAO;EACf,KAAK,MAAM,WAAW;EACtB,OAAO,gBAAgB,iBAAiB,UAAU,SAAS;CAC7D;AACF;AAEA,MAAa,qBAA2B;CACtC,IAAI,EAAE,UAAU,GAAG;EACjB,SAAS,gBAAgB,MAAM,WAAW;EAC1C,OAAO,gBAAgB,oBAAoB,UAAU,SAAS;CAChE;AACF"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { isProductionEnv } from "./env.js";
|
|
2
|
+
// src/utils/warn.ts
|
|
3
|
+
const warned = /* @__PURE__ */ new Set();
|
|
4
|
+
const warnOnce = (key, message) => {
|
|
5
|
+
if (isProductionEnv() || warned.has(key)) return;
|
|
6
|
+
warned.add(key);
|
|
7
|
+
console.warn(`[@rootnative/web] ${message}`);
|
|
8
|
+
};
|
|
9
|
+
export { warnOnce };
|
|
10
|
+
|
|
11
|
+
//# sourceMappingURL=warn.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"warn.js","names":[],"sources":["../../src/utils/warn.ts"],"sourcesContent":["import { isProductionEnv } from './env'\n\nconst warned = new Set<string>()\n\n// A development warning that prints once per key.\nexport const warnOnce = (key: string, message: string): void => {\n if (isProductionEnv() || warned.has(key)) return\n warned.add(key)\n console.warn(`[@rootnative/web] ${message}`)\n}\n\n// For tests.\nexport const resetWarnings = (): void => warned.clear()\n"],"mappings":";;AAEA,MAAM,yBAAS,IAAI,IAAY;AAG/B,MAAa,YAAY,KAAa,YAA0B;CAC9D,IAAI,gBAAgB,KAAK,OAAO,IAAI,GAAG,GAAG;CAC1C,OAAO,IAAI,GAAG;CACd,QAAQ,KAAK,qBAAqB,SAAS;AAC7C"}
|
package/dist/vite/index.d.ts
CHANGED
|
@@ -1,7 +1,15 @@
|
|
|
1
1
|
import { Plugin } from "vite";
|
|
2
|
+
export type RootnativeOptions = {
|
|
3
|
+
/**
|
|
4
|
+
* Runs the Babel plugin of `react-native-worklets` on your code and on the libraries that
|
|
5
|
+
* have worklets. Reanimated needs it, and so does a library built on Reanimated. The app
|
|
6
|
+
* must have `react-native-worklets` and `@babel/core`. Default: `false`.
|
|
7
|
+
*/
|
|
8
|
+
worklets?: boolean | undefined;
|
|
9
|
+
};
|
|
2
10
|
/**
|
|
3
11
|
* The Vite plugin. It points `react-native` at `@rootnative/web`, resolves `.web.*` files
|
|
4
12
|
* first, defines `__DEV__`, and replaces `Platform.OS` with `'web'`.
|
|
5
13
|
*/
|
|
6
|
-
export declare const rootnative: () => Plugin;
|
|
14
|
+
export declare const rootnative: (options?: RootnativeOptions) => Plugin;
|
|
7
15
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/vite/index.js
CHANGED
|
@@ -1,138 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const LINE_BREAK = /[\n\r\u2028\u2029]/;
|
|
6
|
-
const isNode = (value) => typeof value === "object" && value !== null && typeof value.type === "string";
|
|
7
|
-
const identifierName = (node) => isNode(node) && node.type === "Identifier" && typeof node.name === "string" ? node.name : void 0;
|
|
8
|
-
const nodes = (value) => Array.isArray(value) ? value.filter(isNode) : isNode(value) ? [value] : [];
|
|
9
|
-
const collectBindings = (pattern, names) => {
|
|
10
|
-
if (!isNode(pattern)) return;
|
|
11
|
-
switch (pattern.type) {
|
|
12
|
-
case "Identifier": {
|
|
13
|
-
const name = identifierName(pattern);
|
|
14
|
-
if (name !== void 0) names.add(name);
|
|
15
|
-
return;
|
|
16
|
-
}
|
|
17
|
-
case "ObjectPattern":
|
|
18
|
-
for (const property of nodes(pattern.properties)) collectBindings(property.type === "RestElement" ? property.argument : property.value, names);
|
|
19
|
-
return;
|
|
20
|
-
case "ArrayPattern":
|
|
21
|
-
for (const element of nodes(pattern.elements)) collectBindings(element, names);
|
|
22
|
-
return;
|
|
23
|
-
case "RestElement":
|
|
24
|
-
collectBindings(pattern.argument, names);
|
|
25
|
-
return;
|
|
26
|
-
case "AssignmentPattern": collectBindings(pattern.left, names);
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
const isWriteTarget = (parent, key, grandparent) => {
|
|
30
|
-
switch (parent.type) {
|
|
31
|
-
case "AssignmentExpression":
|
|
32
|
-
case "AssignmentPattern":
|
|
33
|
-
case "ForInStatement":
|
|
34
|
-
case "ForOfStatement": return key === "left";
|
|
35
|
-
case "UpdateExpression":
|
|
36
|
-
case "ArrayPattern":
|
|
37
|
-
case "RestElement": return true;
|
|
38
|
-
case "UnaryExpression": return parent.operator === "delete";
|
|
39
|
-
case "Property": return key === "value" && grandparent?.type === "ObjectPattern";
|
|
40
|
-
default: return false;
|
|
41
|
-
}
|
|
42
|
-
};
|
|
43
|
-
const platformRead = (node) => {
|
|
44
|
-
if (node.computed === true || identifierName(node.property) !== "OS") return void 0;
|
|
45
|
-
const name = identifierName(node.object);
|
|
46
|
-
if (name !== void 0) return {
|
|
47
|
-
name,
|
|
48
|
-
namespace: false
|
|
49
|
-
};
|
|
50
|
-
const object = node.object;
|
|
51
|
-
if (isNode(object) && object.type === "MemberExpression" && object.computed !== true && identifierName(object.property) === "Platform") {
|
|
52
|
-
const namespace = identifierName(object.object);
|
|
53
|
-
if (namespace !== void 0) return {
|
|
54
|
-
name: namespace,
|
|
55
|
-
namespace: true
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
};
|
|
59
|
-
/**
|
|
60
|
-
* Replaces each read of `Platform.OS` with `"web"`, so a minifier can drop the branches of the
|
|
61
|
-
* other platforms. Only a `Platform` imported from `react-native` or `@rootnative/web` counts.
|
|
62
|
-
* Takes the ESTree program of `code`, with `start` and `end` offsets. Returns `null` when
|
|
63
|
-
* nothing changes.
|
|
64
|
-
*/
|
|
65
|
-
const inlinePlatformOS = (code, program) => {
|
|
66
|
-
if (!isNode(program)) return null;
|
|
67
|
-
const named = /* @__PURE__ */ new Set();
|
|
68
|
-
const namespaces = /* @__PURE__ */ new Set();
|
|
69
|
-
const declared = /* @__PURE__ */ new Set();
|
|
70
|
-
const reads = [];
|
|
71
|
-
const visit = (node, parent, key, grandparent) => {
|
|
72
|
-
switch (node.type) {
|
|
73
|
-
case "ImportDeclaration": {
|
|
74
|
-
const source = isNode(node.source) ? node.source.value : void 0;
|
|
75
|
-
if (typeof source !== "string" || !SOURCES.has(source)) break;
|
|
76
|
-
for (const specifier of nodes(node.specifiers)) {
|
|
77
|
-
const local = identifierName(specifier.local);
|
|
78
|
-
if (local === void 0) continue;
|
|
79
|
-
if (specifier.type === "ImportNamespaceSpecifier") namespaces.add(local);
|
|
80
|
-
const imported = isNode(specifier.imported) ? identifierName(specifier.imported) ?? specifier.imported.value : void 0;
|
|
81
|
-
if (specifier.type === "ImportSpecifier" && imported === "Platform") named.add(local);
|
|
82
|
-
}
|
|
83
|
-
break;
|
|
84
|
-
}
|
|
85
|
-
case "VariableDeclarator":
|
|
86
|
-
collectBindings(node.id, declared);
|
|
87
|
-
break;
|
|
88
|
-
case "FunctionDeclaration":
|
|
89
|
-
case "FunctionExpression":
|
|
90
|
-
case "ArrowFunctionExpression":
|
|
91
|
-
collectBindings(node.id, declared);
|
|
92
|
-
for (const param of nodes(node.params)) collectBindings(param, declared);
|
|
93
|
-
break;
|
|
94
|
-
case "ClassDeclaration":
|
|
95
|
-
case "ClassExpression":
|
|
96
|
-
collectBindings(node.id, declared);
|
|
97
|
-
break;
|
|
98
|
-
case "CatchClause":
|
|
99
|
-
collectBindings(node.param, declared);
|
|
100
|
-
break;
|
|
101
|
-
case "MemberExpression": {
|
|
102
|
-
const read = platformRead(node);
|
|
103
|
-
if (read !== void 0 && (parent === void 0 || !isWriteTarget(parent, key, grandparent))) reads.push({
|
|
104
|
-
...read,
|
|
105
|
-
start: node.start,
|
|
106
|
-
end: node.end
|
|
107
|
-
});
|
|
108
|
-
break;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
for (const childKey of Object.keys(node)) for (const child of nodes(node[childKey])) visit(child, node, childKey, parent);
|
|
112
|
-
};
|
|
113
|
-
visit(program, void 0, "", void 0);
|
|
114
|
-
const edits = reads.filter((read) => (read.namespace ? namespaces : named).has(read.name) && !declared.has(read.name) && read.end - read.start >= 5 && !LINE_BREAK.test(code.slice(read.start, read.end)));
|
|
115
|
-
if (edits.length === 0) return null;
|
|
116
|
-
let output = code;
|
|
117
|
-
for (const { start, end } of edits) output = output.slice(0, start) + REPLACEMENT.padEnd(end - start) + output.slice(end);
|
|
118
|
-
return output;
|
|
119
|
-
};
|
|
120
|
-
// src/bundler/index.ts
|
|
121
|
-
/** Module names that resolve to this package. Each one matches the exact name only. */
|
|
122
|
-
const ALIASES = { "react-native": "@rootnative/web" };
|
|
123
|
-
/**
|
|
124
|
-
* The globals that React Native code expects from its bundler, each as the code that replaces
|
|
125
|
-
* the name. `__DEV__` is `true` in a development build and `false` in a production build.
|
|
126
|
-
*/
|
|
127
|
-
const globalDefines = (production) => ({ __DEV__: JSON.stringify(!production) });
|
|
128
|
-
/**
|
|
129
|
-
* Puts a `.web` variant of each extension first, as Metro does for the web platform. An
|
|
130
|
-
* import of `./Button` then finds `Button.web.tsx` before `Button.tsx`.
|
|
131
|
-
*/
|
|
132
|
-
const webExtensions = (extensions) => {
|
|
133
|
-
const web = extensions.map((extension) => extension.startsWith(".web.") ? extension : `.web${extension}`);
|
|
134
|
-
return [.../* @__PURE__ */ new Set([...web, ...extensions])];
|
|
135
|
-
};
|
|
1
|
+
import { ALIASES, globalDefines, webExtensions } from "../bundler/index.js";
|
|
2
|
+
import { inlinePlatformOS } from "../bundler/inlinePlatformOS.js";
|
|
3
|
+
import { WORKLET_CODE, createWorkletTransform } from "../bundler/worklets.js";
|
|
4
|
+
import { isProductionEnv } from "../utils/env.js";
|
|
136
5
|
// src/vite/index.ts
|
|
137
6
|
const VITE_DEFAULT_EXTENSIONS = [
|
|
138
7
|
".mjs",
|
|
@@ -155,52 +24,80 @@ const aliasArray = (alias) => {
|
|
|
155
24
|
}));
|
|
156
25
|
};
|
|
157
26
|
const defineCode = (value) => typeof value === "string" ? value : JSON.stringify(value);
|
|
27
|
+
const PLATFORM_CODE = /\bPlatform\b/;
|
|
158
28
|
/**
|
|
159
29
|
* The Vite plugin. It points `react-native` at `@rootnative/web`, resolves `.web.*` files
|
|
160
30
|
* first, defines `__DEV__`, and replaces `Platform.OS` with `'web'`.
|
|
161
31
|
*/
|
|
162
|
-
const rootnative = () =>
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
config
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
},
|
|
184
|
-
transform: {
|
|
185
|
-
filter: {
|
|
186
|
-
id: SCRIPT_ID,
|
|
187
|
-
code: /\bPlatform\b/
|
|
188
|
-
},
|
|
189
|
-
handler(code) {
|
|
190
|
-
let program;
|
|
191
|
-
try {
|
|
192
|
-
program = this.parse(code);
|
|
193
|
-
} catch {
|
|
194
|
-
return null;
|
|
32
|
+
const rootnative = (options = {}) => {
|
|
33
|
+
let workletize;
|
|
34
|
+
return {
|
|
35
|
+
name: "@rootnative/web",
|
|
36
|
+
config(config) {
|
|
37
|
+
if (options.worklets === true) {
|
|
38
|
+
const transform = createWorkletTransform(config.root ?? process.cwd());
|
|
39
|
+
workletize = transform;
|
|
40
|
+
config.optimizeDeps ??= {};
|
|
41
|
+
config.optimizeDeps.rolldownOptions ??= {};
|
|
42
|
+
const rolldownOptions = config.optimizeDeps.rolldownOptions;
|
|
43
|
+
rolldownOptions.plugins = [rolldownOptions.plugins, {
|
|
44
|
+
name: "@rootnative/web:worklets",
|
|
45
|
+
transform: {
|
|
46
|
+
filter: {
|
|
47
|
+
id: SCRIPT_ID,
|
|
48
|
+
code: WORKLET_CODE
|
|
49
|
+
},
|
|
50
|
+
handler: transform
|
|
51
|
+
}
|
|
52
|
+
}];
|
|
195
53
|
}
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
54
|
+
config.resolve ??= {};
|
|
55
|
+
config.resolve.alias = [...aliasArray(config.resolve.alias), ...Object.entries(ALIASES).map(([name, replacement]) => ({
|
|
56
|
+
find: new RegExp(`^${escapeRegExp(name)}$`),
|
|
57
|
+
replacement
|
|
58
|
+
}))];
|
|
59
|
+
const extensions = webExtensions(config.resolve.extensions ?? VITE_DEFAULT_EXTENSIONS);
|
|
60
|
+
config.resolve.extensions = extensions;
|
|
61
|
+
const defines = Object.fromEntries(Object.entries(globalDefines(isProductionEnv())).map(([name, code]) => [name, config.define?.[name] === void 0 ? code : defineCode(config.define[name])]));
|
|
62
|
+
config.define = {
|
|
63
|
+
...config.define,
|
|
64
|
+
...defines
|
|
200
65
|
};
|
|
66
|
+
config.optimizeDeps ??= {};
|
|
67
|
+
config.optimizeDeps.rolldownOptions ??= {};
|
|
68
|
+
config.optimizeDeps.rolldownOptions.transform ??= {};
|
|
69
|
+
config.optimizeDeps.rolldownOptions.transform.define = {
|
|
70
|
+
...defines,
|
|
71
|
+
...config.optimizeDeps.rolldownOptions.transform.define
|
|
72
|
+
};
|
|
73
|
+
config.optimizeDeps.rolldownOptions.resolve ??= {};
|
|
74
|
+
config.optimizeDeps.rolldownOptions.resolve.extensions ??= extensions;
|
|
75
|
+
},
|
|
76
|
+
transform: {
|
|
77
|
+
filter: {
|
|
78
|
+
id: SCRIPT_ID,
|
|
79
|
+
code: options.worklets === true ? [PLATFORM_CODE, WORKLET_CODE] : PLATFORM_CODE
|
|
80
|
+
},
|
|
81
|
+
async handler(code, id) {
|
|
82
|
+
const worklets = workletize === void 0 ? null : await workletize(code, id);
|
|
83
|
+
const current = worklets?.code ?? code;
|
|
84
|
+
if (!PLATFORM_CODE.test(current)) return worklets;
|
|
85
|
+
let program;
|
|
86
|
+
try {
|
|
87
|
+
program = this.parse(current);
|
|
88
|
+
} catch {
|
|
89
|
+
return worklets;
|
|
90
|
+
}
|
|
91
|
+
const inlined = inlinePlatformOS(current, program);
|
|
92
|
+
if (inlined === null) return worklets;
|
|
93
|
+
return {
|
|
94
|
+
code: inlined,
|
|
95
|
+
map: worklets?.map ?? null
|
|
96
|
+
};
|
|
97
|
+
}
|
|
201
98
|
}
|
|
202
|
-
}
|
|
203
|
-
}
|
|
99
|
+
};
|
|
100
|
+
};
|
|
204
101
|
export { rootnative };
|
|
205
102
|
|
|
206
103
|
//# sourceMappingURL=index.js.map
|
package/dist/vite/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../../src/bundler/inlinePlatformOS.ts","../../src/bundler/index.ts","../../src/vite/index.ts"],"sourcesContent":["interface AstNode {\n type: string\n start: number\n end: number\n [key: string]: unknown\n}\n\ninterface PlatformRead {\n start: number\n end: number\n name: string\n namespace: boolean\n}\n\nconst SOURCES = new Set(['react-native', '@rootnative/web'])\nconst REPLACEMENT = '\"web\"'\nconst LINE_BREAK = /[\\n\\r\\u2028\\u2029]/\n\nconst isNode = (value: unknown): value is AstNode =>\n typeof value === 'object' &&\n value !== null &&\n typeof (value as { type?: unknown }).type === 'string'\n\nconst identifierName = (node: unknown): string | undefined =>\n isNode(node) && node.type === 'Identifier' && typeof node.name === 'string'\n ? node.name\n : undefined\n\nconst nodes = (value: unknown): AstNode[] =>\n Array.isArray(value) ? value.filter(isNode) : isNode(value) ? [value] : []\n\nconst collectBindings = (pattern: unknown, names: Set<string>): void => {\n if (!isNode(pattern)) return\n switch (pattern.type) {\n case 'Identifier': {\n const name = identifierName(pattern)\n if (name !== undefined) names.add(name)\n return\n }\n case 'ObjectPattern':\n for (const property of nodes(pattern.properties)) {\n collectBindings(property.type === 'RestElement' ? property.argument : property.value, names)\n }\n return\n case 'ArrayPattern':\n for (const element of nodes(pattern.elements)) collectBindings(element, names)\n return\n case 'RestElement':\n collectBindings(pattern.argument, names)\n return\n case 'AssignmentPattern':\n collectBindings(pattern.left, names)\n }\n}\n\nconst isWriteTarget = (parent: AstNode, key: string, grandparent: AstNode | undefined): boolean => {\n switch (parent.type) {\n case 'AssignmentExpression':\n case 'AssignmentPattern':\n case 'ForInStatement':\n case 'ForOfStatement':\n return key === 'left'\n case 'UpdateExpression':\n case 'ArrayPattern':\n case 'RestElement':\n return true\n case 'UnaryExpression':\n return parent.operator === 'delete'\n case 'Property':\n return key === 'value' && grandparent?.type === 'ObjectPattern'\n default:\n return false\n }\n}\n\nconst platformRead = (node: AstNode): Omit<PlatformRead, 'start' | 'end'> | undefined => {\n if (node.computed === true || identifierName(node.property) !== 'OS') return undefined\n const name = identifierName(node.object)\n if (name !== undefined) return { name, namespace: false }\n const object = node.object\n if (\n isNode(object) &&\n object.type === 'MemberExpression' &&\n object.computed !== true &&\n identifierName(object.property) === 'Platform'\n ) {\n const namespace = identifierName(object.object)\n if (namespace !== undefined) return { name: namespace, namespace: true }\n }\n return undefined\n}\n\n/**\n * Replaces each read of `Platform.OS` with `\"web\"`, so a minifier can drop the branches of the\n * other platforms. Only a `Platform` imported from `react-native` or `@rootnative/web` counts.\n * Takes the ESTree program of `code`, with `start` and `end` offsets. Returns `null` when\n * nothing changes.\n */\nexport const inlinePlatformOS = (code: string, program: object): string | null => {\n if (!isNode(program)) return null\n\n const named = new Set<string>()\n const namespaces = new Set<string>()\n const declared = new Set<string>()\n const reads: PlatformRead[] = []\n\n const visit = (\n node: AstNode,\n parent: AstNode | undefined,\n key: string,\n grandparent: AstNode | undefined,\n ): void => {\n switch (node.type) {\n case 'ImportDeclaration': {\n const source = isNode(node.source) ? node.source.value : undefined\n if (typeof source !== 'string' || !SOURCES.has(source)) break\n for (const specifier of nodes(node.specifiers)) {\n const local = identifierName(specifier.local)\n if (local === undefined) continue\n if (specifier.type === 'ImportNamespaceSpecifier') namespaces.add(local)\n const imported = isNode(specifier.imported)\n ? (identifierName(specifier.imported) ?? specifier.imported.value)\n : undefined\n if (specifier.type === 'ImportSpecifier' && imported === 'Platform') named.add(local)\n }\n break\n }\n case 'VariableDeclarator':\n collectBindings(node.id, declared)\n break\n case 'FunctionDeclaration':\n case 'FunctionExpression':\n case 'ArrowFunctionExpression':\n collectBindings(node.id, declared)\n for (const param of nodes(node.params)) collectBindings(param, declared)\n break\n case 'ClassDeclaration':\n case 'ClassExpression':\n collectBindings(node.id, declared)\n break\n case 'CatchClause':\n collectBindings(node.param, declared)\n break\n case 'MemberExpression': {\n const read = platformRead(node)\n if (\n read !== undefined &&\n (parent === undefined || !isWriteTarget(parent, key, grandparent))\n ) {\n reads.push({ ...read, start: node.start, end: node.end })\n }\n break\n }\n }\n for (const childKey of Object.keys(node)) {\n for (const child of nodes(node[childKey])) visit(child, node, childKey, parent)\n }\n }\n visit(program, undefined, '', undefined)\n\n // A local binding with the same name may shadow the import in some scope. Without scope\n // analysis, skip that name in the whole module.\n const edits = reads.filter(\n (read) =>\n (read.namespace ? namespaces : named).has(read.name) &&\n !declared.has(read.name) &&\n // The replacement keeps the length and the lines of the original, so every later column\n // stays put and the source map stays exact. A shorter or a multi-line read keeps its code.\n read.end - read.start >= REPLACEMENT.length &&\n !LINE_BREAK.test(code.slice(read.start, read.end)),\n )\n if (edits.length === 0) return null\n\n // Each edit keeps its length, so the offsets of the others stay valid in any order.\n let output = code\n for (const { start, end } of edits) {\n output = output.slice(0, start) + REPLACEMENT.padEnd(end - start) + output.slice(end)\n }\n return output\n}\n","export { inlinePlatformOS } from './inlinePlatformOS'\n\n/** Module names that resolve to this package. Each one matches the exact name only. */\nexport const ALIASES: Readonly<Record<string, string>> = {\n 'react-native': '@rootnative/web',\n}\n\n/**\n * The globals that React Native code expects from its bundler, each as the code that replaces\n * the name. `__DEV__` is `true` in a development build and `false` in a production build.\n */\nexport const globalDefines = (production: boolean): Record<string, string> => ({\n __DEV__: JSON.stringify(!production),\n})\n\n/**\n * Puts a `.web` variant of each extension first, as Metro does for the web platform. An\n * import of `./Button` then finds `Button.web.tsx` before `Button.tsx`.\n */\nexport const webExtensions = (extensions: readonly string[]): string[] => {\n const web = extensions.map((extension) =>\n extension.startsWith('.web.') ? extension : `.web${extension}`,\n )\n return [...new Set([...web, ...extensions])]\n}\n","import type { Alias, AliasOptions, Plugin } from 'vite'\n\nimport { ALIASES, globalDefines, inlinePlatformOS, webExtensions } from '../bundler'\nimport { isProductionEnv } from '../utils/env'\n\n// Vite's default `resolve.extensions`. A config that sets the option replaces this list, so the\n// plugin starts from the same one.\nconst VITE_DEFAULT_EXTENSIONS = ['.mjs', '.js', '.mts', '.ts', '.jsx', '.tsx', '.json']\n\nconst SCRIPT_ID = /\\.[cm]?[jt]sx?(?:$|\\?)/\n\nconst escapeRegExp = (value: string): string => value.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&')\n\nconst isAliasArray = (alias: AliasOptions): alias is readonly Alias[] => Array.isArray(alias)\n\nconst aliasArray = (alias: AliasOptions | undefined): Alias[] => {\n if (alias === undefined) return []\n if (isAliasArray(alias)) return [...alias]\n return Object.entries(alias).map(([find, replacement]) => ({ find, replacement }))\n}\n\nconst defineCode = (value: unknown): string =>\n typeof value === 'string' ? value : JSON.stringify(value)\n\n/**\n * The Vite plugin. It points `react-native` at `@rootnative/web`, resolves `.web.*` files\n * first, defines `__DEV__`, and replaces `Platform.OS` with `'web'`.\n */\nexport const rootnative = (): Plugin => ({\n name: '@rootnative/web',\n\n // Edits the config in place, so the app's own values win. A returned partial config would\n // append the lists after the app's entries and override its defines.\n config(config) {\n config.resolve ??= {}\n // Last, so an alias in the app's config wins. The first matching alias resolves an import.\n config.resolve.alias = [\n ...aliasArray(config.resolve.alias),\n ...Object.entries(ALIASES).map(([name, replacement]) => ({\n find: new RegExp(`^${escapeRegExp(name)}$`),\n replacement,\n })),\n ]\n config.resolve.extensions = webExtensions(config.resolve.extensions ?? VITE_DEFAULT_EXTENSIONS)\n\n // Vite has set NODE_ENV before this hook runs, so `__DEV__` follows React's development mode.\n const defines = Object.fromEntries(\n Object.entries(globalDefines(isProductionEnv())).map(([name, code]) => [\n name,\n config.define?.[name] === undefined ? code : defineCode(config.define[name]),\n ]),\n )\n config.define = { ...config.define, ...defines }\n // The dep optimizer applies only its own defines, so a pre-bundled library gets them here.\n config.optimizeDeps ??= {}\n config.optimizeDeps.rolldownOptions ??= {}\n config.optimizeDeps.rolldownOptions.transform ??= {}\n config.optimizeDeps.rolldownOptions.transform.define = {\n ...defines,\n ...config.optimizeDeps.rolldownOptions.transform.define,\n }\n },\n\n transform: {\n filter: { id: SCRIPT_ID, code: /\\bPlatform\\b/ },\n handler(code) {\n let program: object\n try {\n program = this.parse(code)\n } catch {\n // Not plain JavaScript yet. The module keeps its code, and a later step reports a real\n // syntax error.\n return null\n }\n const inlined = inlinePlatformOS(code, program)\n return inlined === null ? null : { code: inlined, map: null }\n },\n },\n})\n"],"mappings":";;AAcA,MAAM,0BAAU,IAAI,IAAI,CAAC,gBAAgB,iBAAiB,CAAC;AAC3D,MAAM,cAAc;AACpB,MAAM,aAAa;AAEnB,MAAM,UAAU,UACd,OAAO,UAAU,YACjB,UAAU,QACV,OAAQ,MAA6B,SAAS;AAEhD,MAAM,kBAAkB,SACtB,OAAO,IAAI,KAAK,KAAK,SAAS,gBAAgB,OAAO,KAAK,SAAS,WAC/D,KAAK,OACL,KAAA;AAEN,MAAM,SAAS,UACb,MAAM,QAAQ,KAAK,I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AC3B,QAAQ;IAGN,OAAO;GACT;GACA,MAAM,UAAU,iBAAiB,MAAM,OAAO;GAC9C,OAAO,YAAY,OAAO,OAAO;IAAE,MAAM;IAAS,KAAK;GAAK;EAC9D;CACF;AACF"}
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../src/vite/index.ts"],"sourcesContent":["import type { Alias, AliasOptions, Plugin } from 'vite'\n\nimport {\n ALIASES,\n createWorkletTransform,\n globalDefines,\n inlinePlatformOS,\n WORKLET_CODE,\n webExtensions,\n type WorkletTransform,\n} from '../bundler'\nimport { isProductionEnv } from '../utils/env'\n\n// Vite's default `resolve.extensions`. A config that sets the option replaces this list, so the\n// plugin starts from the same one.\nconst VITE_DEFAULT_EXTENSIONS = ['.mjs', '.js', '.mts', '.ts', '.jsx', '.tsx', '.json']\n\nconst SCRIPT_ID = /\\.[cm]?[jt]sx?(?:$|\\?)/\n\nconst escapeRegExp = (value: string): string => value.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&')\n\nconst isAliasArray = (alias: AliasOptions): alias is readonly Alias[] => Array.isArray(alias)\n\nconst aliasArray = (alias: AliasOptions | undefined): Alias[] => {\n if (alias === undefined) return []\n if (isAliasArray(alias)) return [...alias]\n return Object.entries(alias).map(([find, replacement]) => ({ find, replacement }))\n}\n\nconst defineCode = (value: unknown): string =>\n typeof value === 'string' ? value : JSON.stringify(value)\n\nconst PLATFORM_CODE = /\\bPlatform\\b/\n\nexport type RootnativeOptions = {\n /**\n * Runs the Babel plugin of `react-native-worklets` on your code and on the libraries that\n * have worklets. Reanimated needs it, and so does a library built on Reanimated. The app\n * must have `react-native-worklets` and `@babel/core`. Default: `false`.\n */\n worklets?: boolean | undefined\n}\n\n/**\n * The Vite plugin. It points `react-native` at `@rootnative/web`, resolves `.web.*` files\n * first, defines `__DEV__`, and replaces `Platform.OS` with `'web'`.\n */\nexport const rootnative = (options: RootnativeOptions = {}): Plugin => {\n let workletize: WorkletTransform | undefined\n\n return {\n name: '@rootnative/web',\n\n // Edits the config in place, so the app's own values win. A returned partial config would\n // append the lists after the app's entries and override its defines.\n config(config) {\n if (options.worklets === true) {\n const transform = createWorkletTransform(config.root ?? process.cwd())\n workletize = transform\n // The dev server pre-bundles the libraries, and a `transform` hook of a Vite plugin\n // never sees their files. The optimizer takes its own plugin.\n config.optimizeDeps ??= {}\n config.optimizeDeps.rolldownOptions ??= {}\n const rolldownOptions = config.optimizeDeps.rolldownOptions\n rolldownOptions.plugins = [\n rolldownOptions.plugins,\n {\n name: '@rootnative/web:worklets',\n transform: { filter: { id: SCRIPT_ID, code: WORKLET_CODE }, handler: transform },\n },\n ] as typeof rolldownOptions.plugins\n }\n\n config.resolve ??= {}\n // Last, so an alias in the app's config wins. The first matching alias resolves an import.\n config.resolve.alias = [\n ...aliasArray(config.resolve.alias),\n ...Object.entries(ALIASES).map(([name, replacement]) => ({\n find: new RegExp(`^${escapeRegExp(name)}$`),\n replacement,\n })),\n ]\n const extensions = webExtensions(config.resolve.extensions ?? VITE_DEFAULT_EXTENSIONS)\n config.resolve.extensions = extensions\n\n // Vite has set NODE_ENV before this hook runs, so `__DEV__` follows React's development mode.\n const defines = Object.fromEntries(\n Object.entries(globalDefines(isProductionEnv())).map(([name, code]) => [\n name,\n config.define?.[name] === undefined ? code : defineCode(config.define[name]),\n ]),\n )\n config.define = { ...config.define, ...defines }\n // The dep optimizer applies only its own defines, so a pre-bundled library gets them here.\n config.optimizeDeps ??= {}\n config.optimizeDeps.rolldownOptions ??= {}\n config.optimizeDeps.rolldownOptions.transform ??= {}\n config.optimizeDeps.rolldownOptions.transform.define = {\n ...defines,\n ...config.optimizeDeps.rolldownOptions.transform.define,\n }\n // The dep optimizer resolves a relative import inside a library with its own list. Without\n // the `.web` variants there, a library loads its native file, not its `.web.js` file.\n config.optimizeDeps.rolldownOptions.resolve ??= {}\n config.optimizeDeps.rolldownOptions.resolve.extensions ??= extensions\n },\n\n transform: {\n filter: {\n id: SCRIPT_ID,\n code: options.worklets === true ? [PLATFORM_CODE, WORKLET_CODE] : PLATFORM_CODE,\n },\n async handler(code, id) {\n // The Worklets step comes first, as the last plugin of the Babel preset does on native:\n // it reads the functions that the other transforms made.\n const worklets = workletize === undefined ? null : await workletize(code, id)\n const current = worklets?.code ?? code\n if (!PLATFORM_CODE.test(current)) return worklets\n\n let program: object\n try {\n program = this.parse(current)\n } catch {\n // Not plain JavaScript yet. The module keeps its code, and a later step reports a real\n // syntax error.\n return worklets\n }\n const inlined = inlinePlatformOS(current, program)\n if (inlined === null) return worklets\n // The replaced reads change only columns on their own lines, so the map of the\n // Worklets step stays close.\n return { code: inlined, map: worklets?.map ?? null }\n },\n },\n }\n}\n"],"mappings":";;;;;AAeA,MAAM,0BAA0B;CAAC;CAAQ;CAAO;CAAQ;CAAO;CAAQ;CAAQ;AAAO;AAEtF,MAAM,YAAY;AAElB,MAAM,gBAAgB,UAA0B,MAAM,QAAQ,uBAAuB,MAAM;AAE3F,MAAM,gBAAgB,UAAmD,MAAM,QAAQ,KAAK;AAE5F,MAAM,cAAc,UAA6C;CAC/D,IAAI,UAAU,KAAA,GAAW,OAAO,CAAC;CACjC,IAAI,aAAa,KAAK,GAAG,OAAO,CAAC,GAAG,KAAK;CACzC,OAAO,OAAO,QAAQ,KAAK,CAAC,CAAC,KAAK,CAAC,MAAM,kBAAkB;EAAE;EAAM;CAAY,EAAE;AACnF;AAEA,MAAM,cAAc,UAClB,OAAO,UAAU,WAAW,QAAQ,KAAK,UAAU,KAAK;AAE1D,MAAM,gBAAgB;;;;;AAetB,MAAa,cAAc,UAA6B,CAAC,MAAc;CACrE,IAAI;CAEJ,OAAO;EACL,MAAM;EAIN,OAAO,QAAQ;GACb,IAAI,QAAQ,aAAa,MAAM;IAC7B,MAAM,YAAY,uBAAuB,OAAO,QAAQ,QAAQ,IAAI,CAAC;IACrE,aAAa;IAGb,OAAO,iBAAiB,CAAC;IACzB,OAAO,aAAa,oBAAoB,CAAC;IACzC,MAAM,kBAAkB,OAAO,aAAa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