@stencil/react-output-target
Version:
React output target for @stencil/core components.
162 lines (160 loc) • 6.18 kB
JavaScript
import { readFile as v, access as P, mkdir as b, writeFile as f } from "node:fs/promises";
import { join as c, dirname as j, relative as T } from "node:path";
async function V(t, s) {
try {
const { name: o } = JSON.parse(await v(c(t, "package.json"), "utf8"));
return typeof o == "string" && o ? o : s;
} catch {
return s;
}
}
async function O(t) {
try {
return await P(t), !0;
} catch {
return !1;
}
}
async function W(t, s, o, p) {
await b(c(t, "src"), { recursive: !0 });
const m = {
name: s,
version: "0.0.1",
type: "module",
main: "./dist/index.js",
types: "./dist/index.d.ts",
exports: { ".": { types: "./dist/index.d.ts", import: "./dist/index.js" } },
scripts: { build: "tsc", dev: "tsc --watch" },
peerDependencies: {
react: "^18 || ^19",
"react-dom": "^18 || ^19"
},
dependencies: { [o]: p },
devDependencies: { typescript: ">=4 <7" }
}, y = {
compilerOptions: {
target: "ES2022",
module: "esnext",
moduleResolution: "bundler",
declaration: !0,
rootDir: "src",
outDir: "dist",
strict: !0,
skipLibCheck: !0
},
include: ["src/**/*.ts", "src/**/*.tsx"]
};
await Promise.all([
f(c(t, ".gitignore"), `dist/
node_modules/
`, "utf8"),
f(c(t, "package.json"), JSON.stringify(m, null, 2) + `
`, "utf8"),
f(c(t, "tsconfig.json"), JSON.stringify(y, null, 2) + `
`, "utf8"),
f(c(t, "src", "index.ts"), `// Re-exports React wrappers generated by @stencil/react-output-target.
// src/components.ts is populated by \`stencil build\` — do not edit manually.
export * from './components.js';
`, "utf8")
]);
}
const F = {
init: {
id: "@stencil/react-output-target",
displayName: "React",
description: "Type-safe React wrappers for your Stencil components",
async run({ config: t, workspaceRoot: s, prompts: o, nypm: p, openStencilConfig: m }) {
const { intro: y, outro: h, text: w, confirm: k, spinner: N, isCancel: d, cancel: l, log: u } = o;
y("React output target");
const r = await m();
if (r.outputTargetsContains("reactOutputTarget(")) {
const e = await k({
message: "reactOutputTarget is already in stencil.config.ts — reconfigure?",
initialValue: !1
});
if (d(e) || !e) {
l("Skipping React setup.");
return;
}
}
let n;
const C = await V(t.rootDir, t.fsNamespace);
let i = `${t.fsNamespace}-react`;
if (s) {
const e = await w({
message: "Wrapper package name?",
placeholder: i,
defaultValue: i
});
if (d(e)) {
l("Setup cancelled.");
return;
}
i = e, n = c(j(t.rootDir), "react");
} else {
const e = `../${t.fsNamespace}-react`, a = await w({
message: "Wrapper package directory? (relative to stencil.config.ts)",
placeholder: e,
defaultValue: e
});
if (d(a)) {
l("Setup cancelled.");
return;
}
n = c(t.rootDir, a);
}
const x = T(t.rootDir, c(n, "src")).replace(/\\/g, "/"), g = await k({ message: "Enable server-side rendering (SSR)?", initialValue: !1 });
if (d(g)) {
l("Setup cancelled.");
return;
}
if (g && !s) {
const e = await w({
message: "React wrapper npm package name? (used as clientModule in the SSR config)",
placeholder: i,
defaultValue: i
});
if (d(e)) {
l("Setup cancelled.");
return;
}
i = e;
}
const D = !await O(n);
if (D) {
const e = N();
e.start(`Scaffolding wrapper package at ${T(s ?? t.rootDir, n)}`);
try {
const a = s ? await p.detectPackageManager(s) : void 0, $ = (a == null ? void 0 : a.name) === "pnpm" || (a == null ? void 0 : a.name) === "bun" || (a == null ? void 0 : a.name) === "yarn" && a.majorVersion !== "1" ? "workspace:*" : `file:${T(n, t.rootDir).replace(/\\/g, "/")}`;
await W(n, i, C, $), e.stop("Wrapper package scaffolded");
} catch (a) {
e.stop("Scaffolding failed — continuing"), u.warn(`Could not scaffold wrapper package: ${a}`);
}
}
const R = t.generateExportMaps === !0 ? "ssr" : "dist/ssr", S = g ? `reactOutputTarget({
outDir: '${x}',
hydrateModule: '${C}/${R}',
clientModule: '${i}',
})` : `reactOutputTarget({ outDir: '${x}' })`;
try {
r.addImport("@stencil/react-output-target", ["reactOutputTarget"]), r.outputTargetsContains("type: 'standalone'") || r.outputTargetsContains('type: "standalone"') || r.outputTargetsContains("type: 'dist-custom-elements'") || r.outputTargetsContains('type: "dist-custom-elements"') || r.addOutputTarget("{ type: 'standalone' }"), g && (r.outputTargetsContains("type: 'ssr'") || r.outputTargetsContains('type: "ssr"') || r.outputTargetsContains("type: 'dist-hydrate-script'") || r.outputTargetsContains('type: "dist-hydrate-script"') || r.addOutputTarget("{ type: 'ssr' }")), r.replaceOutputTarget("reactOutputTarget(", S) || r.addOutputTarget(S), await r.save(), u.success("stencil.config.ts updated");
} catch (e) {
u.warn(
`Could not automatically update stencil.config.ts (${e}). Add manually:
import { reactOutputTarget } from '@stencil/react-output-target';
// in outputTargets:
${S}` + (g ? `
{ type: 'ssr' }` : "")
);
}
u.info("Installing @stencil/react-output-target..."), await p.addDependency(["@stencil/react-output-target"], { cwd: t.rootDir, dev: !0 }), u.success("Installed @stencil/react-output-target"), D && await O(n) && (u.info("Installing React peer dependencies in wrapper package..."), await p.addDependency(["@stencil/react-output-target"], { cwd: n }), await p.addDependency(["react", "react-dom", "@types/react", "@types/react-dom", "typescript"], {
cwd: n,
dev: !0
}), u.success("React peer dependencies installed")), h("React output target configured");
}
}
};
export {
F as wizard
};
//# sourceMappingURL=wizard.js.map