|
|
@@ -25,9 +25,14 @@ import {
|
|
|
} from '@babel/types'
|
|
|
import { walk } from 'estree-walker'
|
|
|
import { RawSourceMap } from 'source-map'
|
|
|
-import { CSS_VARS_HELPER, genCssVarsCode, injectCssVarsCalls } from './cssVars'
|
|
|
+import {
|
|
|
+ CSS_VARS_HELPER,
|
|
|
+ genCssVarsCode,
|
|
|
+ genNormalScriptCssVarsCode
|
|
|
+} from './cssVars'
|
|
|
import { compileTemplate, SFCTemplateCompileOptions } from './compileTemplate'
|
|
|
import { warnExperimental, warnOnce } from './warn'
|
|
|
+import { rewriteDefault } from './rewriteDefault'
|
|
|
|
|
|
const DEFINE_PROPS = 'defineProps'
|
|
|
const DEFINE_EMIT = 'defineEmit'
|
|
|
@@ -92,6 +97,8 @@ export function compileScript(
|
|
|
|
|
|
const scopeId = options.id ? options.id.replace(/^data-v-/, '') : ''
|
|
|
const cssVars = sfc.cssVars
|
|
|
+ const hasInheritAttrsFlag =
|
|
|
+ sfc.template && sfc.template.attrs['inherit-attrs'] === 'false'
|
|
|
const scriptLang = script && script.lang
|
|
|
const scriptSetupLang = scriptSetup && scriptSetup.lang
|
|
|
const isTS = scriptLang === 'ts' || scriptSetupLang === 'ts'
|
|
|
@@ -113,18 +120,26 @@ export function compileScript(
|
|
|
sourceType: 'module'
|
|
|
}).program.body
|
|
|
const bindings = analyzeScriptBindings(scriptAst)
|
|
|
+ const needRewrite = cssVars.length || hasInheritAttrsFlag
|
|
|
+ let content = script.content
|
|
|
+ if (needRewrite) {
|
|
|
+ content = rewriteDefault(content, `__default__`, plugins)
|
|
|
+ if (cssVars.length) {
|
|
|
+ content += genNormalScriptCssVarsCode(
|
|
|
+ cssVars,
|
|
|
+ bindings,
|
|
|
+ scopeId,
|
|
|
+ !!options.isProd
|
|
|
+ )
|
|
|
+ }
|
|
|
+ if (hasInheritAttrsFlag) {
|
|
|
+ content += `__default__.inheritAttrs = false`
|
|
|
+ }
|
|
|
+ content += `\nexport default __default__`
|
|
|
+ }
|
|
|
return {
|
|
|
...script,
|
|
|
- content: cssVars.length
|
|
|
- ? injectCssVarsCalls(
|
|
|
- sfc,
|
|
|
- cssVars,
|
|
|
- bindings,
|
|
|
- scopeId,
|
|
|
- !!options.isProd,
|
|
|
- plugins
|
|
|
- )
|
|
|
- : script.content,
|
|
|
+ content,
|
|
|
bindings,
|
|
|
scriptAst
|
|
|
}
|
|
|
@@ -922,6 +937,9 @@ export function compileScript(
|
|
|
// 11. finalize default export
|
|
|
// expose: [] makes <script setup> components "closed" by default.
|
|
|
let runtimeOptions = `\n expose: [],`
|
|
|
+ if (hasInheritAttrsFlag) {
|
|
|
+ runtimeOptions += `\n inheritAttrs: false,`
|
|
|
+ }
|
|
|
if (hasInlinedSsrRenderFn) {
|
|
|
runtimeOptions += `\n __ssrInlineRender: true,`
|
|
|
}
|