Skip to content

Commit

Permalink
refactor(language-core): determine skipTemplateCodegen in advance
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Sep 2, 2024
1 parent 16b1b36 commit bdf5d41
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { ScriptCodegenContext } from './context';
import { codeFeatures, type ScriptCodegenOptions } from './index';
import { getTemplateUsageVars } from './template';

export function* generateInternalComponent(
export function* generateComponentSelf(
options: ScriptCodegenOptions,
ctx: ScriptCodegenContext,
templateCodegenCtx: TemplateCodegenContext
Expand Down
10 changes: 3 additions & 7 deletions packages/language-core/lib/codegen/script/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { endOfLine, generateSfcBlockSection, newLine } from '../common';
import { generateGlobalTypes } from '../globalTypes';
import type { TemplateCodegenContext } from '../template/context';
import { createScriptCodegenContext, ScriptCodegenContext } from './context';
import { generateInternalComponent } from './internalComponent';
import { generateComponentSelf } from './componentSelf';
import { generateScriptSetup, generateScriptSetupImports } from './scriptSetup';
import { generateSrc } from './src';
import { generateStyleModulesType } from './styleModulesType';
Expand Down Expand Up @@ -115,9 +115,7 @@ export function* generateScript(options: ScriptCodegenOptions): Generator<Code,
yield generateSfcBlockSection(options.sfc.script, 0, classBlockEnd, codeFeatures.all);
yield `__VLS_template = () => {`;
const templateCodegenCtx = yield* generateTemplate(options, ctx, true);
if (templateCodegenCtx) {
yield* generateInternalComponent(options, ctx, templateCodegenCtx);
}
yield* generateComponentSelf(options, ctx, templateCodegenCtx);
yield `},${newLine}`;
yield generateSfcBlockSection(options.sfc.script, classBlockEnd, options.sfc.script.content.length, codeFeatures.all);
}
Expand All @@ -143,9 +141,7 @@ export function* generateScript(options: ScriptCodegenOptions): Generator<Code,
yield `function __VLS_template() {${newLine}`;
const templateCodegenCtx = yield* generateTemplate(options, ctx, false);
yield `}${endOfLine}`;
if (templateCodegenCtx) {
yield* generateInternalComponent(options, ctx, templateCodegenCtx);
}
yield* generateComponentSelf(options, ctx, templateCodegenCtx);
}

// #4788
Expand Down
6 changes: 2 additions & 4 deletions packages/language-core/lib/codegen/script/scriptSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { endOfLine, generateSfcBlockSection, newLine } from '../common';
import { generateComponent, generateEmitsOption } from './component';
import type { ScriptCodegenContext } from './context';
import { ScriptCodegenOptions, codeFeatures } from './index';
import { generateInternalComponent } from './internalComponent';
import { generateComponentSelf } from './componentSelf';
import { generateTemplate } from './template';

export function* generateScriptSetupImports(
Expand Down Expand Up @@ -290,9 +290,7 @@ function* generateSetupFunction(
yield `function __VLS_template() {${newLine}`;
const templateCodegenCtx = yield* generateTemplate(options, ctx, false);
yield `}${endOfLine}`;
if (templateCodegenCtx) {
yield* generateInternalComponent(options, ctx, templateCodegenCtx);
}
yield* generateComponentSelf(options, ctx, templateCodegenCtx);
yield `type __VLS_TemplateResult = ReturnType<typeof __VLS_template>${endOfLine}`;

if (syntax) {
Expand Down
30 changes: 9 additions & 21 deletions packages/language-core/lib/codegen/script/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,29 +91,17 @@ export function* generateTemplate(
options: ScriptCodegenOptions,
ctx: ScriptCodegenContext,
isClassComponent: boolean
): Generator<Code, TemplateCodegenContext | undefined> {
): Generator<Code, TemplateCodegenContext> {
ctx.generatedTemplate = true;

if (!options.vueCompilerOptions.skipTemplateCodegen) {
const templateCodegenCtx = createTemplateCodegenContext({
scriptSetupBindingNames: new Set(),
edited: options.edited,
});
yield* generateTemplateCtx(options, isClassComponent);
yield* generateTemplateComponents(options);
yield* generateTemplateBody(options, templateCodegenCtx);
return templateCodegenCtx;
}
else {
const templateUsageVars = [...getTemplateUsageVars(options, ctx)];
yield `// @ts-ignore${newLine}`;
yield `[${templateUsageVars.join(', ')}]${newLine}`;
yield `return {${newLine}`;
yield ` slots: {},${newLine}`;
yield ` refs: {},${newLine}`;
yield ` attrs: {},${newLine}`;
yield `}${endOfLine}`;
}
const templateCodegenCtx = createTemplateCodegenContext({
scriptSetupBindingNames: new Set(),
edited: options.edited,
});
yield* generateTemplateCtx(options, isClassComponent);
yield* generateTemplateComponents(options);
yield* generateTemplateBody(options, templateCodegenCtx);
return templateCodegenCtx;
}

function* generateTemplateBody(
Expand Down
5 changes: 2 additions & 3 deletions packages/language-core/lib/plugins/vue-tsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function createTsx(
);
const generatedTemplate = computed(() => {

if (!_sfc.template) {
if (ctx.vueCompilerOptions.skipTemplateCodegen || !_sfc.template) {
return;
}

Expand Down Expand Up @@ -158,7 +158,6 @@ function createTsx(
const generatedScript = computed(() => {
const codes: Code[] = [];
const linkedCodeMappings: Mapping[] = [];
const _template = generatedTemplate();
let generatedLength = 0;
const codegen = generateScript({
ts,
Expand All @@ -167,7 +166,7 @@ function createTsx(
lang: lang(),
scriptRanges: scriptRanges(),
scriptSetupRanges: scriptSetupRanges(),
templateCodegen: _template,
templateCodegen: generatedTemplate(),
compilerOptions: ctx.compilerOptions,
vueCompilerOptions: ctx.vueCompilerOptions,
edited: ctx.vueCompilerOptions.__test || (fileEditTimes.get(fileName) ?? 0) >= 2,
Expand Down

0 comments on commit bdf5d41

Please sign in to comment.