Skip to content

Commit

Permalink
fix: make setup() and <script setup> work
Browse files Browse the repository at this point in the history
  • Loading branch information
mariobuikhuizen committed Sep 26, 2024
1 parent daa98c3 commit aa6fa5b
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions js/src/esmVueTemplate.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import * as Vue from 'vue'
import { parse } from 'vue/compiler-sfc'
import { parse, compileScript, compileTemplate } from 'vue/compiler-sfc'
import esModuleShims from './es-module-shims-txt.js'

window.esmsInitOptions = { shimMode: true };

export async function compileSfc(sfcStr, mixin) {
await init()
const { descriptor: {script, template, styles} } = parse(sfcStr);
const parsedTemplate = parse(sfcStr)
const { descriptor: {script, scriptSetup, template, styles} } = parsedTemplate;

styles && styles.forEach(({content, attrs}) => {
const prefixedCssId = attrs.id && `ipyvue-${attrs.id}`;
Expand All @@ -31,9 +32,25 @@ export async function compileSfc(sfcStr, mixin) {
script.content = script.content.replace(/^[^{]+(?={)/, "export default ");
}
}
let compiledScript = (script || scriptSetup) && compileScript(parsedTemplate.descriptor, {id: "abc"});
let {setup, ...rest} = compiledScript ? (await toModule(compiledScript.content)).default : {}

const compiledTemplate = template && compileTemplate({
source: template.content,
compilerOptions: {
bindingMetadata: compiledScript ? compiledScript.bindings : {},
prefixIdentifiers: true,
}
});
if (compiledTemplate && compiledTemplate.tips.length) {
console.warn(compiledTemplate.tips);
}

const templateModule = (await toModule(compiledTemplate.code))
return {
...(template && {render: Vue.compile(template.content)}),
mixins: [script ? (await toModule(script.content)).default : {}, mixin],
...(template && templateModule),
...(setup && {setup}),
mixins: [rest || {}, mixin],
};
}

Expand Down

0 comments on commit aa6fa5b

Please sign in to comment.