Skip to content

Commit

Permalink
fix(cli-utils): vue losing the graphql call due to missing sfc plugin (
Browse files Browse the repository at this point in the history
…#305)

Co-authored-by: Phil Pluckthun <phil@kitten.sh>
  • Loading branch information
JoviDeCroock and kitten authored May 15, 2024
1 parent 2679d49 commit a5ac4fb
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .changeset/silver-eels-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@gql.tada/cli-utils": patch
"gql.tada": patch
---

Fix Vue not transpiling to `.tsx` files properly due to missing SFC plugin
61 changes: 59 additions & 2 deletions packages/cli-utils/src/ts/transformers/vue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,60 @@ import ts from 'typescript';
import type { VirtualCode } from '@vue/language-core';
import * as vueCompilerDOM from '@vue/compiler-dom';
import * as vue from '@vue/language-core';
import { parse } from '@vue/language-core';

const useVueFilePlugin: vue.VueLanguagePlugin = (_ctx) => {
return {
version: 2,

parseSFC(_fileName, content) {
return parse(content);
},

updateSFC(sfc, change) {
const blocks = [
sfc.descriptor.template,
sfc.descriptor.script,
sfc.descriptor.scriptSetup,
...sfc.descriptor.styles,
...sfc.descriptor.customBlocks,
].filter((block): block is NonNullable<typeof block> => !!block);

const hitBlock = blocks.find(
(block) => change.start >= block.loc.start.offset && change.end <= block.loc.end.offset
);
if (!hitBlock) {
return;
}

const oldContent = hitBlock.content;
const newContent = (hitBlock.content =
hitBlock.content.substring(0, change.start - hitBlock.loc.start.offset) +
change.newText +
hitBlock.content.substring(change.end - hitBlock.loc.start.offset));

// #3449
const endTagRegex = new RegExp(`</\\s*${hitBlock.type}\\s*>`);
const insertedEndTag = !!oldContent.match(endTagRegex) !== !!newContent.match(endTagRegex);
if (insertedEndTag) {
return;
}

const lengthDiff = change.newText.length - (change.end - change.start);

for (const block of blocks) {
if (block.loc.start.offset > change.end) {
block.loc.start.offset += lengthDiff;
}
if (block.loc.end.offset >= change.end) {
block.loc.end.offset += lengthDiff;
}
}

return sfc;
},
};
};

function* forEachEmbeddedCode(virtualCode: VirtualCode) {
yield virtualCode;
Expand Down Expand Up @@ -34,15 +88,18 @@ export const transform = (sourceFile: ts.SourceFile): VirtualCode | undefined =>
if (!VueVirtualCode || !getBasePlugins) {
return undefined;
} else if (!plugins) {
plugins = getBasePlugins({
const pluginContext = {
modules: {
typescript: ts,
'@vue/compiler-dom': vueCompilerDOM,
},
compilerOptions: {},
globalTypesHolder: undefined,
vueCompilerOptions,
});
};
plugins = getBasePlugins(pluginContext);
const vueSfcPlugin = useVueFilePlugin(pluginContext);
plugins.push(vueSfcPlugin);
}

const snapshot = ts.ScriptSnapshot.fromString(sourceFile.getFullText());
Expand Down

0 comments on commit a5ac4fb

Please sign in to comment.