Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add option experimentalShamefullySupportOptionsApi #1202

Merged
merged 1 commit into from Apr 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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