react-native-roku 0.0.0-stage → 0.2.0-preview.3
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 +328 -0
- package/CONTRIBUTING.md +34 -0
- package/LICENSE +21 -0
- package/NOTICE.md +10 -0
- package/README.md +66 -2
- package/bin/react-native-roku.mjs +91 -0
- package/build/assets.cjs +311 -0
- package/build/assets.d.cts +50 -0
- package/build/compatibility.d.mts +40 -0
- package/build/compatibility.mjs +272 -0
- package/build/dev.d.mts +34 -0
- package/build/dev.mjs +297 -0
- package/build/devWorkspace.d.mts +11 -0
- package/build/devWorkspace.mjs +86 -0
- package/build/device.d.mts +47 -0
- package/build/device.mjs +310 -0
- package/build/environment.d.mts +29 -0
- package/build/environment.mjs +144 -0
- package/build/esbuild.d.mts +5 -0
- package/build/esbuild.mjs +118 -0
- package/build/fontVariants.d.mts +27 -0
- package/build/fontVariants.mjs +424 -0
- package/build/frontend.d.mts +52 -0
- package/build/frontend.mjs +75 -0
- package/build/identity.mjs +45 -0
- package/build/index.d.mts +22 -0
- package/build/index.mjs +87 -0
- package/build/intl.mjs +121 -0
- package/build/metro.cjs +38 -0
- package/build/metro.d.cts +37 -0
- package/build/metroFrontend.mjs +222 -0
- package/build/metroMetadata.mjs +119 -0
- package/build/metroModules.mjs +397 -0
- package/build/metroRefresh.d.mts +64 -0
- package/build/metroRefresh.mjs +346 -0
- package/build/metroTransformer.cjs +146 -0
- package/build/nativeTextMemo.mjs +296 -0
- package/build/nativewind.d.mts +9 -0
- package/build/nativewind.mjs +92 -0
- package/build/project.d.mts +111 -0
- package/build/project.mjs +615 -0
- package/build/reactCompiler.mjs +113 -0
- package/build/reactProfile.cjs +35 -0
- package/build/refreshRuntime.mjs +187 -0
- package/build/refreshSourceMap.mjs +53 -0
- package/build/release.mjs +99 -0
- package/build/resolution.d.mts +3 -0
- package/build/resolution.mjs +25 -0
- package/build/surfaces.d.mts +1 -0
- package/build/surfaces.mjs +42 -0
- package/build/testing/node.d.mts +29 -0
- package/build/testing/node.mjs +106 -0
- package/build/yoga.d.mts +8 -0
- package/build/yoga.mjs +186 -0
- package/compatibility/public-api.json +327 -0
- package/compatibility/registry.json +2258 -0
- package/config/release.json +15 -0
- package/config/toolchain.json +34 -0
- package/docs/compatibility.md +813 -0
- package/docs/getting-started.md +460 -0
- package/docs/maintenance.md +368 -0
- package/docs/qualification.md +241 -0
- package/docs/standalone-testing.md +56 -0
- package/examples/hello-roku/README.md +35 -0
- package/examples/hello-roku/package.template.json +31 -0
- package/examples/hello-roku/pnpm-workspace.yaml +11 -0
- package/examples/hello-roku/roku.config.json +22 -0
- package/examples/hello-roku/src/App.tsx +54 -0
- package/examples/hello-roku/src/Catalog.tsx +200 -0
- package/examples/hello-roku/src/assets.d.ts +16 -0
- package/examples/hello-roku/src/catalog-cover.png +0 -0
- package/examples/hello-roku/tsconfig.json +1 -0
- package/examples/hello-roku/tsconfig.react.json +1 -0
- package/native/components/RNRAppControl.brs +215 -0
- package/native/components/RNRAppControl.xml +13 -0
- package/native/components/RNRDeviceFacts.brs +19 -0
- package/native/components/RNRDeviceFacts.xml +9 -0
- package/native/components/RNRDeviceFactsTask.brs +74 -0
- package/native/components/RNRDeviceFactsTask.xml +9 -0
- package/native/components/RNRTextRaster.brs +15 -0
- package/native/components/RNRTextRaster.xml +9 -0
- package/native/components/RNRTextRasterTask.brs +77 -0
- package/native/components/RNRTextRasterTask.xml +9 -0
- package/native/manifest.json +7 -0
- package/package.json +183 -3
- package/src/animation/clock.ts +10 -0
- package/src/animation/derivedValue.ts +163 -0
- package/src/animation/legacyAnimated.ts +255 -0
- package/src/animation/reanimated.ts +243 -0
- package/src/animation/sharedValue.ts +285 -0
- package/src/animation/worklets.ts +23 -0
- package/src/application/AppRegistry.ts +31 -0
- package/src/application/mountApplication.ts +136 -0
- package/src/graphics/BitmapLayer.tsx +68 -0
- package/src/graphics/RoundedBorder.tsx +116 -0
- package/src/graphics/TextRasterCache.ts +259 -0
- package/src/graphics/gradient.ts +37 -0
- package/src/graphics/inlineRaster.ts +85 -0
- package/src/graphics/png.ts +77 -0
- package/src/graphics/rasterPixels.ts +89 -0
- package/src/graphics/surfaces.ts +105 -0
- package/src/graphics/textRaster.types.ts +60 -0
- package/src/graphics/textRasterGeometry.ts +116 -0
- package/src/host/host.types.ts +57 -0
- package/src/index.ts +22 -0
- package/src/interop/nativewind.ts +2 -0
- package/src/layout/FontResources.ts +252 -0
- package/src/layout/affine.ts +89 -0
- package/src/layout/flexLayout.ts +398 -0
- package/src/layout/fontFaces.d.mts +19 -0
- package/src/layout/fontFaces.mjs +85 -0
- package/src/layout/fontVariants.d.mts +2 -0
- package/src/layout/fontVariants.mjs +8 -0
- package/src/layout/inlineParagraph.ts +158 -0
- package/src/layout/inlineText.ts +68 -0
- package/src/layout/layoutReuse.ts +117 -0
- package/src/layout/nativeNodeFields.ts +179 -0
- package/src/layout/preparedFields.ts +78 -0
- package/src/layout/textLayout.ts +138 -0
- package/src/layout/transform.ts +113 -0
- package/src/media/SceneGraphAudio.ts +161 -0
- package/src/media/SceneGraphVideo.ts +167 -0
- package/src/media/audioSession.ts +84 -0
- package/src/media/index.ts +2 -0
- package/src/media/videoErrorDetails.ts +40 -0
- package/src/media/videoSession.ts +136 -0
- package/src/navigation/RouterStore.ts +244 -0
- package/src/navigation/expoRouter.ts +115 -0
- package/src/react-native/NativeInput.ts +494 -0
- package/src/react-native/NativeSystemState.ts +95 -0
- package/src/react-native/accessibility.ts +265 -0
- package/src/react-native/activityIndicator.ts +72 -0
- package/src/react-native/compilerPressable.ts +26 -0
- package/src/react-native/createNativeRoot.ts +2126 -0
- package/src/react-native/directionalFocus.ts +55 -0
- package/src/react-native/environment.ts +275 -0
- package/src/react-native/flatList.ts +1001 -0
- package/src/react-native/flatListViewability.ts +231 -0
- package/src/react-native/focusEvents.ts +85 -0
- package/src/react-native/image.ts +247 -0
- package/src/react-native/index.ts +54 -0
- package/src/react-native/interactionManager.ts +76 -0
- package/src/react-native/keyboard.ts +60 -0
- package/src/react-native/linearGradient.ts +94 -0
- package/src/react-native/modal.tsx +148 -0
- package/src/react-native/nativeHostContext.ts +80 -0
- package/src/react-native/nativeModules.ts +86 -0
- package/src/react-native/nativeRoot.types.ts +22 -0
- package/src/react-native/nativeViewHandle.ts +191 -0
- package/src/react-native/nativewindPressable.ts +38 -0
- package/src/react-native/panResponder.ts +73 -0
- package/src/react-native/primitives.ts +685 -0
- package/src/react-native/safeArea.ts +76 -0
- package/src/react-native/scrollView.ts +289 -0
- package/src/react-native/styleInterop.ts +8 -0
- package/src/react-native/styleSheet.ts +54 -0
- package/src/react-native/textInput.ts +334 -0
- package/src/react-native/unsupported.ts +25 -0
- package/src/renderer/SceneRoot.ts +1006 -0
- package/src/scene/SdkDeviceFacts.ts +151 -0
- package/src/scene/SdkDeviceServices.ts +72 -0
- package/src/scene/SdkSceneGraph.ts +124 -0
- package/src/scene/SdkTextEntry.ts +321 -0
- package/src/scene/SdkTextMetrics.ts +136 -0
- package/src/scene/SdkTextRasterTransport.ts +191 -0
- package/src/scene/SdkWebTransport.ts +122 -0
- package/src/scene/createRokuHost.ts +213 -0
- package/src/scene/sceneGraph.types.ts +38 -0
- package/src/services/DeviceServices.ts +53 -0
- package/src/services/RegistryStorage.ts +85 -0
- package/src/services/RokuKeySession.ts +29 -0
- package/src/services/asyncStorage.ts +23 -0
- package/src/services/base64.ts +52 -0
- package/src/services/crypto.ts +18 -0
- package/src/services/expoConstants.ts +11 -0
- package/src/services/expoLinking.ts +12 -0
- package/src/services/expoStatusBar.ts +4 -0
- package/src/services/installWebRuntime.ts +105 -0
- package/src/services/intlEnglish.ts +54 -0
- package/src/services/languageRuntime.ts +65 -0
- package/src/services/nativeTimers.ts +96 -0
- package/src/services/secureStore.ts +23 -0
- package/src/services/textEntryRequest.ts +22 -0
- package/src/services/utf8.ts +57 -0
- package/src/services/webRuntime.ts +346 -0
- package/src/testing/CanvasResources.ts +141 -0
- package/src/testing/CanvasSceneGraph.ts +496 -0
- package/src/testing/CanvasTextEntry.ts +237 -0
- package/src/testing/PreviewTextObservations.ts +82 -0
- package/src/testing/RecordingSceneGraph.ts +136 -0
- package/src/types/normalize-colors.d.ts +4 -0
- package/src/vector/dynamicSvg.tsx +226 -0
- package/src/vector/svg.ts +222 -0
- package/src/vector/svg.types.ts +4 -0
- package/src/vector/svgElements.ts +58 -0
- package/src/vector/svgKey.d.mts +4 -0
- package/src/vector/svgKey.mjs +16 -0
- package/src/vector/svgPath.ts +157 -0
- package/src/vector/svgPixels.ts +133 -0
- package/tsconfig.consumer.json +29 -0
package/build/index.mjs
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import { copyFileSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { dirname, relative, resolve, sep } from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
|
|
7
|
+
export const packageRoot = fileURLToPath(new URL("../", import.meta.url));
|
|
8
|
+
const require = createRequire(new URL("../package.json", import.meta.url));
|
|
9
|
+
/** Public import aliases implemented by this version of the platform. */
|
|
10
|
+
export const adapterModules = Object.freeze(
|
|
11
|
+
Object.fromEntries(
|
|
12
|
+
Object.entries(
|
|
13
|
+
JSON.parse(readFileSync(new URL("../compatibility/registry.json", import.meta.url))).modules,
|
|
14
|
+
).map(([name, module]) => [name, module.adapter]),
|
|
15
|
+
),
|
|
16
|
+
);
|
|
17
|
+
export function resolveAdapter(specifier) {
|
|
18
|
+
return adapterModules[specifier] ? require.resolve(adapterModules[specifier]) : null;
|
|
19
|
+
}
|
|
20
|
+
export function resolveRuntime(subpath = "") {
|
|
21
|
+
return require.resolve(`react-native-roku${subpath ? "/" + subpath : ""}`);
|
|
22
|
+
}
|
|
23
|
+
export function isRuntimeSource(path) {
|
|
24
|
+
return path.startsWith(resolve(packageRoot, "src") + sep);
|
|
25
|
+
}
|
|
26
|
+
export const nativeComponents = Object.freeze(
|
|
27
|
+
JSON.parse(readFileSync(new URL("../native/manifest.json", import.meta.url))),
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
/** Copy package-owned native mechanics into the consumer's generated tree. */
|
|
31
|
+
export function prepareNativeComponents(generatedDirectory) {
|
|
32
|
+
const directory = resolve(generatedDirectory, "native-components");
|
|
33
|
+
mkdirSync(directory, { recursive: true });
|
|
34
|
+
const files = [];
|
|
35
|
+
for (const name of Object.values(nativeComponents))
|
|
36
|
+
for (const extension of ["xml", "brs"]) {
|
|
37
|
+
const source = resolve(packageRoot, "native/components", `${name}.${extension}`);
|
|
38
|
+
const target = resolve(directory, `${name}.${extension}`);
|
|
39
|
+
copyFileSync(source, target);
|
|
40
|
+
files.push({
|
|
41
|
+
path: relative(generatedDirectory, target).split(sep).join("/"),
|
|
42
|
+
sha256: createHash("sha256").update(readFileSync(source)).digest("hex"),
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
writeFileSync(
|
|
46
|
+
resolve(generatedDirectory, "nativeComponents.ts"),
|
|
47
|
+
Object.entries(nativeComponents)
|
|
48
|
+
.map(([key, name]) => `export {default as ${key}} from "./native-components/${name}.xml";`)
|
|
49
|
+
.join("\n") + "\n",
|
|
50
|
+
);
|
|
51
|
+
return { components: nativeComponents, files };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Installed source content, including native/build files, is a build input. */
|
|
55
|
+
export function runtimeInputs() {
|
|
56
|
+
const walk = (directory) =>
|
|
57
|
+
readdirSync(directory, { withFileTypes: true }).flatMap((entry) => {
|
|
58
|
+
if (entry.name.startsWith(".")) return [];
|
|
59
|
+
const path = resolve(directory, entry.name);
|
|
60
|
+
if (entry.isDirectory())
|
|
61
|
+
return ["node_modules", "dist", "generated", "coverage"].includes(entry.name)
|
|
62
|
+
? []
|
|
63
|
+
: walk(path);
|
|
64
|
+
if (/\.(?:test|spec)\.|\.xml\.d\.ts$|\.md$/.test(path)) return [];
|
|
65
|
+
return [
|
|
66
|
+
{
|
|
67
|
+
path: relative(packageRoot, path).split(sep).join("/"),
|
|
68
|
+
sha256: createHash("sha256").update(readFileSync(path)).digest("hex"),
|
|
69
|
+
},
|
|
70
|
+
];
|
|
71
|
+
});
|
|
72
|
+
return [
|
|
73
|
+
...walk(resolve(packageRoot, "config")),
|
|
74
|
+
...walk(resolve(packageRoot, "compatibility")),
|
|
75
|
+
...walk(resolve(packageRoot, "src")),
|
|
76
|
+
...walk(resolve(packageRoot, "build")),
|
|
77
|
+
...walk(resolve(packageRoot, "native")),
|
|
78
|
+
...walk(resolve(packageRoot, "bin")),
|
|
79
|
+
...walk(resolve(packageRoot, "examples/hello-roku")),
|
|
80
|
+
...["package.json", "tsconfig.consumer.json"].map((path) => ({
|
|
81
|
+
path,
|
|
82
|
+
sha256: createHash("sha256")
|
|
83
|
+
.update(readFileSync(resolve(packageRoot, path)))
|
|
84
|
+
.digest("hex"),
|
|
85
|
+
})),
|
|
86
|
+
].sort((a, b) => a.path.localeCompare(b.path));
|
|
87
|
+
}
|
package/build/intl.mjs
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { resolve } from "node:path";
|
|
5
|
+
|
|
6
|
+
/** Pure upstream data only. Never evaluate dependency source to extract it. */
|
|
7
|
+
function literal(node) {
|
|
8
|
+
if (ts.isStringLiteralLike(node)) return node.text;
|
|
9
|
+
if (ts.isNumericLiteral(node)) return Number(node.text);
|
|
10
|
+
if (node.kind === ts.SyntaxKind.NullKeyword) return null;
|
|
11
|
+
if (node.kind === ts.SyntaxKind.TrueKeyword) return true;
|
|
12
|
+
if (node.kind === ts.SyntaxKind.FalseKeyword) return false;
|
|
13
|
+
if (
|
|
14
|
+
ts.isPrefixUnaryExpression(node) &&
|
|
15
|
+
node.operator === ts.SyntaxKind.MinusToken &&
|
|
16
|
+
ts.isNumericLiteral(node.operand)
|
|
17
|
+
) {
|
|
18
|
+
const value = -Number(node.operand.text);
|
|
19
|
+
if (!Object.is(value, -0)) return value;
|
|
20
|
+
}
|
|
21
|
+
if (ts.isArrayLiteralExpression(node)) return node.elements.map(literal);
|
|
22
|
+
if (ts.isObjectLiteralExpression(node)) {
|
|
23
|
+
const value = Object.create(null);
|
|
24
|
+
for (const property of node.properties) {
|
|
25
|
+
if (
|
|
26
|
+
!ts.isPropertyAssignment(property) ||
|
|
27
|
+
!property.name ||
|
|
28
|
+
ts.isComputedPropertyName(property.name)
|
|
29
|
+
)
|
|
30
|
+
throw Error("Nonliteral Intl data property");
|
|
31
|
+
const key =
|
|
32
|
+
ts.isIdentifier(property.name) ||
|
|
33
|
+
ts.isStringLiteralLike(property.name) ||
|
|
34
|
+
ts.isNumericLiteral(property.name)
|
|
35
|
+
? property.name.text
|
|
36
|
+
: null;
|
|
37
|
+
if (key === null || key === "__proto__" || Object.hasOwn(value, key))
|
|
38
|
+
throw Error("Unsupported Intl data key");
|
|
39
|
+
value[key] = literal(property.initializer);
|
|
40
|
+
}
|
|
41
|
+
return value;
|
|
42
|
+
}
|
|
43
|
+
throw Error("Intl data contains executable source; review the upstream layout");
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Bound SDK per-file transformation work without pruning locales or editing
|
|
47
|
+
* installed dependencies. Every emitted file lives in the build's generation. */
|
|
48
|
+
export function prepareIntlDataModules(
|
|
49
|
+
path,
|
|
50
|
+
generatedDirectory,
|
|
51
|
+
{ threshold = 64 * 1024, chunkCharacters = 64 * 1024, source: inputSource } = {},
|
|
52
|
+
) {
|
|
53
|
+
if (!generatedDirectory) throw Error("An Intl build requires an owned generatedDirectory");
|
|
54
|
+
if (!(
|
|
55
|
+
Number.isSafeInteger(threshold) &&
|
|
56
|
+
threshold > 0 &&
|
|
57
|
+
Number.isSafeInteger(chunkCharacters) &&
|
|
58
|
+
chunkCharacters > 0 &&
|
|
59
|
+
chunkCharacters <= 64 * 1024
|
|
60
|
+
))
|
|
61
|
+
throw Error("Invalid Intl data chunk bounds");
|
|
62
|
+
const code = inputSource ?? readFileSync(path, "utf8");
|
|
63
|
+
const source = ts.createSourceFile(path, code, ts.ScriptTarget.Latest, true, ts.ScriptKind.JS);
|
|
64
|
+
const replacements = [],
|
|
65
|
+
imports = [],
|
|
66
|
+
files = [];
|
|
67
|
+
const directory = resolve(
|
|
68
|
+
generatedDirectory,
|
|
69
|
+
"intl-data",
|
|
70
|
+
createHash("sha256").update(code).digest("hex").slice(0, 20),
|
|
71
|
+
);
|
|
72
|
+
for (const statement of source.statements) {
|
|
73
|
+
if (!ts.isVariableStatement(statement)) continue;
|
|
74
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
75
|
+
const node = declaration.initializer;
|
|
76
|
+
if (!node || node.end - node.getStart(source) < threshold) continue;
|
|
77
|
+
let data;
|
|
78
|
+
if (
|
|
79
|
+
ts.isCallExpression(node) &&
|
|
80
|
+
node.expression.getText(source) === "JSON.parse" &&
|
|
81
|
+
node.arguments.length === 1 &&
|
|
82
|
+
ts.isStringLiteralLike(node.arguments[0])
|
|
83
|
+
) {
|
|
84
|
+
data = node.arguments[0].text;
|
|
85
|
+
JSON.parse(data);
|
|
86
|
+
} else if (ts.isObjectLiteralExpression(node) || ts.isArrayLiteralExpression(node))
|
|
87
|
+
data = JSON.stringify(literal(node));
|
|
88
|
+
else throw Error(`Review large Intl initializer ${declaration.name.getText(source)}`);
|
|
89
|
+
mkdirSync(directory, { recursive: true });
|
|
90
|
+
const index = replacements.length,
|
|
91
|
+
fragments = [];
|
|
92
|
+
for (let offset = 0; offset < data.length; offset += chunkCharacters) {
|
|
93
|
+
const part = resolve(directory, `data-${index}-${fragments.length}.mjs`);
|
|
94
|
+
writeFileSync(
|
|
95
|
+
part,
|
|
96
|
+
`export default ${JSON.stringify(data.slice(offset, offset + chunkCharacters))};\n`,
|
|
97
|
+
);
|
|
98
|
+
files.push(part);
|
|
99
|
+
fragments.push(part);
|
|
100
|
+
}
|
|
101
|
+
const driver = resolve(directory, `data-${index}.mjs`);
|
|
102
|
+
writeFileSync(
|
|
103
|
+
driver,
|
|
104
|
+
fragments.map((part, n) => `import part${n} from ${JSON.stringify(part)};`).join("\n") +
|
|
105
|
+
`\nexport default JSON.parse([${fragments.map((_, n) => `part${n}`).join(",")}].join(""));\n`,
|
|
106
|
+
);
|
|
107
|
+
files.push(driver);
|
|
108
|
+
const name = `__rnrIntlData${index}`;
|
|
109
|
+
if (code.includes(name)) throw Error("Intl extraction name collision");
|
|
110
|
+
imports.push(`import ${name} from ${JSON.stringify(driver)};`);
|
|
111
|
+
replacements.push({ start: node.getStart(source), end: node.end, value: name });
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
if (!replacements.length && code.length > 2 * 1024 * 1024)
|
|
115
|
+
throw Error("Review changed Intl static data layout before SDK transformation");
|
|
116
|
+
let contents = code;
|
|
117
|
+
for (const replacement of replacements.toReversed())
|
|
118
|
+
contents =
|
|
119
|
+
contents.slice(0, replacement.start) + replacement.value + contents.slice(replacement.end);
|
|
120
|
+
return { contents: imports.join("\n") + "\n" + contents, files };
|
|
121
|
+
}
|
package/build/metro.cjs
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
const { createRequire } = require("node:module");
|
|
2
|
+
const { resolve } = require("node:path");
|
|
3
|
+
const { resolveAdapter } = require("./index.mjs");
|
|
4
|
+
const { resolveRokuRelative } = require("./resolution.mjs");
|
|
5
|
+
const { nativewindLeaf } = require("./nativewind.mjs");
|
|
6
|
+
exports.withRokuBabelTransformer = require("./metroTransformer.cjs").withRokuBabelTransformer;
|
|
7
|
+
/** Target-scoped Metro resolver integration. SDK packaging/HMR is a separate stage. */
|
|
8
|
+
exports.withRoku = function withRoku(config, { root = config.projectRoot ?? process.cwd() } = {}) {
|
|
9
|
+
const nativeRequire = createRequire(resolve(root, "package.json"));
|
|
10
|
+
const previous = config.resolver?.resolveRequest;
|
|
11
|
+
return {
|
|
12
|
+
...config,
|
|
13
|
+
resolver: {
|
|
14
|
+
...config.resolver,
|
|
15
|
+
platforms: [...new Set([...(config.resolver?.platforms ?? []), "roku"])],
|
|
16
|
+
resolveRequest(context, name, platform) {
|
|
17
|
+
if (platform === "roku") {
|
|
18
|
+
const adapter = resolveAdapter(name);
|
|
19
|
+
if (adapter) return { type: "sourceFile", filePath: adapter };
|
|
20
|
+
if (name === "react" || name.startsWith("react/"))
|
|
21
|
+
return {
|
|
22
|
+
type: "sourceFile",
|
|
23
|
+
filePath: nativeRequire.resolve(name),
|
|
24
|
+
};
|
|
25
|
+
if (name.startsWith("react-native/") || name.startsWith("@roku-sdk/"))
|
|
26
|
+
throw Error(`Unadapted native import in Roku frontend: ${name}`);
|
|
27
|
+
const leaf = nativewindLeaf(name, context.originModulePath, nativeRequire);
|
|
28
|
+
if (leaf) return { type: "sourceFile", filePath: leaf };
|
|
29
|
+
const selected = resolveRokuRelative(context.originModulePath, name);
|
|
30
|
+
if (selected) return { type: "sourceFile", filePath: selected };
|
|
31
|
+
}
|
|
32
|
+
return previous
|
|
33
|
+
? previous(context, name, platform)
|
|
34
|
+
: context.resolveRequest(context, name, platform);
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export declare function withRoku<
|
|
2
|
+
T extends { projectRoot?: string; resolver?: Record<string, unknown> },
|
|
3
|
+
>(config: T, options?: { root?: string }): T & { resolver: Record<string, unknown> };
|
|
4
|
+
|
|
5
|
+
type BabelTransformer = {
|
|
6
|
+
transform: (...args: never[]) => unknown;
|
|
7
|
+
getCacheKey?: (...args: never[]) => string | Uint8Array;
|
|
8
|
+
};
|
|
9
|
+
type ImageOptions = Omit<import("./assets.cjs").RokuImageAssetsOptions, "root" | "reset">;
|
|
10
|
+
type TransformerOptions = {
|
|
11
|
+
root?: string;
|
|
12
|
+
generatedDirectory?: string;
|
|
13
|
+
plugins?: readonly string[];
|
|
14
|
+
preparedSourcesPath?: string;
|
|
15
|
+
fastRefresh?: boolean;
|
|
16
|
+
};
|
|
17
|
+
type CacheKey<T extends BabelTransformer> = {
|
|
18
|
+
getCacheKey(...args: T extends { getCacheKey: (...args: infer P) => unknown } ? P : []): string;
|
|
19
|
+
};
|
|
20
|
+
type ImageTransformer<T extends BabelTransformer> = Omit<T, "transform" | "getCacheKey"> &
|
|
21
|
+
CacheKey<T> & {
|
|
22
|
+
transform(
|
|
23
|
+
...args: Parameters<T["transform"]>
|
|
24
|
+
): ReturnType<T["transform"]> | Promise<Awaited<ReturnType<T["transform"]>>>;
|
|
25
|
+
};
|
|
26
|
+
export declare function withRokuBabelTransformer<T extends BabelTransformer>(
|
|
27
|
+
upstream: T,
|
|
28
|
+
options: TransformerOptions & { imageAssets: ImageOptions },
|
|
29
|
+
): ImageTransformer<T>;
|
|
30
|
+
export declare function withRokuBabelTransformer<T extends BabelTransformer>(
|
|
31
|
+
upstream: T,
|
|
32
|
+
options?: TransformerOptions & { imageAssets?: undefined | false },
|
|
33
|
+
): Omit<T, "getCacheKey"> & CacheKey<T>;
|
|
34
|
+
export declare function withRokuBabelTransformer<T extends BabelTransformer>(
|
|
35
|
+
upstream: T,
|
|
36
|
+
options: TransformerOptions & { imageAssets?: ImageOptions | false },
|
|
37
|
+
): ImageTransformer<T>;
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { dirname, relative, resolve, sep } from "node:path";
|
|
4
|
+
import { createHash } from "node:crypto";
|
|
5
|
+
import { pathToFileURL } from "node:url";
|
|
6
|
+
import { rokuBuildProfile, installedPackage, toolchain } from "./environment.mjs";
|
|
7
|
+
import { prepareIntlDataModules } from "./intl.mjs";
|
|
8
|
+
import { writeMetroModules } from "./metroModules.mjs";
|
|
9
|
+
import { withRoku } from "./metro.cjs";
|
|
10
|
+
import { createMetroMetadata } from "./metroMetadata.mjs";
|
|
11
|
+
import { createRokuImageAssets } from "./assets.cjs";
|
|
12
|
+
|
|
13
|
+
const platformRequire = createRequire(import.meta.url);
|
|
14
|
+
|
|
15
|
+
/** Finite optional frontend; applications retain their Metro/Babel configuration
|
|
16
|
+
* and export ordinary mount functions. SDK/Solid compilation remains separate. */
|
|
17
|
+
export async function buildRokuMetroModules({
|
|
18
|
+
root = process.cwd(),
|
|
19
|
+
entry,
|
|
20
|
+
outfile,
|
|
21
|
+
profile = "production",
|
|
22
|
+
configPath,
|
|
23
|
+
configuration,
|
|
24
|
+
workDirectory,
|
|
25
|
+
exports: names = ["mountApplication"],
|
|
26
|
+
maxModuleBytes = 2 * 1024 * 1024,
|
|
27
|
+
imageAssets,
|
|
28
|
+
refresh,
|
|
29
|
+
}) {
|
|
30
|
+
root = resolve(root);
|
|
31
|
+
if (typeof entry !== "string" || typeof outfile !== "string")
|
|
32
|
+
throw Error("A Roku Metro frontend needs an entry and outfile");
|
|
33
|
+
outfile = resolve(root, outfile);
|
|
34
|
+
workDirectory = resolve(root, workDirectory ?? resolve(dirname(outfile), ".metro"));
|
|
35
|
+
const compilation = rokuBuildProfile(profile);
|
|
36
|
+
if (
|
|
37
|
+
refresh &&
|
|
38
|
+
(!compilation.development || typeof refresh.session?.createModuleIdFactory !== "function")
|
|
39
|
+
)
|
|
40
|
+
throw Error("Roku refresh requires a development profile and a refresh session");
|
|
41
|
+
const require = createRequire(resolve(root, "package.json"));
|
|
42
|
+
for (const [name, version] of Object.entries(toolchain.optionalPackages.metro)) {
|
|
43
|
+
const expected = version.startsWith("npm:")
|
|
44
|
+
? version.slice(version.lastIndexOf("@") + 1)
|
|
45
|
+
: version;
|
|
46
|
+
let actual;
|
|
47
|
+
try {
|
|
48
|
+
actual = installedPackage(root, name).version;
|
|
49
|
+
} catch {
|
|
50
|
+
throw Error(
|
|
51
|
+
`Install the optional Metro tool cohort before using the Metro frontend: ${name}@${version}`,
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
if (actual !== expected)
|
|
55
|
+
throw Error(`Review the Metro tool cohort: expected ${name}@${expected}, found ${actual}`);
|
|
56
|
+
}
|
|
57
|
+
const Metro = require("metro");
|
|
58
|
+
const { getDefaultConfig, mergeConfig } = require("@react-native/metro-config");
|
|
59
|
+
if (configuration && configPath) throw Error("Choose a Metro configuration object or configPath");
|
|
60
|
+
const config = configuration
|
|
61
|
+
? mergeConfig(getDefaultConfig(root), configuration)
|
|
62
|
+
: await Metro.loadConfig(
|
|
63
|
+
{ cwd: root, ...(configPath ? { config: resolve(root, configPath) } : {}) },
|
|
64
|
+
getDefaultConfig(root),
|
|
65
|
+
);
|
|
66
|
+
const generatedDirectory = resolve(workDirectory, "dependencies");
|
|
67
|
+
const images = imageAssets ? createRokuImageAssets({ ...imageAssets, root, reset: true }) : null;
|
|
68
|
+
const imageConfiguration = images?.metroConfiguration();
|
|
69
|
+
// The file map must see extracted data before Metro starts. Cache hits still
|
|
70
|
+
// refer to these content-addressed files, so prepare every pinned entry that
|
|
71
|
+
// can carry collation tables without pruning or executing dependency source.
|
|
72
|
+
const intl = ["index.js", "polyfill.js", "polyfill-force.js"].map((name) => {
|
|
73
|
+
const path = platformRequire.resolve(
|
|
74
|
+
"@formatjs/intl-collator" + (name === "index.js" ? "" : "/" + name),
|
|
75
|
+
);
|
|
76
|
+
const prepared = prepareIntlDataModules(path, generatedDirectory);
|
|
77
|
+
return {
|
|
78
|
+
path,
|
|
79
|
+
sha256: createHash("sha256").update(readFileSync(path)).digest("hex"),
|
|
80
|
+
files: prepared.files,
|
|
81
|
+
};
|
|
82
|
+
});
|
|
83
|
+
mkdirSync(workDirectory, { recursive: true });
|
|
84
|
+
const upstream = require.resolve(config.transformer.babelTransformerPath);
|
|
85
|
+
const preparedSources = {};
|
|
86
|
+
let bundle,
|
|
87
|
+
metadata,
|
|
88
|
+
raw,
|
|
89
|
+
imagePreparationPasses = 0;
|
|
90
|
+
for (let attempt = 0; attempt < (images ? 3 : 1); attempt++) {
|
|
91
|
+
const phase = images ? resolve(workDirectory, `image-pass-${attempt}`) : workDirectory;
|
|
92
|
+
mkdirSync(phase, { recursive: true });
|
|
93
|
+
const prepared = JSON.stringify(preparedSources);
|
|
94
|
+
const identity = createHash("sha256").update(prepared).digest("hex").slice(0, 24);
|
|
95
|
+
const preparedSourcesPath = images ? resolve(phase, `prepared-${identity}.json`) : undefined;
|
|
96
|
+
if (preparedSourcesPath) writeFileSync(preparedSourcesPath, prepared + "\n");
|
|
97
|
+
const transformer = resolve(phase, `transformer-${identity}.cjs`);
|
|
98
|
+
writeFileSync(
|
|
99
|
+
transformer,
|
|
100
|
+
`const {withRokuBabelTransformer}=require(${JSON.stringify(platformRequire.resolve("react-native-roku/metro"))});module.exports=withRokuBabelTransformer(require(${JSON.stringify(upstream)}),${JSON.stringify({ root, generatedDirectory, imageAssets: images ? false : undefined, preparedSourcesPath, fastRefresh: !!refresh, plugins: [require.resolve("@babel/plugin-transform-class-static-block")] })});\n`,
|
|
101
|
+
);
|
|
102
|
+
const effective = withRoku(
|
|
103
|
+
mergeConfig(config, {
|
|
104
|
+
watchFolders: [
|
|
105
|
+
...new Set([
|
|
106
|
+
...(config.watchFolders ?? []),
|
|
107
|
+
root,
|
|
108
|
+
resolve(workDirectory),
|
|
109
|
+
...(imageAssets ? [resolve(root, imageAssets.generatedDirectory)] : []),
|
|
110
|
+
]),
|
|
111
|
+
],
|
|
112
|
+
resolver: {
|
|
113
|
+
nodeModulesPaths: [
|
|
114
|
+
...new Set([
|
|
115
|
+
...(config.resolver.nodeModulesPaths ?? []),
|
|
116
|
+
resolve(root, "node_modules"),
|
|
117
|
+
]),
|
|
118
|
+
],
|
|
119
|
+
},
|
|
120
|
+
transformer: {
|
|
121
|
+
babelTransformerPath: transformer,
|
|
122
|
+
...(imageConfiguration
|
|
123
|
+
? {
|
|
124
|
+
assetRegistryPath: imageConfiguration.assetRegistryPath,
|
|
125
|
+
assetPlugins: [
|
|
126
|
+
...(config.transformer.assetPlugins ?? []),
|
|
127
|
+
...imageConfiguration.assetPlugins,
|
|
128
|
+
],
|
|
129
|
+
}
|
|
130
|
+
: {}),
|
|
131
|
+
},
|
|
132
|
+
}),
|
|
133
|
+
{ root },
|
|
134
|
+
);
|
|
135
|
+
if (refresh)
|
|
136
|
+
effective.serializer.createModuleIdFactory = refresh.session.createModuleIdFactory(
|
|
137
|
+
effective.serializer.createModuleIdFactory.bind(effective.serializer),
|
|
138
|
+
);
|
|
139
|
+
metadata = createMetroMetadata(root, effective.serializer, refresh);
|
|
140
|
+
effective.serializer = metadata.serializer;
|
|
141
|
+
raw = resolve(phase, "metro.bundle.js");
|
|
142
|
+
bundle = await Metro.runBuild(effective, {
|
|
143
|
+
entry: relative(effective.projectRoot, resolve(root, entry)),
|
|
144
|
+
platform: "roku",
|
|
145
|
+
dev: compilation.development,
|
|
146
|
+
minify: false,
|
|
147
|
+
bundleOut: raw,
|
|
148
|
+
sourceMap: true,
|
|
149
|
+
sourceMapOut: raw + ".map",
|
|
150
|
+
sourceMapUrl: "metro.bundle.js.map",
|
|
151
|
+
assets: true,
|
|
152
|
+
});
|
|
153
|
+
if (!Array.isArray(bundle.assets)) throw Error("Review the Metro asset inventory contract");
|
|
154
|
+
if (!images) break;
|
|
155
|
+
imagePreparationPasses++;
|
|
156
|
+
let added = false;
|
|
157
|
+
const owned = resolve(root, imageAssets.generatedDirectory, "embedded-images") + sep;
|
|
158
|
+
for (const { path, source } of metadata.embeddedSources()) {
|
|
159
|
+
if (path.startsWith(owned)) continue;
|
|
160
|
+
const contents = await images.prepareEmbeddedSource(path, source);
|
|
161
|
+
if (contents === source) continue;
|
|
162
|
+
const sha256 = createHash("sha256").update(source).digest("hex");
|
|
163
|
+
const old = preparedSources[path];
|
|
164
|
+
if (old && old.sha256 !== sha256)
|
|
165
|
+
throw Error(`Roku image source changed during preparation: ${path}`);
|
|
166
|
+
if (!old || old.contents !== contents) {
|
|
167
|
+
preparedSources[path] = { sha256, contents };
|
|
168
|
+
added = true;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
if (!added) {
|
|
172
|
+
await images.restoreMetroAssets(bundle.assets);
|
|
173
|
+
break;
|
|
174
|
+
}
|
|
175
|
+
if (attempt === 2) throw Error("The Metro image dependency graph did not settle");
|
|
176
|
+
}
|
|
177
|
+
let refreshMap;
|
|
178
|
+
if (refresh) {
|
|
179
|
+
const path = resolve(workDirectory, "refresh-source-map.json");
|
|
180
|
+
writeFileSync(path, bundle.map);
|
|
181
|
+
refreshMap = {
|
|
182
|
+
path,
|
|
183
|
+
sha256: createHash("sha256").update(bundle.map).digest("hex"),
|
|
184
|
+
url: pathToFileURL(raw + ".map").href,
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
const result = writeMetroModules({
|
|
188
|
+
code: bundle.code,
|
|
189
|
+
map: JSON.parse(bundle.map),
|
|
190
|
+
mapUrl: pathToFileURL(raw + ".map").href,
|
|
191
|
+
outfile: resolve(root, outfile),
|
|
192
|
+
exports: names,
|
|
193
|
+
profile: compilation.name,
|
|
194
|
+
metadata,
|
|
195
|
+
maxModuleBytes,
|
|
196
|
+
...(refresh
|
|
197
|
+
? { refresh: { sessionId: refresh.session.id, generation: refresh.generation }, refreshMap }
|
|
198
|
+
: {}),
|
|
199
|
+
});
|
|
200
|
+
result.metafile.outputs[relative(root, outfile).split("\\").join("/")].entryPoint = relative(
|
|
201
|
+
root,
|
|
202
|
+
resolve(root, entry),
|
|
203
|
+
)
|
|
204
|
+
.split("\\")
|
|
205
|
+
.join("/");
|
|
206
|
+
const record = {
|
|
207
|
+
...result,
|
|
208
|
+
profile: compilation.name,
|
|
209
|
+
bundler: "metro",
|
|
210
|
+
bundle: raw,
|
|
211
|
+
bundleSha256: createHash("sha256").update(bundle.code).digest("hex"),
|
|
212
|
+
intl,
|
|
213
|
+
sourceMap: raw + ".map",
|
|
214
|
+
sources: JSON.parse(bundle.map).sources,
|
|
215
|
+
assets: bundle.assets,
|
|
216
|
+
...(images
|
|
217
|
+
? { imageAssets: Object.fromEntries(images.inventory()), imagePreparationPasses }
|
|
218
|
+
: {}),
|
|
219
|
+
};
|
|
220
|
+
writeFileSync(resolve(workDirectory, "frontend.json"), JSON.stringify(record, null, 2) + "\n");
|
|
221
|
+
return record;
|
|
222
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { isAbsolute, relative, sep } from "node:path";
|
|
3
|
+
|
|
4
|
+
/** Observe the public serializer hooks without changing module selection/IDs. */
|
|
5
|
+
export function createMetroMetadata(root, serializer, refresh) {
|
|
6
|
+
if (
|
|
7
|
+
typeof serializer?.createModuleIdFactory !== "function" ||
|
|
8
|
+
typeof serializer.processModuleFilter !== "function"
|
|
9
|
+
)
|
|
10
|
+
throw Error("Review the Metro serializer metadata boundary");
|
|
11
|
+
const ids = new Map(),
|
|
12
|
+
records = new Map();
|
|
13
|
+
const name = (path) => relative(root, path).split(sep).join("/");
|
|
14
|
+
const result = {
|
|
15
|
+
serializer: {
|
|
16
|
+
...serializer,
|
|
17
|
+
createModuleIdFactory() {
|
|
18
|
+
const assign = serializer.createModuleIdFactory.call(serializer);
|
|
19
|
+
return (path) => {
|
|
20
|
+
const id = assign(path);
|
|
21
|
+
if (ids.has(id) && ids.get(id) !== path)
|
|
22
|
+
throw Error("Metro assigned one module ID to different source files");
|
|
23
|
+
ids.set(id, path);
|
|
24
|
+
return id;
|
|
25
|
+
};
|
|
26
|
+
},
|
|
27
|
+
processModuleFilter(module) {
|
|
28
|
+
const accepted = serializer.processModuleFilter.call(serializer, module);
|
|
29
|
+
if (accepted) {
|
|
30
|
+
const physical = isAbsolute(module.path) && existsSync(module.path);
|
|
31
|
+
const source = physical ? module.getSource() : null;
|
|
32
|
+
records.set(module.path, {
|
|
33
|
+
physical,
|
|
34
|
+
embeddedSource: source?.includes("data:image/") ? source.toString("utf8") : null,
|
|
35
|
+
refreshSource: refresh && source ? source.toString("utf8") : null,
|
|
36
|
+
input: physical
|
|
37
|
+
? {
|
|
38
|
+
bytes: Buffer.byteLength(source),
|
|
39
|
+
imports: [...module.dependencies.values()].map((dependency) => ({
|
|
40
|
+
path: dependency.absolutePath
|
|
41
|
+
? name(dependency.absolutePath)
|
|
42
|
+
: dependency.data.name,
|
|
43
|
+
kind: dependency.data.data.asyncType
|
|
44
|
+
? "dynamic-import"
|
|
45
|
+
: dependency.data.data.isESMImport
|
|
46
|
+
? "import-statement"
|
|
47
|
+
: "require-call",
|
|
48
|
+
original: dependency.data.name,
|
|
49
|
+
...(!dependency.absolutePath ? { external: true } : {}),
|
|
50
|
+
})),
|
|
51
|
+
}
|
|
52
|
+
: null,
|
|
53
|
+
scripts: module.output
|
|
54
|
+
.filter((output) => output.type === "js/script")
|
|
55
|
+
.map((output) => output.data.code),
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
return accepted;
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
moduleInput(id, bytes) {
|
|
62
|
+
const path = ids.get(id),
|
|
63
|
+
record = records.get(path);
|
|
64
|
+
if (!record) throw Error(`Unattested Metro module ID: ${id}`);
|
|
65
|
+
return record.physical ? { path: name(path), bytesInOutput: bytes } : null;
|
|
66
|
+
},
|
|
67
|
+
moduleRefresh(id) {
|
|
68
|
+
const path = ids.get(id),
|
|
69
|
+
record = records.get(path);
|
|
70
|
+
if (!record) throw Error(`Unattested Metro refresh module: ${id}`);
|
|
71
|
+
return { source: name(path), safe: refresh.session.classify(path, record.refreshSource) };
|
|
72
|
+
},
|
|
73
|
+
scriptInputs(preamble) {
|
|
74
|
+
const inputs = {},
|
|
75
|
+
occupied = [];
|
|
76
|
+
for (const [path, record] of records) {
|
|
77
|
+
if (!record.physical) continue;
|
|
78
|
+
for (const raw of record.scripts) {
|
|
79
|
+
const code = raw.trimEnd();
|
|
80
|
+
if (!code) continue;
|
|
81
|
+
let offset = preamble.indexOf(code);
|
|
82
|
+
while (
|
|
83
|
+
offset !== -1 &&
|
|
84
|
+
occupied.some(([start, end]) => offset < end && offset + code.length > start)
|
|
85
|
+
)
|
|
86
|
+
offset = preamble.indexOf(code, offset + 1);
|
|
87
|
+
if (offset === -1) throw Error(`Unattested Metro startup script: ${path}`);
|
|
88
|
+
occupied.push([offset, offset + code.length]);
|
|
89
|
+
const key = name(path);
|
|
90
|
+
inputs[key] = {
|
|
91
|
+
bytesInOutput: (inputs[key]?.bytesInOutput ?? 0) + Buffer.byteLength(code),
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return inputs;
|
|
96
|
+
},
|
|
97
|
+
metafile(outputs) {
|
|
98
|
+
const used = new Set(Object.values(outputs).flatMap((output) => Object.keys(output.inputs)));
|
|
99
|
+
return {
|
|
100
|
+
inputs: Object.fromEntries(
|
|
101
|
+
[...records]
|
|
102
|
+
.filter(([path, record]) => record.physical && used.has(name(path)))
|
|
103
|
+
.map(([path, record]) => [name(path), record.input]),
|
|
104
|
+
),
|
|
105
|
+
outputs,
|
|
106
|
+
};
|
|
107
|
+
},
|
|
108
|
+
outputName: name,
|
|
109
|
+
embeddedSources(inputs) {
|
|
110
|
+
return [...records]
|
|
111
|
+
.filter(
|
|
112
|
+
([path, record]) =>
|
|
113
|
+
record.embeddedSource && (!inputs || Object.hasOwn(inputs, name(path))),
|
|
114
|
+
)
|
|
115
|
+
.map(([path, record]) => ({ path, source: record.embeddedSource }));
|
|
116
|
+
},
|
|
117
|
+
};
|
|
118
|
+
return result;
|
|
119
|
+
}
|