hydrogen-sanity
Version:
Sanity.io toolkit for Hydrogen
60 lines (58 loc) • 2.12 kB
JavaScript
function sanity() {
return {
name: "sanity",
enforce: "pre",
// Run before other plugins to intercept @sanity/react-loader resolution
async config() {
return {
envPrefix: ["SANITY_STUDIO_"],
ssr: {
optimizeDeps: {
// Pre-bundle Sanity dependencies for better SSR performance
include: ["@sanity/client"]
},
// Prevent externalization of Sanity dependencies to ensure proper ESM resolution
noExternal: ["@sanity/client"]
}
};
},
configResolved(resolvedConfig) {
const ssrConditions = [
"es2015",
...(resolvedConfig.ssr?.resolve?.conditions || []).filter(
(condition) => condition !== "es2015" && condition !== "node" && condition !== "require"
)
];
if (resolvedConfig.ssr?.resolve) {
resolvedConfig.ssr.resolve.conditions = ssrConditions;
}
if (resolvedConfig.environments?.ssr?.resolve?.conditions) {
const envConditions = [
"es2015",
...resolvedConfig.environments.ssr.resolve.conditions.filter(
(condition) => condition !== "es2015" && condition !== "node" && condition !== "require"
)
];
resolvedConfig.environments.ssr.resolve.conditions = envConditions;
}
},
// Surgically redirect @sanity/react-loader to its server bundle during SSR
// The browser bundle doesn't export server-only functions like setServerClient/loadQuery
// Without this, Hydrogen/Oxygen's 'browser' condition causes the wrong bundle to load
async resolveId(id, importer, options) {
if (id === "@sanity/react-loader" && options.ssr) {
const pkgResolved = await this.resolve("@sanity/react-loader/package.json", importer, {
...options,
skipSelf: true
});
if (pkgResolved) {
const serverBundle = pkgResolved.id.replace("/package.json", "/dist/index.js");
return { id: serverBundle };
}
}
return null;
}
};
}
export { sanity };
//# sourceMappingURL=index.js.map