-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent locking up when encountering invalid CSS (#4675)
* Prevent locking up when encountering invalid CSS * Add a changeset * Move the unit test to the test folder * ponyfill aggregateerror * Use the exported type * keep original errors * fix
- Loading branch information
Showing
15 changed files
with
229 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'astro': patch | ||
--- | ||
|
||
Prevent locking up when encountering invalid CSS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export type { | ||
CompileProps | ||
} from './compile'; | ||
export type { | ||
TransformStyle | ||
} from './types'; | ||
|
||
export { | ||
cachedCompilation, | ||
invalidateCompilation, | ||
isCached, | ||
getCachedSource | ||
} from './compile.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import type { TransformOptions } from '@astrojs/compiler'; | ||
import type { TransformStyle } from './types'; | ||
import type { SourceMapInput } from 'rollup'; | ||
|
||
type PreprocessStyle = TransformOptions['preprocessStyle']; | ||
|
||
export function createStylePreprocessor(transformStyle: TransformStyle, cssDeps: Set<string>, errors: Error[]): PreprocessStyle { | ||
const preprocessStyle: PreprocessStyle = async (value: string, attrs: Record<string, string>) => { | ||
const lang = `.${attrs?.lang || 'css'}`.toLowerCase(); | ||
|
||
try { | ||
const result = await transformStyle(value, lang); | ||
|
||
if (!result) return null as any; // TODO: add type in compiler to fix "any" | ||
|
||
for (const dep of result.deps) { | ||
cssDeps.add(dep); | ||
} | ||
|
||
let map: SourceMapInput | undefined; | ||
if (result.map) { | ||
if (typeof result.map === 'string') { | ||
map = result.map; | ||
} else if (result.map.mappings) { | ||
map = result.map.toString(); | ||
} | ||
} | ||
|
||
return { code: result.code, map }; | ||
} catch (err) { | ||
errors.push(err as unknown as Error); | ||
return { | ||
error: err + '' | ||
}; | ||
} | ||
}; | ||
|
||
return preprocessStyle; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import type { SourceMap } from 'rollup'; | ||
|
||
export type TransformStyleResult = null | { | ||
code: string; | ||
map: SourceMap | null; | ||
deps: Set<string>; | ||
} | ||
|
||
export type TransformStyle = (source: string, lang: string) => TransformStyleResult | Promise<TransformStyleResult>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export type { | ||
ViteStyleTransformer | ||
} from './style-transform'; | ||
export { | ||
createViteStyleTransformer, | ||
createTransformStyles | ||
} from './style-transform.js'; |
Oops, something went wrong.