{"version":3,"file":"staticFiles.mjs","sources":["../../../src/node/staticFiles.ts"],"sourcesContent":["import fs from 'node:fs/promises';\nimport path from 'node:path';\nimport outdent from 'outdent';\nimport { createElement } from 'react';\nimport { renderToStaticMarkup } from 'react-dom/server';\nimport { DefaultDocument } from '@strapi/admin/_internal';\n\nimport type { BuildContext } from './create-build-context';\n\nconst getEntryModule = (ctx: BuildContext): string => {\n  const pluginsObject = ctx.plugins\n    .map(({ name, importName }) => `'${name}': ${importName}`)\n    .join(',\\n');\n\n  const pluginsImport = ctx.plugins\n    .map(({ importName, modulePath }) => `import ${importName} from '${modulePath}';`)\n    .join('\\n');\n\n  return outdent`\n        /**\n         * This file was automatically generated by Strapi.\n         * Any modifications made will be discarded.\n         */\n        ${pluginsImport}\n        import { renderAdmin } from \"@strapi/strapi/admin\"\n\n        ${\n          ctx.customisations?.modulePath\n            ? `import customisations from '${ctx.customisations.modulePath}'`\n            : ''\n        }\n\n        renderAdmin(\n          document.getElementById(\"strapi\"),\n          {\n            ${ctx.customisations?.modulePath ? 'customisations,' : ''}\n            ${ctx.features ? `features: ${JSON.stringify(ctx.features)},` : ''}\n            plugins: {\n        ${pluginsObject}\n            }\n        })\n      `;\n};\n\ninterface GetDocumentHTMLArgs extends Pick<BuildContext, 'logger'> {\n  props?: {\n    entryPath?: string;\n  };\n}\n\n/**\n * TODO: Here in the future we could add the ability\n * to load a user's Document component?\n */\nconst getDocumentHTML = ({ logger, props = {} }: GetDocumentHTMLArgs) => {\n  const result = renderToStaticMarkup(createElement(DefaultDocument, props));\n  logger.debug('Rendered the HTML');\n\n  return outdent`<!DOCTYPE html>${result}`;\n};\n\nconst AUTO_GENERATED_WARNING = `\nThis file was automatically generated by Strapi.\nAny modifications made will be discarded.\n`.trim();\n\n/**\n * Because we now auto-generate the index.html file,\n * we should be clear that people _should not_ modify it.\n *\n * @internal\n */\nconst decorateHTMLWithAutoGeneratedWarning = (htmlTemplate: string): string =>\n  htmlTemplate.replace(/<head/, `\\n<!--\\n${AUTO_GENERATED_WARNING}\\n-->\\n<head`);\n\nconst writeStaticClientFiles = async (ctx: BuildContext) => {\n  const prettier = await import('prettier'); // ESM-only\n\n  /**\n   * For everything to work effectively we create a client folder in `.strapi` at the cwd level.\n   * We then use the function we need to \"createAdmin\" as well as generate the Document index.html as well.\n   *\n   * All this links together an imaginary \"src/index\" that then allows vite to correctly build the admin panel.\n   */\n\n  await fs.mkdir(ctx.runtimeDir, { recursive: true });\n  ctx.logger.debug('Created the runtime directory');\n\n  const indexHtml = decorateHTMLWithAutoGeneratedWarning(\n    await getDocumentHTML({\n      logger: ctx.logger,\n      props:\n        ctx.bundler === 'vite'\n          ? {\n              entryPath: `/${ctx.entry}`,\n            }\n          : undefined,\n    })\n  );\n\n  await fs.writeFile(\n    path.join(ctx.runtimeDir, 'index.html'),\n    await prettier.format(indexHtml, {\n      parser: 'html',\n    })\n  );\n  ctx.logger.debug('Wrote the index.html file');\n  await fs.writeFile(\n    path.join(ctx.runtimeDir, 'app.js'),\n    await prettier.format(getEntryModule(ctx), {\n      parser: 'babel',\n    })\n  );\n  ctx.logger.debug('Wrote the app.js file');\n};\n\nexport { writeStaticClientFiles, getDocumentHTML };\n"],"names":["getEntryModule","ctx","pluginsObject","plugins","map","name","importName","join","pluginsImport","modulePath","outdent","customisations","features","JSON","stringify","getDocumentHTML","logger","props","result","renderToStaticMarkup","createElement","DefaultDocument","debug","AUTO_GENERATED_WARNING","trim","decorateHTMLWithAutoGeneratedWarning","htmlTemplate","replace","writeStaticClientFiles","prettier","fs","mkdir","runtimeDir","recursive","indexHtml","bundler","entryPath","entry","undefined","writeFile","path","format","parser"],"mappings":";;;;;;;AASA,MAAMA,iBAAiB,CAACC,GAAAA,GAAAA;IACtB,MAAMC,aAAAA,GAAgBD,IAAIE,OAAO,CAC9BC,GAAG,CAAC,CAAC,EAAEC,IAAI,EAAEC,UAAU,EAAE,GAAK,CAAC,CAAC,EAAED,IAAAA,CAAK,GAAG,EAAEC,UAAAA,CAA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