UNPKG

@strapi/strapi

Version:

An open source headless CMS solution to create and manage your own API. It provides a powerful dashboard and features to make your life easier. Databases supported: MySQL, MariaDB, PostgreSQL, SQLite

1 lines 6.47 kB
{"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,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