Skip to content

Commit

Permalink
feat: add option experimentalShamefullySupportOptionsApi (#1202)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Apr 15, 2022
1 parent ca7e4aa commit 18a71f4
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 5 deletions.
Expand Up @@ -16,6 +16,10 @@
],
"markdownDescription": "https://github.com/johnsoncodehk/volar/pull/351"
},
"experimentalShamefullySupportOptionsApi": {
"type": "boolean",
"markdownDescription": "Support intellisense compoonents that export options without `Vue.extend` and `defineComponent`. This is essentially a hack and not recommended to use it."
},
"experimentalTemplateCompilerOptions": {
"type": "object",
"markdownDescription": "https://github.com/johnsoncodehk/volar/issues/576"
Expand Down
16 changes: 15 additions & 1 deletion packages/vue-code-gen/src/generators/script.ts
Expand Up @@ -22,6 +22,7 @@ export function generate(
getHtmlGen: () => ReturnType<typeof templateGen['generate']> | undefined,
getStyleBindTexts: () => string[],
vueLibName: string,
shimComponentOptions: boolean,
) {

const codeGen = new CodeGen<EmbeddedFileMappingData>();
Expand Down Expand Up @@ -166,7 +167,20 @@ export function generate(
addVirtualCode('script', scriptRanges.exportDefault.end, script.content.length);
}
else {
addVirtualCode('script', 0, script.content.length);
let isExportRawObject = false;
if (scriptRanges?.exportDefault) {
isExportRawObject = script.content.substring(scriptRanges.exportDefault.expression.start, scriptRanges.exportDefault.expression.end).startsWith('{');
}
if (isExportRawObject && shimComponentOptions && scriptRanges?.exportDefault) {
addVirtualCode('script', 0, scriptRanges.exportDefault.expression.start);
codeGen.addText(`(await import('${vueLibName}')).defineComponent(`);
addVirtualCode('script', scriptRanges.exportDefault.expression.start, scriptRanges.exportDefault.expression.end);
codeGen.addText(`)`);
addVirtualCode('script', scriptRanges.exportDefault.expression.end, script.content.length);
}
else {
addVirtualCode('script', 0, script.content.length);
}
}
}
function addVirtualCode(vueTag: 'script' | 'scriptSetup', start: number, end: number) {
Expand Down
2 changes: 2 additions & 0 deletions packages/vue-code-gen/src/index.ts
Expand Up @@ -18,6 +18,7 @@ export function generateSFCScriptTypeCheckCode(
scriptLang: 'js' | 'jsx' | 'ts' | 'tsx',
scriptCode: string | undefined,
scriptSetupCode: string | undefined,
shimComponentOptions: boolean,
templateAst?: CompilerDOM.RootNode,
cssVars?: string[],
vueLibName = 'vue',
Expand All @@ -43,6 +44,7 @@ export function generateSFCScriptTypeCheckCode(
// () => templateAst ? generateTemplateScript(templateAst) : undefined,
() => cssVars ?? [],
vueLibName,
shimComponentOptions,
);

return {
Expand Down
1 change: 1 addition & 0 deletions packages/vue-typescript/src/types.ts
Expand Up @@ -13,6 +13,7 @@ export interface ITemplateScriptData {

export interface VueCompilerOptions {
experimentalCompatMode?: 2 | 3;
experimentalShamefullySupportOptionsApi?: boolean;
experimentalTemplateCompilerOptions?: any;
experimentalTemplateCompilerOptionsRequirePath?: string;
experimentalDisableTemplateSupport?: boolean;
Expand Down
6 changes: 4 additions & 2 deletions packages/vue-typescript/src/use/useSfcScriptGen.ts
Expand Up @@ -8,6 +8,7 @@ import type { parseScriptSetupRanges } from '@volar/vue-code-gen/out/parsers/scr
import { getVueLibraryName } from '../utils/localTypes';
import type { EmbeddedFileMappingData, TextRange } from '@volar/vue-code-gen';
import { Embedded, EmbeddedFile, Sfc } from '../vueFile';
import { VueCompilerOptions } from '../types';

export function useSfcScriptGen<T extends 'template' | 'script'>(
lsType: T,
Expand All @@ -20,7 +21,7 @@ export function useSfcScriptGen<T extends 'template' | 'script'>(
scriptSetupRanges: Ref<ReturnType<typeof parseScriptSetupRanges> | undefined>,
htmlGen: Ref<ReturnType<typeof templateGen.generate> | undefined>,
sfcStyles: ReturnType<(typeof import('./useSfcStyles'))['useSfcStyles']>['files'],
isVue2: boolean,
compilerOptions: VueCompilerOptions,
getCssVBindRanges: (cssEmbeddeFile: EmbeddedFile) => TextRange[],
) {

Expand All @@ -44,7 +45,8 @@ export function useSfcScriptGen<T extends 'template' | 'script'>(
}
return bindTexts;
},
getVueLibraryName(isVue2),
getVueLibraryName(compilerOptions.experimentalCompatMode === 2),
!!compilerOptions.experimentalShamefullySupportOptionsApi,
)
);
const file = computed(() => {
Expand Down
4 changes: 2 additions & 2 deletions packages/vue-typescript/src/vueFile.ts
Expand Up @@ -199,7 +199,7 @@ export function createVueFile(
computed(() => scriptSetupRanges.value),
sfcTemplateScript.templateCodeGens,
computed(() => sfcStyles.files.value),
compilerOptions.experimentalCompatMode === 2,
compilerOptions,
getCssVBindRanges,
);
const sfcScriptForScriptLs = useSfcScriptGen(
Expand All @@ -213,7 +213,7 @@ export function createVueFile(
computed(() => scriptSetupRanges.value),
sfcTemplateScript.templateCodeGens,
computed(() => sfcStyles.files.value),
compilerOptions.experimentalCompatMode === 2,
compilerOptions,
getCssVBindRanges,
);
const sfcRefSugarRanges = computed(() => (sfcScriptSetup.ast.value ? {
Expand Down

0 comments on commit 18a71f4

Please sign in to comment.