Build CSS File
(manifest, minifyCSS)
| 81 | |
| 82 | /** Build CSS File */ |
| 83 | function buildImportCSS(manifest, minifyCSS) { |
| 84 | // gather list of imported CSS files |
| 85 | const allCSSFiles = new Set(); |
| 86 | for (const f in manifest) { |
| 87 | manifest[f].js.forEach((js) => { |
| 88 | if (!js.endsWith('.css.proxy.js')) return; |
| 89 | const isCSSModule = js.endsWith('.module.css.proxy.js'); |
| 90 | allCSSFiles.add(isCSSModule ? js : js.replace(/\.proxy\.js$/, '')); |
| 91 | }); |
| 92 | } |
| 93 | |
| 94 | // read + concat |
| 95 | let code = ''; |
| 96 | allCSSFiles.forEach((file) => { |
| 97 | const contents = fs.readFileSync(file, 'utf-8'); |
| 98 | |
| 99 | if (file.endsWith('.module.css.proxy.js')) { |
| 100 | // css modules |
| 101 | const matches = contents.match(/^export let code = *(.*)$/m); |
| 102 | if (matches && matches[1]) |
| 103 | code += |
| 104 | '\n' + |
| 105 | matches[1] |
| 106 | .trim() |
| 107 | .replace(/^('|")/, '') |
| 108 | .replace(/('|");?$/, ''); |
| 109 | } else { |
| 110 | // normal css |
| 111 | code += '\n' + contents; |
| 112 | fs.unlinkSync(file); // after we‘ve scanned a CSS file, remove it (so it‘s not double-loaded) |
| 113 | } |
| 114 | }); |
| 115 | |
| 116 | // sanitize JSON values |
| 117 | const css = code.replace(/\\n/g, '\n').replace(/\\"/g, '"'); |
| 118 | |
| 119 | // minify |
| 120 | return minifyCSS ? csso.minify(css).css : css; |
| 121 | } |
| 122 | exports.buildImportCSS = buildImportCSS; |