UNPKG

@stencil/react-output-target

Version:

React output target for @stencil/core components.

152 lines (150 loc) 5.91 kB
import { access as R, mkdir as $, writeFile as g } from "node:fs/promises"; import { join as i, dirname as N, relative as S } from "node:path"; async function D(t) { try { return await R(t), !0; } catch { return !1; } } async function b(t, c, f, u) { await $(i(t, "src"), { recursive: !0 }); const m = { name: c, 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: { [f]: u }, 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([ g(i(t, ".gitignore"), `dist/ node_modules/ `, "utf8"), g(i(t, "package.json"), JSON.stringify(m, null, 2) + ` `, "utf8"), g(i(t, "tsconfig.json"), JSON.stringify(y, null, 2) + ` `, "utf8"), g(i(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 I = { init: { id: "@stencil/react-output-target", displayName: "React", description: "Type-safe React wrappers for your Stencil components", async run({ config: t, workspaceRoot: c, prompts: f, nypm: u, openStencilConfig: m }) { const { intro: y, outro: h, text: w, confirm: k, spinner: O, isCancel: d, cancel: p, log: o } = f; y("React output target"); const s = await m(); if (s.outputTargetsContains("reactOutputTarget(")) { const e = await k({ message: "reactOutputTarget is already in stencil.config.ts — reconfigure?", initialValue: !1 }); if (d(e) || !e) { p("Skipping React setup."); return; } } let r, n = `${t.fsNamespace}-react`; if (c) { const e = await w({ message: "Wrapper package name?", placeholder: n, defaultValue: n }); if (d(e)) { p("Setup cancelled."); return; } n = e, r = i(N(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)) { p("Setup cancelled."); return; } r = i(t.rootDir, a); } const C = S(t.rootDir, i(r, "src")).replace(/\\/g, "/"), l = await k({ message: "Enable server-side rendering (SSR)?", initialValue: !1 }); if (d(l)) { p("Setup cancelled."); return; } if (l && !c) { const e = await w({ message: "React wrapper npm package name? (used as clientModule in the SSR config)", placeholder: n, defaultValue: n }); if (d(e)) { p("Setup cancelled."); return; } n = e; } const x = !await D(r); if (x) { const e = O(); e.start(`Scaffolding wrapper package at ${S(c ?? t.rootDir, r)}`); try { const a = c ? await u.detectPackageManager(c) : void 0, v = (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:${S(r, t.rootDir).replace(/\\/g, "/")}`; await b(r, n, t.fsNamespace, v), e.stop("Wrapper package scaffolded"); } catch (a) { e.stop("Scaffolding failed — continuing"), o.warn(`Could not scaffold wrapper package: ${a}`); } } const T = l ? `reactOutputTarget({ outDir: '${C}', hydrateModule: '${t.fsNamespace}/dist/ssr', clientModule: '${n}', })` : `reactOutputTarget({ outDir: '${C}' })`; try { s.addImport("@stencil/react-output-target", ["reactOutputTarget"]), s.outputTargetsContains("type: 'standalone'") || s.outputTargetsContains('type: "standalone"') || s.outputTargetsContains("type: 'dist-custom-elements'") || s.outputTargetsContains('type: "dist-custom-elements"') || s.addOutputTarget("{ type: 'standalone' }"), l && (s.outputTargetsContains("type: 'ssr'") || s.outputTargetsContains('type: "ssr"') || s.outputTargetsContains("type: 'dist-hydrate-script'") || s.outputTargetsContains('type: "dist-hydrate-script"') || s.addOutputTarget("{ type: 'ssr' }")), s.replaceOutputTarget("reactOutputTarget(", T) || s.addOutputTarget(T), await s.save(), o.success("stencil.config.ts updated"); } catch (e) { o.warn( `Could not automatically update stencil.config.ts (${e}). Add manually: import { reactOutputTarget } from '@stencil/react-output-target'; // in outputTargets: ${T}` + (l ? ` { type: 'ssr' }` : "") ); } o.info("Installing @stencil/react-output-target..."), await u.addDependency(["@stencil/react-output-target"], { cwd: t.rootDir, dev: !0 }), o.success("Installed @stencil/react-output-target"), x && await D(r) && (o.info("Installing React peer dependencies in wrapper package..."), await u.addDependency(["@stencil/react-output-target"], { cwd: r, dev: !0 }), await u.addDependency(["react", "react-dom", "@types/react", "@types/react-dom", "typescript"], { cwd: r, dev: !0 }), o.success("React peer dependencies installed")), h("React output target configured"); } } }; export { I as wizard }; //# sourceMappingURL=wizard.js.map