@stencil/react-output-target
Version:
React output target for @stencil/core components.
22 lines (20 loc) • 5.92 kB
JavaScript
;Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d=require("ts-morph"),S=require("node:fs/promises"),n=require("node:path");function K(e,l,w){var x;const i=new d.Project({skipAddingFilesFromTsConfig:!0}).addSourceFileAtPath(e);if(i.getImportDeclaration("@stencil/react-output-target")||i.addImportDeclaration({moduleSpecifier:"@stencil/react-output-target",namedImports:["reactOutputTarget"]}),!i.getDescendantsOfKind(d.SyntaxKind.PropertyAssignment).find(a=>a.getName()==="outputTargets")){const a=((x=i.getVariableDeclaration("config"))==null?void 0:x.getInitializerIfKind(d.SyntaxKind.ObjectLiteralExpression))??i.getDescendantsOfKind(d.SyntaxKind.ObjectLiteralExpression).find(T=>T.getProperty("namespace")!==void 0);if(!a)throw new Error("Could not find Stencil config object in stencil.config.ts");a.addPropertyAssignment({name:"outputTargets",initializer:"[]"})}const p=i.getDescendantsOfKind(d.SyntaxKind.PropertyAssignment).find(a=>a.getName()==="outputTargets"),m=p.getInitializerIfKind(d.SyntaxKind.ArrayLiteralExpression);if(!m)throw new Error("outputTargets is not an array literal in stencil.config.ts");const r=m.getText(),g=r.includes("reactOutputTarget("),s=m.getElements().map(a=>a.getText().trim()).filter(a=>!a.includes("reactOutputTarget("));return r.includes("type: 'standalone'")||r.includes('type: "standalone"')||r.includes("type: 'dist-custom-elements'")||r.includes('type: "dist-custom-elements"')||s.push("{ type: 'standalone' }"),w&&(r.includes("type: 'ssr'")||r.includes('type: "ssr"')||r.includes("type: 'dist-hydrate-script'")||r.includes('type: "dist-hydrate-script"')||s.push("{ type: 'ssr' }")),s.push(l),p.setInitializer(`[
${s.map(a=>` ${a}`).join(`,
`)},
]`),i.formatText(),i.saveSync(),!g}async function v(e){try{return await S.access(e),!0}catch{return!1}}async function C(e,l,w,f){await S.mkdir(n.join(e,"src"),{recursive:!0});const i={name:l,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:{[w]:f},devDependencies:{typescript:">=4 <7"}},j={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([S.writeFile(n.join(e,".gitignore"),`dist/
node_modules/
`,"utf8"),S.writeFile(n.join(e,"package.json"),JSON.stringify(i,null,2)+`
`,"utf8"),S.writeFile(n.join(e,"tsconfig.json"),JSON.stringify(j,null,2)+`
`,"utf8"),S.writeFile(n.join(e,"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:e,workspaceRoot:l,prompts:w,nypm:f}){var k,b;const{intro:i,outro:j,text:p,confirm:m,spinner:r,isCancel:g,cancel:s,log:u}=w;i("React output target");const x=n.join(e.rootDir,"stencil.config.ts");if((b=(k=new d.Project({skipAddingFilesFromTsConfig:!0}).addSourceFileAtPath(x).getDescendantsOfKind(d.SyntaxKind.PropertyAssignment).find(t=>t.getName()==="outputTargets"))==null?void 0:k.getInitializerIfKind(d.SyntaxKind.ArrayLiteralExpression))==null?void 0:b.getText().includes("reactOutputTarget(")){const t=await m({message:"reactOutputTarget is already in stencil.config.ts — reconfigure?",initialValue:!1});if(g(t)||!t){s("Skipping React setup.");return}}let c,o=`${e.fsNamespace}-react`;if(l){const t=await p({message:"Wrapper package name?",placeholder:o,defaultValue:o});if(g(t)){s("Setup cancelled.");return}o=t,c=n.join(n.dirname(e.rootDir),o)}else{const t=`../${e.fsNamespace}-react`,y=await p({message:"Wrapper package directory? (relative to stencil.config.ts)",placeholder:t,defaultValue:t});if(g(y)){s("Setup cancelled.");return}c=n.join(e.rootDir,y)}const D=n.relative(e.rootDir,n.join(c,"src")).replace(/\\/g,"/"),h=await m({message:"Enable server-side rendering (SSR)?",initialValue:!1});if(g(h)){s("Setup cancelled.");return}if(h&&!l){const t=await p({message:"React wrapper npm package name? (used as clientModule in the SSR config)",placeholder:o,defaultValue:o});if(g(t)){s("Setup cancelled.");return}o=t}const O=!await v(c);if(O){const t=r();t.start(`Scaffolding wrapper package at ${n.relative(l??e.rootDir,c)}`);try{const y=l?"workspace:*":`file:${n.relative(c,e.rootDir).replace(/\\/g,"/")}`;await C(c,o,e.fsNamespace,y),t.stop("Wrapper package scaffolded")}catch(y){t.stop("Scaffolding failed — continuing"),u.warn(`Could not scaffold wrapper package: ${y}`)}}const P=h?`reactOutputTarget({
outDir: '${D}',
hydrateModule: '${e.fsNamespace}/dist/ssr',
clientModule: '${o}',
})`:`reactOutputTarget({ outDir: '${D}' })`;try{const t=K(x,P,h);u.success(t?"stencil.config.ts updated":"reactOutputTarget already present — no changes made")}catch(t){u.warn(`Could not automatically update stencil.config.ts (${t}). Add manually:
import { reactOutputTarget } from '@stencil/react-output-target';
// in outputTargets:
${P}`+(h?`
{ type: 'ssr' }`:""))}u.info("Installing @stencil/react-output-target..."),await f.addDependency(["@stencil/react-output-target"],{cwd:e.rootDir,dev:!0}),u.success("Installed @stencil/react-output-target"),O&&await v(c)&&(u.info("Installing React peer dependencies in wrapper package..."),await f.addDependency(["@stencil/react-output-target"],{cwd:c,dev:!0}),await f.addDependency(["react","react-dom","@types/react","@types/react-dom","typescript"],{cwd:c,dev:!0}),u.success("React peer dependencies installed")),j("React output target configured")}}};exports.wizard=I;
//# sourceMappingURL=wizard.cjs.map