one
Version:
One is a new React Framework that makes Vite serve both native and web.
125 lines (124 loc) • 4.05 kB
JavaScript
import fs from "fs";
import path from "path";
import { virtualEntryId } from "./virtualEntryConstants.native.js";
var MIME = {
png: "image/png",
jpg: "image/jpeg",
jpeg: "image/jpeg",
gif: "image/gif",
webp: "image/webp",
avif: "image/avif",
bmp: "image/bmp",
ico: "image/x-icon"
};
var ASSET_EXT_RE = /\.(png|jpe?g|gif|webp|avif|bmp|ico)/;
function inlineAssetImports(code, id) {
if (!ASSET_EXT_RE.test(code)) return null;
var importRe = /import\s+(\w+)\s+from\s*['"]([^'"?]+\.(?:png|jpe?g|gif|webp|avif|bmp|ico))['"]\s*;?/g;
var base = id.split("?")[0];
var result = code;
var changed = false;
var m;
var edits = [];
while (m = importRe.exec(code)) {
var [full, name, spec] = m;
var abs = spec.startsWith(".") ? path.resolve(path.dirname(base), spec) : null;
if (!abs || !fs.existsSync(abs)) continue;
var ext = path.extname(abs).slice(1).toLowerCase();
var uri = `data:${MIME[ext] || "application/octet-stream"};base64,${fs.readFileSync(abs).toString("base64")}`;
edits.push({
full,
name,
uri
});
}
var _iteratorNormalCompletion = true,
_didIteratorError = false,
_iteratorError = void 0;
try {
for (var _iterator = edits[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var e = _step.value;
result = result.replace(e.full, `const ${e.name} = ${JSON.stringify(e.uri)};`);
changed = true;
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return != null) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return changed ? {
code: result,
map: null
} : null;
}
function webBundledAssetPlugin() {
return {
name: "one:web-bundled-asset",
transform: function (code, id) {
return inlineAssetImports(code, id);
}
};
}
function bundledDevPlugin(enabled) {
return {
name: "one:bundled-dev",
// mirror the client's asset inlining on the server env so SSR emits the same
// data-uris (the rolldown client plugin above can't reach the ssr pipeline).
// bundledDev is off by default, so only register the hook when it's on, and
// let rust pre-filter on the asset extensions inlineAssetImports looks for.
...(enabled ? {
transform: {
filter: {
code: ASSET_EXT_RE
},
handler(code, id) {
var env = this.environment;
if (!env || env.name !== "ssr" || env.mode !== "dev") return;
return inlineAssetImports(code, id);
}
}
} : {}),
config(_userConfig, env) {
if (!enabled || env.command !== "serve") return;
return {
experimental: {
bundledDev: true
},
environments: {
client: {
build: {
rolldownOptions: {
// bundle the virtual entry so FullBundleDev serves it at
// /assets/_virtual_one-entry.js
input: [virtualEntryId],
plugins: [webBundledAssetPlugin()],
experimental: {
// rolldown 1.1.0 defaulted lazyBarrel ON, which breaks One's
// index.mjs barrel re-exports under FullBundleDev (e.g. `Slot`
// resolves to undefined). pin it off.
lazyBarrel: false
},
// react-native packages import native-only exports
// (codegenNativeComponent, TurboModuleRegistry, …) from react-native
// → aliased to react-native-web, which doesn't export them. shim as
// undefined instead of failing the build (matches the dep optimizer).
shimMissingExports: true
}
}
}
}
};
}
};
}
export { bundledDevPlugin };
//# sourceMappingURL=bundledDevPlugin.native.js.map