This commit is contained in:
Fernando Fernández 2025-05-05 20:38:28 +00:00 committed by GitHub
commit 0419808b92
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 24 additions and 1 deletions

View File

@ -235,6 +235,12 @@ export interface TransformOptions
* @default null
*/
transformHoist?: HoistTransform | null
/**
* Allow `<script>` and `<style>` tags inside the template of SFC files.
* Disabled by default to avoid unintentionally adding side effects into the application.
* @default false
*/
allowSideEffectTags?: boolean
/**
* If the pairing runtime provides additional built-in elements, use this to
* mark them as built-in so the compiler will generate component vnodes

View File

@ -127,6 +127,7 @@ export function createTransformContext(
root: RootNode,
{
filename = '',
allowSideEffectTags = false,
prefixIdentifiers = false,
hoistStatic = false,
hmr = false,
@ -155,6 +156,7 @@ export function createTransformContext(
// options
filename,
selfName: nameMatch && capitalize(camelize(nameMatch[1])),
allowSideEffectTags,
prefixIdentifiers,
hoistStatic,
hmr,

View File

@ -13,6 +13,13 @@ describe('compiler: ignore side effect tags', () => {
expect(err!.message).toMatch(`Tags with side effect`)
})
it('should allow script when allowSideEffectTags is true', () => {
const { code } = compile(`<script>console.log(1)</script>`, {
allowSideEffectTags: true,
})
expect(code).toMatch('script')
})
it('should ignore style', () => {
let err: CompilerError | undefined
const { code } = compile(`<style>h1 { color: red }</style>`, {
@ -24,4 +31,11 @@ describe('compiler: ignore side effect tags', () => {
expect(err).toBeDefined()
expect(err!.message).toMatch(`Tags with side effect`)
})
it('should allow style when allowSideEffectTags is true', () => {
const { code } = compile(`<style>h1 { color: red }</style>`, {
allowSideEffectTags: true,
})
expect(code).toMatch('style')
})
})

View File

@ -5,7 +5,8 @@ export const ignoreSideEffectTags: NodeTransform = (node, context) => {
if (
node.type === NodeTypes.ELEMENT &&
node.tagType === ElementTypes.ELEMENT &&
(node.tag === 'script' || node.tag === 'style')
(node.tag === 'script' || node.tag === 'style') &&
!context.allowSideEffectTags
) {
__DEV__ &&
context.onError(