import l from"@babel/core";import f from"node:fs";import d from"node:path";import m from"babel-plugin-styled-components";import u from"crypto";import{babelCache as p}from"../babel-cache.js";import{isDevelopMode as h}from"../../utils/is-develop-mode.js";const P=()=>({name:"styled-components-ssr",setup:i=>{const c=process.cwd();i.onLoad({filter:/\.[tj]sx?$/},async t=>{if(t.path.endsWith(`${d.sep}replay.js`))return;let e=t.path.split(".").pop();const s=p.get(t.path);if(s&&t.path.includes("/node_modules/"))return{contents:s.contents,loader:e};let n=["importMeta","topLevelAwait","classProperties","classPrivateProperties","classPrivateMethods","jsx"];(e==="tsx"||e==="ts")&&n.push("typescript");let o=await f.promises.readFile(t.path,"utf8");if(o.indexOf("styled-components")===-1)return{contents:o,loader:e};const a=u.createHash("sha1").update(o).digest("base64");if(s&&s.hash===a)return{contents:s.contents,loader:e};try{const r=(await l.transformAsync(o,{babelrc:!1,configFile:!1,ast:!1,root:c,filename:t.path,parserOpts:{allowAwaitOutsideFunction:!0,plugins:n},generatorOpts:{decoratorsBeforeExport:!0},plugins:[m],sourceMaps:!1}))?.code||void 0;return p.set(t.path,{contents:r,hash:a}),{contents:r,loader:e}}catch(r){if(!h())throw r;return{contents:o,loader:e}}})}});export{P as StyledComponentsSSR};
