Skip to content

Commit

Permalink
Merge pull request #3 from akshay-devadiga/bugfix/preserve-style-attr…
Browse files Browse the repository at this point in the history
…ibutes

Bug Fix: Ensure style tag attributes are preserved during Vue 2 to Vue 3 conversion
  • Loading branch information
abdul-alhasany authored Oct 7, 2024
2 parents bd89f2f + dc73823 commit 3190b91
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
5 changes: 3 additions & 2 deletions tools/vue-composition-converter/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ worker.addEventListener('message', (event: any) => {
});
const processCode = () => {
const { scriptContent, templateContent, styleContent } = parseSfc(userInput.value);
const { scriptContent, templateContent, styleContent, styleAttributes } = parseSfc(userInput.value);
errorMessage.value = '';
Expand Down Expand Up @@ -302,7 +302,8 @@ const processCode = () => {
const compositionOutput = composeSfc(
updatedCode.value,
templateContent,
styleContent
styleContent,
styleAttributes
);
updatedCode.value = compositionOutput;
Expand Down
4 changes: 2 additions & 2 deletions tools/vue-composition-converter/tests/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ const parseCode = (input: string) => {
};

const formatCode = (input: string) => {
const { templateContent, scriptContent, styleContent } = parseSfc(input);
const { templateContent, scriptContent, styleContent, styleAttributes } = parseSfc(input);
const result = format(parseCode(scriptContent));

const composedSfc = composeSfc(result, templateContent, styleContent);
const composedSfc = composeSfc(result, templateContent, styleContent, styleAttributes);
return composedSfc;
};

Expand Down
17 changes: 15 additions & 2 deletions tools/vue-composition-converter/utils/parse-sfc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import type { ElementNode } from '@vue/compiler-dom';
import { parse } from '@vue/compiler-dom';

/**
* Generates a string of attributes from an ElementNode.
*/
const getAttributesString = (node: ElementNode | undefined) => {
return node?.props?.map((prop) => {
return `${prop.name}${prop.value ? `="${prop.value.content}"` : ''}`;
}).join(' ') || '';
};

/**
* Parse SFC user input to extract template, script and style
*/
Expand All @@ -21,10 +30,13 @@ const parseSfc = (sfcContent:string) => {
return (node as ElementNode).tag === 'style';
});

const styleAttributes = getAttributesString(style as ElementNode);

return {
templateContent: template?.innerLoc.source,
scriptContent: script?.innerLoc.source,
styleContent: style?.innerLoc.source,
styleAttributes: styleAttributes || 'scoped'
};
};

Expand All @@ -36,11 +48,12 @@ const parseSfc = (sfcContent:string) => {
const composeSfc = (
scriptContent:string,
templateContent:string,
styleContent:string
styleContent:string,
styleAttributes:string
) => {
const templateSfc = `<template>${templateContent}</template>`;
let scriptSfc = `&lt;script setup&gt;\n${scriptContent}\n&lt;/script&gt;`;
const styleSfc = `<style scoped>${styleContent}</style>`;
const styleSfc = `<style ${styleAttributes}>${styleContent}</style>`;
scriptSfc = scriptSfc.replaceAll('&lt;', '<').replaceAll('&gt;', '>');
return [templateSfc, scriptSfc, styleSfc].join('\n\n');
};
Expand Down

0 comments on commit 3190b91

Please sign in to comment.