Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(compiler-core): add allowSideEffectTag option for disabling ignoreSideEffectTags transform #11184

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/compiler-core/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,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
Expand Down
2 changes: 2 additions & 0 deletions packages/compiler-core/src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export function createTransformContext(
root: RootNode,
{
filename = '',
allowSideEffectTags = false,
prefixIdentifiers = false,
hoistStatic = false,
hmr = false,
Expand Down Expand Up @@ -156,6 +157,7 @@ export function createTransformContext(
// options
filename,
selfName: nameMatch && capitalize(camelize(nameMatch[1])),
allowSideEffectTags,
prefixIdentifiers,
hoistStatic,
hmr,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>`, {
Expand All @@ -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')
})
})
3 changes: 2 additions & 1 deletion packages/compiler-dom/src/transforms/ignoreSideEffectTags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down