Skip to content

Commit 713b661

Browse files
authored
Merge pull request #1917 from maizzle/fix/warn-tailwind-failures
fix: warn when a style tag fails to compile
2 parents e2df83b + 31b6e65 commit 713b661

2 files changed

Lines changed: 30 additions & 4 deletions

File tree

‎src/tests/transformers/tailwindcss.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,28 @@ describe('tailwindcss', () => {
224224
expect(result).toContain('@import "./nonexistent.css"')
225225
expect(result).not.toContain('"')
226226
})
227+
228+
it('warns when a style tag fails to compile', async () => {
229+
const { warn, error } = console
230+
const warnings: string[] = []
231+
console.warn = (message: string) => warnings.push(message)
232+
console.error = () => {}
233+
234+
try {
235+
// Raw and empty tags aren't compiled but still count towards the position
236+
const html = '<style raw>.r { color: red }</style><style></style><style>.ok { color: red }</style><style>@import "./nonexistent.css";</style>'
237+
const result = await run(html, '/path/to/emails/welcome.vue')
238+
239+
expect(result).toContain('@import "./nonexistent.css"')
240+
expect(warnings).toHaveLength(1)
241+
expect(warnings[0]).toContain('[maizzle]')
242+
expect(warnings[0]).toContain('<style> #4 in /path/to/emails/welcome.vue')
243+
expect(warnings[0]).toContain('nonexistent.css')
244+
} finally {
245+
console.warn = warn
246+
console.error = error
247+
}
248+
})
227249
})
228250

229251
describe('short-circuit', () => {

‎src/transformers/tailwindcss.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,13 @@ export function rewriteImportsSourceNone(css: string): string {
183183
* transformers (inliner, purge, etc.) work with fully compiled CSS.
184184
*/
185185
export async function tailwindcss(dom: ChildNode[], config: MaizzleConfig, filePath?: string, sourceFiles?: string[]): Promise<ChildNode[]> {
186-
const styleTags: { node: Element; cssContent: string }[] = []
186+
const styleTags: { node: Element; cssContent: string; position: number }[] = []
187+
let styleCount = 0
187188

188189
walk(dom, (node) => {
189190
if ((node as Element).name !== 'style') return
190191

192+
styleCount++
191193
const el = node as Element
192194
const attrs = el.attribs
193195

@@ -210,7 +212,7 @@ export async function tailwindcss(dom: ChildNode[], config: MaizzleConfig, fileP
210212

211213
if (!rawContent.trim()) return
212214

213-
styleTags.push({ node: el, cssContent: decodeStyleEntities(rawContent) })
215+
styleTags.push({ node: el, cssContent: decodeStyleEntities(rawContent), position: styleCount })
214216
})
215217

216218
if (!styleTags.length) return dom
@@ -236,7 +238,7 @@ export async function tailwindcss(dom: ChildNode[], config: MaizzleConfig, fileP
236238
const firstTailwindStyle = styleTags.findIndex(({ cssContent }) => usesTailwind(cssContent))
237239

238240
for (let i = 0; i < styleTags.length; i++) {
239-
const { node, cssContent } = styleTags[i]
241+
const { node, cssContent, position } = styleTags[i]
240242

241243
/**
242244
* Only add source directives to style tags that import Tailwind —
@@ -259,7 +261,9 @@ export async function tailwindcss(dom: ChildNode[], config: MaizzleConfig, fileP
259261
data: optimized,
260262
parent: node,
261263
} as any]
262-
} catch {
264+
} catch (error) {
265+
console.warn(`[maizzle] Failed to compile <style> #${position} in ${filePath ?? 'a string template'}, it was left uncompiled: ${(error as Error)?.message ?? error}`)
266+
263267
/**
264268
* If CSS processing fails, still replace with decoded content
265269
* so HTML entities don't break the CSS.

0 commit comments

Comments
 (0)