Skip to content

Commit faed989

Browse files
committed
wip: support inherit-attrs="false" on sfc <tempalte>
1 parent 47d73c2 commit faed989

File tree

4 files changed

+75
-26
lines changed

4 files changed

+75
-26
lines changed

packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,27 @@ return { x }
3333
export const n = 1"
3434
`;
3535
36+
exports[`SFC compile <script setup> <template inherit-attrs="false"> 1`] = `
37+
"
38+
const __default__ = {}
39+
__default__.inheritAttrs = false
40+
export default __default__"
41+
`;
42+
43+
exports[`SFC compile <script setup> <template inherit-attrs="false"> 2`] = `
44+
"export default {
45+
expose: [],
46+
inheritAttrs: false,
47+
setup(__props) {
48+
49+
const a = 1
50+
51+
return { a }
52+
}
53+
54+
}"
55+
`;
56+
3657
exports[`SFC compile <script setup> defineEmit() 1`] = `
3758
"export default {
3859
expose: [],

packages/compiler-sfc/__tests__/compileScript.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,28 @@ const myEmit = defineEmit(['foo', 'bar'])
7070
emits: ['foo', 'bar'],`)
7171
})
7272

73+
test('<template inherit-attrs="false">', () => {
74+
const { content } = compile(`
75+
<script>
76+
export default {}
77+
</script>
78+
<template inherit-attrs="false">
79+
{{ a }}
80+
</template>
81+
`)
82+
assertCode(content)
83+
84+
const { content: content2 } = compile(`
85+
<script setup>
86+
const a = 1
87+
</script>
88+
<template inherit-attrs="false">
89+
{{ a }}
90+
</template>
91+
`)
92+
assertCode(content2)
93+
})
94+
7395
describe('<script> and <script setup> co-usage', () => {
7496
test('script first', () => {
7597
const { content } = compile(`

packages/compiler-sfc/src/compileScript.ts

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,14 @@ import {
2525
} from '@babel/types'
2626
import { walk } from 'estree-walker'
2727
import { RawSourceMap } from 'source-map'
28-
import { CSS_VARS_HELPER, genCssVarsCode, injectCssVarsCalls } from './cssVars'
28+
import {
29+
CSS_VARS_HELPER,
30+
genCssVarsCode,
31+
genNormalScriptCssVarsCode
32+
} from './cssVars'
2933
import { compileTemplate, SFCTemplateCompileOptions } from './compileTemplate'
3034
import { warnExperimental, warnOnce } from './warn'
35+
import { rewriteDefault } from './rewriteDefault'
3136

3237
const DEFINE_PROPS = 'defineProps'
3338
const DEFINE_EMIT = 'defineEmit'
@@ -92,6 +97,8 @@ export function compileScript(
9297

9398
const scopeId = options.id ? options.id.replace(/^data-v-/, '') : ''
9499
const cssVars = sfc.cssVars
100+
const hasInheritAttrsFlag =
101+
sfc.template && sfc.template.attrs['inherit-attrs'] === 'false'
95102
const scriptLang = script && script.lang
96103
const scriptSetupLang = scriptSetup && scriptSetup.lang
97104
const isTS = scriptLang === 'ts' || scriptSetupLang === 'ts'
@@ -113,18 +120,26 @@ export function compileScript(
113120
sourceType: 'module'
114121
}).program.body
115122
const bindings = analyzeScriptBindings(scriptAst)
123+
const needRewrite = cssVars.length || hasInheritAttrsFlag
124+
let content = script.content
125+
if (needRewrite) {
126+
content = rewriteDefault(content, `__default__`, plugins)
127+
if (cssVars.length) {
128+
content += genNormalScriptCssVarsCode(
129+
cssVars,
130+
bindings,
131+
scopeId,
132+
!!options.isProd
133+
)
134+
}
135+
if (hasInheritAttrsFlag) {
136+
content += `__default__.inheritAttrs = false`
137+
}
138+
content += `\nexport default __default__`
139+
}
116140
return {
117141
...script,
118-
content: cssVars.length
119-
? injectCssVarsCalls(
120-
sfc,
121-
cssVars,
122-
bindings,
123-
scopeId,
124-
!!options.isProd,
125-
plugins
126-
)
127-
: script.content,
142+
content,
128143
bindings,
129144
scriptAst
130145
}
@@ -922,6 +937,9 @@ export function compileScript(
922937
// 11. finalize default export
923938
// expose: [] makes <script setup> components "closed" by default.
924939
let runtimeOptions = `\n expose: [],`
940+
if (hasInheritAttrsFlag) {
941+
runtimeOptions += `\n inheritAttrs: false,`
942+
}
925943
if (hasInlinedSsrRenderFn) {
926944
runtimeOptions += `\n __ssrInlineRender: true,`
927945
}

packages/compiler-sfc/src/cssVars.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import {
88
BindingMetadata
99
} from '@vue/compiler-dom'
1010
import { SFCDescriptor } from './parse'
11-
import { rewriteDefault } from './rewriteDefault'
12-
import { ParserPlugin } from '@babel/parser'
1311
import postcss, { Root } from 'postcss'
1412
import hash from 'hash-sum'
1513

@@ -96,22 +94,13 @@ export function genCssVarsCode(
9694

9795
// <script setup> already gets the calls injected as part of the transform
9896
// this is only for single normal <script>
99-
export function injectCssVarsCalls(
100-
sfc: SFCDescriptor,
97+
export function genNormalScriptCssVarsCode(
10198
cssVars: string[],
10299
bindings: BindingMetadata,
103100
id: string,
104-
isProd: boolean,
105-
parserPlugins: ParserPlugin[]
101+
isProd: boolean
106102
): string {
107-
const script = rewriteDefault(
108-
sfc.script!.content,
109-
`__default__`,
110-
parserPlugins
111-
)
112-
113103
return (
114-
script +
115104
`\nimport { ${CSS_VARS_HELPER} as _${CSS_VARS_HELPER} } from 'vue'\n` +
116105
`const __injectCSSVars__ = () => {\n${genCssVarsCode(
117106
cssVars,
@@ -122,7 +111,6 @@ export function injectCssVarsCalls(
122111
`const __setup__ = __default__.setup\n` +
123112
`__default__.setup = __setup__\n` +
124113
` ? (props, ctx) => { __injectCSSVars__();return __setup__(props, ctx) }\n` +
125-
` : __injectCSSVars__\n` +
126-
`export default __default__`
114+
` : __injectCSSVars__\n`
127115
)
128116
}

0 commit comments

Comments
 (0)