UNPKG

one

Version:

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

369 lines (368 loc) 16.5 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf, __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: !0 }); }, __copyProps = (to, from, except, desc) => { if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__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: !0 }) : target, mod )), __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod); var one_exports = {}; __export(one_exports, { one: () => one }); module.exports = __toCommonJS(one_exports); var import_compiler = require("@vxrn/compiler"), import_resolve = require("@vxrn/resolve"), import_node_events = __toESM(require("node:events"), 1), import_node_path = __toESM(require("node:path"), 1), import_vite_plugin_barrel = require("vite-plugin-barrel"), import_vite_tsconfig_paths = __toESM(require("vite-tsconfig-paths"), 1), import_vxrn = require("vxrn"), import_constants = require("../constants"), import_polyfills_server = require("../polyfills-server"), import_getRouterRootFromOneOptions = require("../utils/getRouterRootFromOneOptions"), import_ensureTsConfig = require("./ensureTsConfig"), import_loadConfig = require("./loadConfig"), import_clientTreeShakePlugin = require("./plugins/clientTreeShakePlugin"), import_fileSystemRouterPlugin = require("./plugins/fileSystemRouterPlugin"), import_fixDependenciesPlugin = require("./plugins/fixDependenciesPlugin"), import_generateFileSystemRouteTypesPlugin = require("./plugins/generateFileSystemRouteTypesPlugin"), import_SSRCSSPlugin = require("./plugins/SSRCSSPlugin"), import_virtualEntryConstants = require("./plugins/virtualEntryConstants"), import_virtualEntryPlugin = require("./plugins/virtualEntryPlugin"); import_node_events.default.setMaxListeners(1e3); globalThis.__vxrnEnableNativeEnv = !0; function one() { var options = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {}, _options_config, _options_optimization, _options_react, _options_ssr, _options_react1, _options_router_experimental, _options_router; if (!globalThis.__oneOptions) return (0, import_loadConfig.setOneOptions)(options), globalThis.__vxrnPluginConfig__ = options, []; (0, import_compiler.clearCompilerCache)(), ((_options_config = options.config) === null || _options_config === void 0 ? void 0 : _options_config.ensureTSConfig) !== !1 && (0, import_ensureTsConfig.ensureTSConfig)(); var { optimizeDeps } = (0, import_vxrn.getOptimizeDeps)("build"), optimizeIds = optimizeDeps.include, optimizeIdRegex = new RegExp( // santize ids for regex // https://stackoverflow.com/questions/6300183/sanitize-string-of-regex-characters-before-regexp-build `${optimizeIds.map(function(id) { return id.replace(/[#-.]|[[-^]|[?|{}]/g, "\\$&"); }).join("|")}` ), tsConfigPathsPlugin = null, vxrnOptions = (0, import_vxrn.getOptionsFilled)(), root = (vxrnOptions == null ? void 0 : vxrnOptions.root) || process.cwd(), barrelOption = (_options_optimization = options.optimization) === null || _options_optimization === void 0 ? void 0 : _options_optimization.barrel, compiler = (_options_react = options.react) === null || _options_react === void 0 ? void 0 : _options_react.compiler; compiler && (0, import_compiler.configureVXRNCompilerPlugin)({ enableCompiler: compiler === "native" ? [ "ios", "android" ] : compiler === "web" ? [ "ssr", "client" ] : !0 }); var autoDepsOptions = (_options_ssr = options.ssr) === null || _options_ssr === void 0 ? void 0 : _options_ssr.autoDepsOptimization, devAndProdPlugins = [ { name: "one:config", // @ts-ignore __get: options }, barrelOption === !1 ? null : (0, import_vite_plugin_barrel.barrel)({ packages: Array.isArray(barrelOption) ? barrelOption : [ "@tamagui/lucide-icons" ] }), { name: "one-define-client-env", async config(userConfig) { var _vxrnOptions_mode, { clientEnvDefine } = await (0, import_vxrn.loadEnv)((_vxrnOptions_mode = vxrnOptions == null ? void 0 : vxrnOptions.mode) !== null && _vxrnOptions_mode !== void 0 ? _vxrnOptions_mode : "development", process.cwd(), userConfig == null ? void 0 : userConfig.envPrefix); return { define: clientEnvDefine }; } }, ...autoDepsOptions === !1 ? [] : [ (0, import_vxrn.autoDepOptimizePlugin)({ onScannedDeps(param) { var { hasReanimated, hasNativewind } = param, _options_native, _options_native_css; (0, import_compiler.configureVXRNCompilerPlugin)({ enableReanimated: hasReanimated, enableNativeCSS: (_options_native_css = (_options_native = options.native) === null || _options_native === void 0 ? void 0 : _options_native.css) !== null && _options_native_css !== void 0 ? _options_native_css : hasNativewind, enableNativewind: hasNativewind }); }, root, include: /node_modules/, ...autoDepsOptions === !0 ? {} : autoDepsOptions }) ], // proxy because you cant add a plugin inside a plugin new Proxy({ name: "one:tsconfig-paths", config(configIncoming) { var _options_config2, _configIncoming_plugins, pathsConfig = (_options_config2 = options.config) === null || _options_config2 === void 0 ? void 0 : _options_config2.tsConfigPaths; pathsConfig !== !1 && (!((_configIncoming_plugins = configIncoming.plugins) === null || _configIncoming_plugins === void 0) && _configIncoming_plugins.flat().some(function(p) { return p && p.name === "vite-tsconfig-paths"; }) || (tsConfigPathsPlugin = (0, import_vite_tsconfig_paths.default)(pathsConfig && typeof pathsConfig == "object" ? pathsConfig : {}))); }, configResolved() { }, resolveId() { } }, { get(target, key, thisArg) { if (key === "config" || key === "name") return Reflect.get(target, key, thisArg); if (tsConfigPathsPlugin) return Reflect.get(tsConfigPathsPlugin, key, thisArg); } }), { name: "one-aliases", enforce: "pre", config() { var tslibLitePath = ""; try { tslibLitePath = (0, import_resolve.resolvePath)("@vxrn/tslib-lite", process.cwd()); } catch (err) { console.info("Can't find tslib-lite, falling back to tslib"), process.env.DEBUG && console.error(err); } return { resolve: { alias: { // testing getting transition between routes working // 'use-sync-external-store/with-selector': resolvePath( // 'use-sync-external-store/shim/with-selector' // ), ...tslibLitePath && { tslib: tslibLitePath } } } }; } }, // [ // { // find: /tslib/, // replacement: resolvePath('@vxrn/tslib-lite'), // }, // // not working but would save ~30Kb stat // // { // // find: /@react-navigation\/core.*\/getStateFromPath/, // // replacement: join(forkPath, 'fork', 'getStateFromPath.mjs'), // // }, // // { // // find: /@react-navigation\/core.*\/getPathFromState/, // // replacement: join(forkPath, 'fork', 'getPathFromState.mjs'), // // }, // ], { name: "one:init-config", config() { var _options_web; return { define: { ...((_options_web = options.web) === null || _options_web === void 0 ? void 0 : _options_web.defaultRenderMode) && { "process.env.ONE_DEFAULT_RENDER_MODE": JSON.stringify(options.web.defaultRenderMode), "import.meta.env.ONE_DEFAULT_RENDER_MODE": JSON.stringify(options.web.defaultRenderMode) }, ...options.setupFile && { "process.env.ONE_SETUP_FILE": JSON.stringify(options.setupFile) }, ...process.env.NODE_ENV !== "production" && vxrnOptions && { "process.env.ONE_SERVER_URL": JSON.stringify(vxrnOptions.server.url), "import.meta.env.ONE_SERVER_URL": JSON.stringify(vxrnOptions.server.url) } }, environments: { client: { define: { "process.env.VITE_ENVIRONMENT": '"client"', "process.env.TAMAGUI_ENVIRONMENT": '"client"', "import.meta.env.VITE_ENVIRONMENT": '"client"', "process.env.EXPO_OS": '"web"' } }, ssr: { define: { "process.env.VITE_ENVIRONMENT": '"ssr"', "process.env.TAMAGUI_ENVIRONMENT": '"ssr"', "import.meta.env.VITE_ENVIRONMENT": '"ssr"', "process.env.EXPO_OS": '"web"' } }, ios: { define: { "process.env.VITE_ENVIRONMENT": '"ios"', "process.env.TAMAGUI_ENVIRONMENT": '"ios"', "import.meta.env.VITE_ENVIRONMENT": '"ios"', "process.env.EXPO_OS": '"ios"' } }, android: { define: { "process.env.VITE_ENVIRONMENT": '"android"', "process.env.TAMAGUI_ENVIRONMENT": '"android"', "import.meta.env.VITE_ENVIRONMENT": '"android"', "process.env.EXPO_OS": '"android"' } } } }; } }, { name: "one:tamagui", config() { return { define: { // safe to set because it only affects web in tamagui, and one is always react 19 "process.env.TAMAGUI_REACT_19": '"1"', "process.env.TAMAGUI_SKIP_THEME_OPTIMIZATION": '"1"' }, environments: { ssr: { define: { "process.env.TAMAGUI_IS_SERVER": '"1"', "process.env.TAMAGUI_KEEP_THEMES": '"1"' } }, ios: { define: { "process.env.TAMAGUI_KEEP_THEMES": '"1"' } }, android: { define: { "process.env.TAMAGUI_KEEP_THEMES": '"1"' } } } }; } }, { name: "route-module-hmr-fix", hotUpdate(param) { var { server, modules } = param; return modules.map(function(m) { var { id } = m; if (!id) return m; var relativePath = import_node_path.default.relative(server.config.root, id), rootDir = relativePath.split(import_node_path.default.sep)[0]; return rootDir === "app" && (m.acceptedHmrExports = /* @__PURE__ */ new Set()), m; }); } }, // Plugins may transform the source code and add imports of `react/jsx-dev-runtime`, which won't be discovered by Vite's initial `scanImports` since the implementation is using ESbuild where such plugins are not executed. // Thus, if the project has a valid `react/jsx-dev-runtime` import, we tell Vite to optimize it, so Vite won't only discover it on the next page load and trigger a full reload. { name: "one:optimize-dev-deps", config(_, env) { if (env.mode === "development") return { optimizeDeps: { include: [ "react/jsx-dev-runtime", "react/compiler-runtime" ] } }; } }, { name: "one:remove-server-from-client", enforce: "pre", transform(code, id) { if (this.environment.name === "client" && id.includes("one-server-only")) return code.replace('import { AsyncLocalStorage } from "node:async_hooks"', "class AsyncLocalStorage {}"); } } ], scan = (_options_react1 = options.react) === null || _options_react1 === void 0 ? void 0 : _options_react1.scan, reactScanPlugin = { name: "one:react-scan", config() { return reactScanConfig; } }; devAndProdPlugins.push(reactScanPlugin); var reactScanConfig = function() { var stringify = function(obj) { return JSON.stringify(JSON.stringify(obj)); }, configs = { disabled: { define: { "process.env.ONE_ENABLE_REACT_SCAN": '""' } }, enabled: { define: { "process.env.ONE_ENABLE_REACT_SCAN": stringify({ enabled: !0, animationSpeed: "slow", showToolbar: !1 }) } } }, getConfigFor = function(platform) { if (process.env.NODE_ENV === "production" || !scan) return configs.disabled; if (scan === !0) return configs.enabled; if (typeof scan == "string") return scan === "native" && platform === "client" || scan === "web" && platform !== "client" ? configs.disabled : configs.enabled; var defaultConfig = scan.options || configs.enabled, perPlatformConfig = platform === "ios" || platform === "android" ? scan.native : scan.web; return { define: { "process.env.ONE_ENABLE_REACT_SCAN": stringify({ ...defaultConfig, ...perPlatformConfig }) } }; }; return { environments: { client: getConfigFor("client"), ios: getConfigFor("ios"), android: getConfigFor("android") } }; }(), nativeWebDevAndProdPlugsin = [ (0, import_clientTreeShakePlugin.clientTreeShakePlugin)(), reactScanPlugin ]; globalThis.__vxrnAddNativePlugins = nativeWebDevAndProdPlugsin, globalThis.__vxrnAddWebPluginsProd = devAndProdPlugins; var flags = { experimentalPreventLayoutRemounting: (_options_router = options.router) === null || _options_router === void 0 || (_options_router_experimental = _options_router.experimental) === null || _options_router_experimental === void 0 ? void 0 : _options_router_experimental.preventLayoutRemounting }, routerRoot = (0, import_getRouterRootFromOneOptions.getRouterRootFromOneOptions)(options); return [ ...devAndProdPlugins, ...nativeWebDevAndProdPlugsin, /** * This is really the meat of one, where it handles requests: */ (0, import_fileSystemRouterPlugin.createFileSystemRouterPlugin)(options), (0, import_generateFileSystemRouteTypesPlugin.generateFileSystemRouteTypesPlugin)(options), (0, import_fixDependenciesPlugin.fixDependenciesPlugin)(options.deps), (0, import_virtualEntryPlugin.createVirtualEntry)({ ...options, flags, root: routerRoot }), { name: "one-define-environment", config() { var _options_native; return { define: { ...((_options_native = options.native) === null || _options_native === void 0 ? void 0 : _options_native.key) && { "process.env.ONE_APP_NAME": JSON.stringify(options.native.key), "import.meta.env.ONE_APP_NAME": JSON.stringify(options.native.key) }, "process.env.ONE_CACHE_KEY": JSON.stringify(import_constants.CACHE_KEY), "import.meta.env.ONE_CACHE_KEY": JSON.stringify(import_constants.CACHE_KEY) } }; } }, (0, import_SSRCSSPlugin.SSRCSSPlugin)({ entries: [ import_virtualEntryConstants.virtualEntryId ] }) ]; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { one }); //# sourceMappingURL=one.js.map