Skip to content

Commit aa6df30

Browse files
committed
fix(css): safer transformCSS logic
1 parent c8ec0be commit aa6df30

File tree

3 files changed

+36
-29
lines changed

3 files changed

+36
-29
lines changed

src/core/utils.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { WindiPluginUtils } from '@windicss/plugin-utils'
12
import { HAS_DIRECTIVE_TEST, HAS_THEME_FUNCTION_TEST } from './constants'
3+
import debug from './debug'
24

35
export const cssRequiresTransform = (source: string) => {
46
return HAS_DIRECTIVE_TEST.test(source) || HAS_THEME_FUNCTION_TEST.test(source)
@@ -7,3 +9,27 @@ export const cssRequiresTransform = (source: string) => {
79
export const isJsx = (source: string) => {
810
return /{`(.*)`}/gms.test(source)
911
}
12+
13+
export const transformCSS = (service: WindiPluginUtils, source: string, resource: string) => {
14+
if (!source || source.length <= 0)
15+
return source
16+
17+
// make sure the transform is required, can be expensive
18+
if (!cssRequiresTransform(source))
19+
return source
20+
21+
let output = source
22+
try {
23+
output = service.transformCSS(source, resource, { globaliseKeyframes: true })
24+
if (!output || output.length <= 0) {
25+
console.warn(`[WindiCSS] Failed to transform CSS for resource: ${resource}.`)
26+
return source
27+
}
28+
debug.loader('Transformed CSS', resource)
29+
}
30+
catch (e) {
31+
console.warn(`[WindiCSS] Failed to transform CSS for resource: ${resource}.`)
32+
return source
33+
}
34+
return output
35+
}

src/loaders/transform-css.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type webpack from 'webpack'
22
import type { Compiler } from '../interfaces'
3-
import debug from '../core/debug'
4-
import { cssRequiresTransform } from '../core/utils'
3+
import { transformCSS } from '../core/utils'
54

65
function TransformCss(
76
this: webpack.loader.LoaderContext,
@@ -16,19 +15,7 @@ function TransformCss(
1615
if (!service)
1716
return source
1817

19-
// only run if there's a directive to apply
20-
if (!cssRequiresTransform(source))
21-
return source
22-
23-
let output = source
24-
try {
25-
output = service.transformCSS(source, this.resource)
26-
debug.loader('Transformed CSS', this.resource)
27-
}
28-
catch (e) {
29-
this.emitWarning(`[Windi CSS] Failed to css for resource: ${this.resource}.`)
30-
}
31-
return output || source
18+
return transformCSS(service, source, this.resource)
3219
}
3320

3421
export default TransformCss

src/loaders/transform-template.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import defaults from 'lodash/defaults'
44
import loaderUtils from 'loader-utils'
55
import debug from '../core/debug'
66
import type { Compiler } from '../interfaces'
7-
import { cssRequiresTransform, isJsx } from '../core/utils'
7+
import { isJsx, transformCSS } from '../core/utils'
88

99
function TransformTemplate(
1010
this: webpack.loader.LoaderContext,
@@ -22,13 +22,8 @@ function TransformTemplate(
2222
/*
2323
* Via the pitcher loader we can transfer post-interpreted CSS
2424
*/
25-
if (this.resource.indexOf('type=style') > 0) {
26-
// if no transform is required
27-
if (!cssRequiresTransform(source))
28-
return source
29-
30-
return service.transformCSS(source, this.resource)
31-
}
25+
if (this.resource.indexOf('type=style') > 0)
26+
return transformCSS(service, source, this.resource)
3227

3328
const hasHtmlWebpackPlugin = this.loaders.filter((loader) => {
3429
// loader name as unresolved module
@@ -62,9 +57,6 @@ function TransformTemplate(
6257
debug.loader('Template has unsupported block, skipping resource', this.resource)
6358
return match
6459
}
65-
if (!cssRequiresTransform(match))
66-
return match
67-
6860
// for jsx styles we need to replace the contents of template strings
6961
if (isJsx(css)) {
7062
let m, transformedCSS
@@ -77,14 +69,16 @@ function TransformTemplate(
7769
// The result can be accessed through the `m`-variable.
7870
m.forEach((match, groupIndex) => {
7971
if (groupIndex === 1) {
80-
transformedCSS = `<style${meta}>\n{\`${service.transformCSS(match, this.resource)}\n\`}</style>`
72+
const transformedJSXCSS = transformCSS(service, match, this.resource)
73+
transformedCSS = `<style${meta}>\n{\`${transformedJSXCSS}\n\`}</style>`
8174
debug.loader('jsx transformed', transformedCSS)
8275
}
8376
})
8477
}
8578
return transformedCSS ?? match
8679
}
87-
return `<style${meta}>\n${service.transformCSS(css, this.resource)}\n</style>`
80+
const transformedCSS = transformCSS(service, css, this.resource)
81+
return `<style${meta}>\n${transformedCSS}\n</style>`
8882
})
8983
debug.loader('Transformed template ', this.resource)
9084
const transformed = service.transformGroups(templateWithTransformedCSS)
@@ -94,7 +88,7 @@ function TransformTemplate(
9488
output = templateWithTransformedCSS
9589
}
9690
catch (e) {
97-
this.emitWarning(`[Windi CSS] Failed to transform groups and css for template: ${this.resource}.`)
91+
this.emitWarning(`[WindiCSS] Failed to transform groups and css for template: ${this.resource}.`)
9892
}
9993
return output
10094
}

0 commit comments

Comments
 (0)