one
Version:
One is a new React Framework that makes Vite serve both native and web.
137 lines (135 loc) • 5.79 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: () => from[key],
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
value: mod,
enumerable: true
}) : target, mod));
var import_node_path = __toESM(require("node:path"), 1);
var import_node_fs = __toESM(require("node:fs"), 1);
var import_vitest = require("vitest");
var import_withOne = require("./withOne.cjs");
const projectRoot = import_node_path.default.resolve(__dirname, "../../");
const tmpDirs = [];
function createOneFixtureProject() {
const workspaceRoot = import_node_path.default.resolve(__dirname, "../../../../");
const tmpDir = import_node_fs.default.mkdtempSync(import_node_path.default.join(workspaceRoot, ".tmp-with-one-"));
tmpDirs.push(tmpDir);
import_node_fs.default.writeFileSync(import_node_path.default.join(tmpDir, "package.json"), JSON.stringify({
name: "tmp-with-one",
private: true
}));
import_node_fs.default.writeFileSync(import_node_path.default.join(tmpDir, "tsconfig.json"), JSON.stringify({
compilerOptions: {
paths: {}
}
}));
import_node_fs.default.mkdirSync(import_node_path.default.join(tmpDir, "app"));
import_node_fs.default.writeFileSync(import_node_path.default.join(tmpDir, "app", "index.tsx"), "export default null\n");
import_node_fs.default.writeFileSync(import_node_path.default.join(tmpDir, "vite.config.ts"), `
const defaultConfigOverrides = (config) => ({
...config,
watchFolders: [
...(config.watchFolders || []),
${JSON.stringify(import_node_path.default.join(tmpDir, "shared"))},
],
resolver: {
...config.resolver,
extraNodeModules: {
...config.resolver?.extraNodeModules,
'fixture-singleton': ${JSON.stringify(tmpDir)},
},
},
})
globalThis.__oneOptions = {
setupFile: {
native: './src/setup-native.ts',
},
native: {
bundler: 'metro',
bundlerOptions: {
argv: {
projectRoot: ${JSON.stringify(tmpDir)},
},
defaultConfigOverrides,
},
},
}
globalThis.__vxrnMetroOptions__ = {
argv: {
projectRoot: ${JSON.stringify(tmpDir)},
},
defaultConfigOverrides,
}
export default {
root: ${JSON.stringify(tmpDir)},
}
`);
return tmpDir;
}
(0, import_vitest.afterAll)(() => {
for (const tmpDir of tmpDirs) {
import_node_fs.default.rmSync(tmpDir, {
recursive: true,
force: true
});
}
});
(0, import_vitest.describe)("withOne", () => {
(0, import_vitest.it)("returns a config produced by the production native bundle pipeline", async () => {
const config = await (0, import_withOne.withOne)(projectRoot, {
loadViteConfig: false
});
(0, import_vitest.expect)(config).toBeTruthy();
(0, import_vitest.expect)(config.resolver).toBeTruthy();
(0, import_vitest.expect)(typeof config.resolver.resolveRequest).toBe("function");
(0, import_vitest.expect)(config.transformer).toBeTruthy();
(0, import_vitest.expect)(config.transformer.babelTransformerPath).toMatch(/vite-plugin-metro.*babel-transformer/);
});
(0, import_vitest.it)("orders sourceExts so .js wins over .mjs (the proven One fix)", async () => {
const config = await (0, import_withOne.withOne)(projectRoot, {
loadViteConfig: false
});
const exts = config.resolver.sourceExts;
(0, import_vitest.expect)(exts).toContain("mjs");
(0, import_vitest.expect)(exts).toContain("js");
(0, import_vitest.expect)(exts.indexOf("js")).toBeLessThan(exts.indexOf("mjs"));
});
(0, import_vitest.it)("accepts a project root as the first arg", async () => {
const config = await (0, import_withOne.withOne)(projectRoot, {
loadViteConfig: false
});
(0, import_vitest.expect)(config).toBeTruthy();
});
(0, import_vitest.it)("loads vite.config by default and applies the real native Metro options", async () => {
const fixtureRoot = createOneFixtureProject();
const config = await (0, import_withOne.withOne)(fixtureRoot);
(0, import_vitest.expect)(config.resolver.extraNodeModules["fixture-singleton"]).toBe(fixtureRoot);
(0, import_vitest.expect)(config.watchFolders).toContain(import_node_path.default.join(fixtureRoot, "shared"));
});
(0, import_vitest.it)("rewrites native default index bundle requests to the One entry", async () => {
const config = await (0, import_withOne.withOne)(projectRoot, {
loadViteConfig: false
});
const rewriteRequestUrl = config.server.rewriteRequestUrl;
(0, import_vitest.expect)(rewriteRequestUrl("/.expo/.virtual-metro-entry.bundle?platform=ios&dev=true")).toContain("/packages/one/metro-entry.bundle?platform=ios&dev=true");
(0, import_vitest.expect)(rewriteRequestUrl("/index.bundle?platform=ios&dev=true&hot=true&minify=false")).toContain("/packages/one/metro-entry.bundle?platform=ios&dev=true&hot=true&minify=false");
});
});