Skip to content

Commit d0f2c68

Browse files
committed
Rozenite plugin
1 parent a509067 commit d0f2c68

6 files changed

Lines changed: 132 additions & 0 deletions

File tree

rozenite/.stats/.dir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dir

rozenite/dist/index.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Metro Bundle Discovery Panel</title>
7+
<script>var __ROZENITE_PANEL__ = true;</script>
8+
<link rel="stylesheet" crossorigin href="./style.css">
9+
</head>
10+
<body>
11+
<iframe id="root" src="http://localhost:8071"></iframe>
12+
</body>
13+
</html>

rozenite/dist/react-native.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import fs from "fs";
2+
import path from "path";
3+
import { createSerializer } from "react-native-bundle-discovery";
4+
import { createServer } from "@discoveryjs/cli";
5+
import discoveryrc from "react-native-bundle-discovery/.discoveryrc.js";
6+
7+
const id = Math.floor(Math.random() * 10);
8+
const fileName = `rozenite-metro-stats-${id}.json`; // Random name in case if multiple instances of Metro are running on different ports
9+
const defaultOutputJsonPath = path.resolve(
10+
path.dirname(new URL(import.meta.url).pathname),
11+
"../.stats",
12+
fileName,
13+
);
14+
15+
try {
16+
fs.unlinkSync(defaultOutputJsonPath);
17+
} catch (e) {
18+
// Ignore if file does not exist
19+
}
20+
21+
let outputJsonPath = defaultOutputJsonPath;
22+
23+
function injectBundleDiscovery(config, options) {
24+
const hasSerializer = Boolean(
25+
config && config.serializer && config.serializer.customSerializer,
26+
);
27+
28+
outputJsonPath = (options && options.outputJsonPath) || defaultOutputJsonPath;
29+
30+
const newSerializer = createSerializer({
31+
projectRoot: process.cwd(),
32+
...options,
33+
outputJsonPath,
34+
serializer: hasSerializer ? config.serializer.customSerializer : undefined,
35+
});
36+
37+
// Inject the new serializer into the Metro config
38+
config.serializer = { ...config.serializer, customSerializer: newSerializer };
39+
40+
return config;
41+
}
42+
43+
function runServer() {
44+
const configFile = path.resolve(
45+
path.dirname(new URL(import.meta.url).pathname),
46+
"./tmp.js",
47+
);
48+
const config = { ...discoveryrc, data: "tmp" };
49+
50+
fs.writeFileSync(
51+
configFile,
52+
`module.exports = ${JSON.stringify(config, null, 1).replace(
53+
'"tmp"',
54+
`() => require("${outputJsonPath}")`,
55+
)}`,
56+
);
57+
58+
createServer({
59+
cache: false,
60+
minify: true,
61+
dev: false,
62+
config: configFile,
63+
configFile,
64+
}).then((server) => server.listen(8071));
65+
}
66+
67+
let isServerRunning = false;
68+
69+
export async function withRozeniteBundleDiscoveryPlugin(config, options) {
70+
const metroConfig = await config;
71+
injectBundleDiscovery(metroConfig, options);
72+
73+
if (!isServerRunning) {
74+
isServerRunning = true;
75+
runServer();
76+
}
77+
78+
return metroConfig;
79+
}

rozenite/dist/rozenite.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "react-native-bundle-discovery-rozenite-plugin",
3+
"version": "1.0.0-rc.2",
4+
"description": "Metro Bundle Discovery for Rozenite.",
5+
"panels": [
6+
{
7+
"name": "\uD83D\uDD0D Bundle Discovery",
8+
"source": "/index.html"
9+
}
10+
]
11+
}

rozenite/dist/style.css

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
html,
2+
body {
3+
height: 100%;
4+
}
5+
6+
body {
7+
overflow: hidden;
8+
}
9+
10+
iframe {
11+
width: 100%;
12+
height: 100%;
13+
border: none;
14+
}

rozenite/package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "react-native-bundle-discovery-rozenite-plugin",
3+
"version": "1.0.0-rc.2",
4+
"license": "MIT",
5+
"main": "dist/react-native.js",
6+
"files": [
7+
"dist",
8+
".stats/.dir"
9+
],
10+
"dependencies": {
11+
"react-native-bundle-discovery": "*",
12+
"@discoveryjs/cli": "2.14.2"
13+
}
14+
}

0 commit comments

Comments
 (0)