UNPKG

one

Version:

One is a new React Framework that makes Vite serve both native and web.

179 lines (176 loc) 7.61 kB
"use strict"; 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_path = __toESM(require("path"), 1); var import_vitest = require("vitest"); var import_index = __toESM(require("./index.native.js"), 1); function _type_of(obj) { "@swc/helpers - typeof"; return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; } var projectRoot = import_path.default.resolve(__dirname, "../../"); var fakeApi = function (cwd) { return { cache: function () {}, cwd: function () { return cwd; } }; }; (0, import_vitest.describe)("one/babel-preset", function () { (0, import_vitest.it)("returns presets and plugins", function () { var result = (0, import_index.default)(fakeApi(projectRoot), { projectRoot, // skip the babel-preset-expo lookup so this test runs without the // expo SDK installed in the workspace root includeExpoPreset: false }); (0, import_vitest.expect)(result).toHaveProperty("plugins"); (0, import_vitest.expect)(Array.isArray(result.plugins)).toBe(true); (0, import_vitest.expect)(result.plugins).toHaveLength(6); }); (0, import_vitest.it)("orders the plugin chain so server code is removed before router transforms", function () { var { plugins } = (0, import_index.default)(fakeApi(projectRoot), { projectRoot, includeExpoPreset: false }); var names = (plugins !== null && plugins !== void 0 ? plugins : []).map(function (p) { return Array.isArray(p) ? p[0] : p; }); (0, import_vitest.expect)(names).toEqual(["@vxrn/vite-plugin-metro/babel-plugins/import-meta-env-plugin", "one/babel-plugin-environment-guard", "one/babel-plugin-remove-server-code", "babel-plugin-module-resolver", "one/babel-plugin-one-router-metro", "one/babel-plugin-inline-one-server-url"]); }); (0, import_vitest.it)('defaults routerRoot to "app"', function () { var { plugins } = (0, import_index.default)(fakeApi(projectRoot), { projectRoot, includeExpoPreset: false }); var removeServer = (plugins !== null && plugins !== void 0 ? plugins : []).find(function (p) { return Array.isArray(p) && p[0] === "one/babel-plugin-remove-server-code"; }); (0, import_vitest.expect)(removeServer === null || removeServer === void 0 ? void 0 : removeServer[1].routerRoot).toBe("app"); }); (0, import_vitest.it)("threads custom routerRoot through", function () { var { plugins } = (0, import_index.default)(fakeApi(projectRoot), { projectRoot, routerRoot: "src/routes", includeExpoPreset: false }); var removeServer = (plugins !== null && plugins !== void 0 ? plugins : []).find(function (p) { return Array.isArray(p) && p[0] === "one/babel-plugin-remove-server-code"; }); (0, import_vitest.expect)(removeServer === null || removeServer === void 0 ? void 0 : removeServer[1].routerRoot).toBe("src/routes"); }); (0, import_vitest.it)("skips the One plugin chain when the Vite Metro caller already injected it", function () { var result = (0, import_index.default)({ cache: function () {}, cwd: function () { return projectRoot; }, caller: function (cb) { return cb({ oneViteMetroBabelConfig: true }); } }, { projectRoot, includeExpoPreset: false }); (0, import_vitest.expect)(result.plugins).toEqual([]); }); }); (0, import_vitest.describe)("buildOneBabelPlugins", function () { (0, import_vitest.it)("produces the canonical One plugin chain (no env plugin)", function () { var plugins = (0, import_index.buildOneBabelPlugins)({ projectRoot, relativeRouterRoot: "app", ignoredRouteFiles: [], linking: void 0, setupFile: void 0, // skip the import-meta-env plugin so the assertion covers the One-only chain includeImportMetaEnv: false }); (0, import_vitest.expect)(plugins).toHaveLength(5); (0, import_vitest.expect)(plugins[0]).toBe("one/babel-plugin-environment-guard"); (0, import_vitest.expect)(plugins[1]).toEqual(["one/babel-plugin-remove-server-code", { routerRoot: "app" }]); (0, import_vitest.expect)(plugins[2]).toMatchObject(["babel-plugin-module-resolver", { alias: {} }]); var oneRouterMetro = plugins[3]; (0, import_vitest.expect)(oneRouterMetro[0]).toBe("one/babel-plugin-one-router-metro"); (0, import_vitest.expect)(oneRouterMetro[1]).toMatchObject({ ONE_ROUTER_ROOT_FOLDER_NAME: "app", ONE_ROUTER_LINKING_CONFIG: void 0, ONE_SETUP_FILE_NATIVE: void 0 }); (0, import_vitest.expect)(_type_of(oneRouterMetro[1].ONE_ROUTER_APP_ROOT_RELATIVE_TO_ENTRY)).toBe("string"); (0, import_vitest.expect)(_type_of(oneRouterMetro[1].ONE_ROUTER_REQUIRE_CONTEXT_REGEX_STRING)).toBe("string"); (0, import_vitest.expect)(plugins[4]).toBe("one/babel-plugin-inline-one-server-url"); }); (0, import_vitest.it)("threads setupFile through as a path relative to the metro entry", function () { var plugins = (0, import_index.buildOneBabelPlugins)({ projectRoot, relativeRouterRoot: "app", setupFile: "src/setup-native.ts", includeImportMetaEnv: false }); var oneRouterMetro = plugins[3]; (0, import_vitest.expect)(oneRouterMetro[1].ONE_SETUP_FILE_NATIVE).toMatch(/setup-native\.ts$/); }); (0, import_vitest.it)("passes the linking config through", function () { var linking = { prefixes: ["myapp://"] }; var plugins = (0, import_index.buildOneBabelPlugins)({ projectRoot, relativeRouterRoot: "app", linking, includeImportMetaEnv: false }); var oneRouterMetro = plugins[3]; (0, import_vitest.expect)(oneRouterMetro[1].ONE_ROUTER_LINKING_CONFIG).toBe(linking); }); (0, import_vitest.it)("emits forward-slash-only paths for `require.context` + `import` specifiers", function () { var plugins = (0, import_index.buildOneBabelPlugins)({ projectRoot, relativeRouterRoot: "app", setupFile: "src/setup-native.ts", includeImportMetaEnv: false }); var oneRouterMetro = plugins[3]; var appRoot = oneRouterMetro[1].ONE_ROUTER_APP_ROOT_RELATIVE_TO_ENTRY; var setupNative = oneRouterMetro[1].ONE_SETUP_FILE_NATIVE; (0, import_vitest.expect)(appRoot).not.toContain("\\"); (0, import_vitest.expect)(setupNative).not.toContain("\\"); }); }); //# sourceMappingURL=index.test.native.js.map