| 1146 | } |
| 1147 | |
| 1148 | export function injectInlinedCSS( |
| 1149 | s: MagicString, |
| 1150 | ctx: Pick<MinimalPluginContext, 'error'>, |
| 1151 | code: string, |
| 1152 | format: InternalModuleFormat, |
| 1153 | injectCode: string, |
| 1154 | ): void { |
| 1155 | let injectionPoint: number |
| 1156 | if (format === 'iife' || format === 'umd') { |
| 1157 | const m = (format === 'iife' ? IIFE_BEGIN_RE : UMD_BEGIN_RE).exec(code) |
| 1158 | if (!m) { |
| 1159 | ctx.error('Injection point for inlined CSS not found') |
| 1160 | } |
| 1161 | injectionPoint = m.index + m[0].length |
| 1162 | } else if (format === 'es') { |
| 1163 | // legacy build |
| 1164 | if (code.startsWith('#!')) { |
| 1165 | // inject after the shebang line instead of into it |
| 1166 | const newlinePos = code.indexOf('\n') |
| 1167 | if (newlinePos === -1) { |
| 1168 | // the shebang has no trailing newline, add one so it stays intact |
| 1169 | s.append(`\n${injectCode}`) |
| 1170 | return |
| 1171 | } |
| 1172 | injectionPoint = newlinePos + 1 |
| 1173 | } else { |
| 1174 | injectionPoint = 0 |
| 1175 | } |
| 1176 | } else { |
| 1177 | ctx.error('Non supported format') |
| 1178 | } |
| 1179 | s.appendRight(injectionPoint, injectCode) |
| 1180 | } |
| 1181 | |
| 1182 | export function cssAnalysisPlugin(config: ResolvedConfig): Plugin { |
| 1183 | return { |