import md5 from 'md5'; import postcss, { AnyNode, Builder, Stringifier } from 'postcss'; import { buildImportInjectStylesStatement } from './builders/import-statement'; import { buildInstantStylesStatement, buildLazyStylesStatement, buildOnDemandStylesStatement } from './builders/styles-statement'; import { Options } from './options'; import { readCodeForEmbed } from './utils/read-code-for-embed'; import { prepareRuntimeOptions } from './utils/runtime-options'; let embedCode: string | undefined; export const createStringify = (options: Required): Stringifier => { const { attachOriginalClassName } = options.modules; const { script, scriptType, moduleType, custom } = options.inject; const { importStatement, injectStatement } = custom || {}; const runtimeOptions = prepareRuntimeOptions(options); return (node: AnyNode, builder: Builder) => { /* istanbul ignore else */ if (node.type === 'root') { let classMap: Record | undefined; // looking for module-map comment node.walkComments(comment => { const cmt = comment.text; if (cmt.startsWith('modules-map:')) { classMap = JSON.parse(cmt.replace('modules-map:', '')); } }); // we handling only roots which contains module-map /* istanbul ignore else */ if (classMap) { // clone for removing comments, as we do not want to touch original root const cloned = node.clone(); // generate stylesheet unique key const sheetKey = md5(cloned.toString()); cloned.walkComments(c => { c.remove() }); // header text builder(`// File generated by the postcss-es-modules plugin. Please do not modify it !!!\n`) if (options.inject.injectMode !== 'none') { // if not none injectMode if (script === 'embed') { // if we have to embed injector code /* istanbul ignore else */ if (!embedCode) { // if code to inject is not loaded yet // we will read it embedCode = readCodeForEmbed(scriptType, moduleType); } // print in the injector code builder(embedCode, node); } else { // import statement if (importStatement) { // if there is custom importStatement, we will use it builder(importStatement, cloned); } else { // if not embedding, we will import the css-es-modules lib buildImportInjectStylesStatement(cloned, builder, options.inject); } } } // if there are custom runtime options // we will add them to runtime if (runtimeOptions) { builder(`const options = ${JSON.stringify(runtimeOptions)};\n`); } // key of stylesheet builder(`const key = '${sheetKey}';`, cloned); builder("\n"); // raw css body builder('const css =`', cloned); postcss.stringify(cloned, builder); builder('`;', cloned); builder("\n"); // styles object if (options.inject.injectMode === 'instant') { buildInstantStylesStatement( builder, cloned, classMap, injectStatement, !!attachOriginalClassName, !!runtimeOptions); } else if (options.inject.injectMode === 'ondemand') { buildOnDemandStylesStatement( builder, cloned, classMap, injectStatement, !!attachOriginalClassName, !!runtimeOptions); } else if (options.inject.injectMode === 'none') { buildOnDemandStylesStatement( builder, cloned, classMap, "throw \"This stylesheet can't be injected, instead please use exported css constant.\"", !!attachOriginalClassName, !!runtimeOptions); } else { buildLazyStylesStatement( builder, cloned, classMap, injectStatement, !!attachOriginalClassName, !!runtimeOptions); } // export statement if (moduleType === 'esm') { builder(`export { styles, css, key };`, cloned); builder("\n"); builder(`export default styles;`, cloned); builder("\n"); } else { builder(`module.exports = { styles, css, key, default: styles };`, cloned); builder("\n"); } return; } } // in any other case we will use default stringify postcss.stringify(node, builder); } }