From 481d77cd2359d86eb83a57e6a7909362d60b68b9 Mon Sep 17 00:00:00 2001 From: aweikalee Date: Sun, 6 Aug 2023 14:50:35 +0800 Subject: [PATCH] fix(plugin-vue): deprecate inline main --- packages/plugin-vue/src/main.ts | 51 +++++++------------------------ packages/plugin-vue/src/script.ts | 22 ------------- 2 files changed, 11 insertions(+), 62 deletions(-) diff --git a/packages/plugin-vue/src/main.ts b/packages/plugin-vue/src/main.ts index c43886fc..91ea5ed8 100644 --- a/packages/plugin-vue/src/main.ts +++ b/packages/plugin-vue/src/main.ts @@ -12,12 +12,7 @@ import { getPrevDescriptor, setSrcDescriptor, } from './utils/descriptorCache' -import { - canInlineMain, - isUseInlineTemplate, - resolveScript, - scriptIdentifier, -} from './script' +import { isUseInlineTemplate, resolveScript, scriptIdentifier } from './script' import { transformTemplateInMain } from './template' import { isEqualBlock, isOnlyTemplateChanged } from './handleHotUpdate' import { createRollupError } from './utils/error' @@ -313,41 +308,17 @@ async function genScriptCode( const script = resolveScript(descriptor, options, ssr) if (script) { - // If the script is js/ts and has no external src, it can be directly placed - // in the main module. - if (canInlineMain(descriptor, options)) { - if (!options.compiler.version) { - // if compiler-sfc exposes no version, it's < 3.3 and doesn't support - // genDefaultAs option. - const userPlugins = options.script?.babelParserPlugins || [] - const defaultPlugins = - script.lang === 'ts' - ? userPlugins.includes('decorators') - ? (['typescript'] as const) - : (['typescript', 'decorators-legacy'] as const) - : [] - scriptCode = options.compiler.rewriteDefault( - script.content, - scriptIdentifier, - [...defaultPlugins, ...userPlugins], - ) - } else { - scriptCode = script.content - } - map = script.map - } else { - if (script.src) { - await linkSrcToDescriptor(script.src, descriptor, pluginContext, false) - } - const src = script.src || descriptor.filename - const langFallback = (script.src && path.extname(src).slice(1)) || 'js' - const attrsQuery = attrsToQuery(script.attrs, langFallback) - const srcQuery = script.src ? `&src=true` : `` - const query = `?vue&type=script${srcQuery}${attrsQuery}` - const request = JSON.stringify(src + query) - scriptCode = - `import _sfc_main from ${request}\n` + `export * from ${request}` // support named exports + if (script.src) { + await linkSrcToDescriptor(script.src, descriptor, pluginContext, false) } + const src = script.src || descriptor.filename + const langFallback = (script.src && path.extname(src).slice(1)) || 'js' + const attrsQuery = attrsToQuery(script.attrs, langFallback) + const srcQuery = script.src ? `&src=true` : `` + const query = `?vue&type=script${srcQuery}${attrsQuery}` + const request = JSON.stringify(src + query) + scriptCode = + `import _sfc_main from ${request}\n` + `export * from ${request}` // support named exports } return { code: scriptCode, diff --git a/packages/plugin-vue/src/script.ts b/packages/plugin-vue/src/script.ts index f62a31e1..7b7964ca 100644 --- a/packages/plugin-vue/src/script.ts +++ b/packages/plugin-vue/src/script.ts @@ -69,9 +69,6 @@ export function resolveScript( reactivityTransform: options.reactivityTransform !== false, templateOptions: resolveTemplateCompilerOptions(descriptor, options, ssr), sourceMap: options.sourceMap, - genDefaultAs: canInlineMain(descriptor, options) - ? scriptIdentifier - : undefined, }) if (!options.isProduction && resolved?.deps) { @@ -94,22 +91,3 @@ export function resolveScript( cacheToUse.set(descriptor, resolved) return resolved } - -// If the script is js/ts and has no external src, it can be directly placed -// in the main module. Skip for build -export function canInlineMain( - descriptor: SFCDescriptor, - options: ResolvedOptions, -): boolean { - if (descriptor.script?.src || descriptor.scriptSetup?.src) { - return false - } - const lang = descriptor.script?.lang || descriptor.scriptSetup?.lang - if (!lang) { - return true - } - if (lang === 'ts' && options.devServer) { - return true - } - return false -}