This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: handle script compilation separately
- Loading branch information
Showing
6 changed files
with
100 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { compileScript, SFCDescriptor, SFCScriptBlock } from '@vue/compiler-sfc' | ||
import { Options } from '.' | ||
import { getTemplateCompilerOptions } from './template' | ||
|
||
// since we generate different output based on whether the template is inlined | ||
// or not, we need to cache the results separately | ||
const inlinedCache = new WeakMap<SFCDescriptor, SFCScriptBlock | null>() | ||
const normalCache = new WeakMap<SFCDescriptor, SFCScriptBlock | null>() | ||
|
||
export function getResolvedScript( | ||
descriptor: SFCDescriptor, | ||
isServer: boolean | ||
): SFCScriptBlock | null | undefined { | ||
const cacheToUse = isServer ? normalCache : inlinedCache | ||
return cacheToUse.get(descriptor) | ||
} | ||
|
||
export function resolveScript( | ||
descriptor: SFCDescriptor, | ||
scopeId: string, | ||
isProd: boolean, | ||
isServer: boolean, | ||
options: Options | ||
) { | ||
if (!descriptor.script && !descriptor.scriptSetup) { | ||
return null | ||
} | ||
|
||
const cached = getResolvedScript(descriptor, isServer) | ||
if (cached) { | ||
return cached | ||
} | ||
|
||
let resolved: SFCScriptBlock | null | ||
|
||
if (compileScript) { | ||
resolved = compileScript(descriptor, { | ||
id: scopeId, | ||
isProd, | ||
inlineTemplate: !isServer, | ||
templateOptions: getTemplateCompilerOptions(options, descriptor, scopeId), | ||
}) | ||
} else if (descriptor.scriptSetup) { | ||
throw new Error( | ||
`<script setup> is not supported by the installed version of ` + | ||
`@vue/compiler-sfc - please upgrade.` | ||
) | ||
} else { | ||
resolved = descriptor.script | ||
} | ||
|
||
if (isServer) { | ||
normalCache.set(descriptor, resolved) | ||
} else { | ||
inlinedCache.set(descriptor, resolved) | ||
} | ||
|
||
return resolved | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
facf4c6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CI failing because the code relies on unreleased typing from
@vue/compiler-sfc
. Tests are passing locally.