Skip to content

Commit

Permalink
Add support for Vue 3 <script setup> (#8045)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jengamon committed May 7, 2022
1 parent 504e546 commit b833a5f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
@@ -0,0 +1,9 @@
<script setup>
import { ref } from 'vue';
const name = ref("parcel");
</script>

<template>
<p>Hello {{ name }}</p>
</template>
8 changes: 8 additions & 0 deletions packages/core/integration-tests/test/vue.js
Expand Up @@ -105,4 +105,12 @@ describe('vue', function () {
assert(contents.includes('h2:hover'));
assert(contents.includes('.box p'));
});
it('should load <script setup> component files', async function () {
let b = await bundle(
path.join(__dirname, '/integration/vue-script-setup/App.vue'),
);
let output = (await run(b)).default;
assert.equal(typeof output.render, 'function');
assert.equal(typeof output.setup, 'function');
});
});
38 changes: 28 additions & 10 deletions packages/transformers/vue/src/VueTransformer.js
Expand Up @@ -49,7 +49,7 @@ export default (new Transformer({
canReuseAST({ast}) {
return ast.type === 'vue' && semver.satisfies(ast.version, '^3.0.0');
},
async parse({asset}) {
async parse({asset, options}) {
// TODO: This parses the vue component multiple times. Fix?
let code = await asset.getCode();
let parsed = compiler.parse(code, {
Expand All @@ -64,22 +64,35 @@ export default (new Transformer({
});
}

const descriptor = parsed.descriptor;
let id = hashObject({
filePath: asset.filePath,
source: options.mode === 'production' ? code : null,
}).slice(-6);

return {
type: 'vue',
version: '3.0.0',
program: parsed.descriptor,
program: {
...descriptor,
script:
descriptor.script != null || descriptor.scriptSetup != null
? compiler.compileScript(descriptor, {
id,
isProd: options.mode === 'production',
})
: null,
id,
},
};
},
async transform({asset, options, resolve, config}) {
let id = hashObject({
filePath: asset.filePath,
}).slice(-6);
let {template, script, styles, customBlocks, id} = nullthrows(
await asset.getAST(),
).program;
let scopeId = 'data-v-' + id;
let hmrId = id + '-hmr';
let basePath = basename(asset.filePath);
let {template, script, styles, customBlocks} = nullthrows(
await asset.getAST(),
).program;
if (asset.pipeline != null) {
return processPipeline({
asset,
Expand Down Expand Up @@ -230,8 +243,12 @@ async function processPipeline({
inMap: template.src ? undefined : template.map,
scoped: styles.some(style => style.scoped),
isFunctional,
compilerOptions: {
...config.compilerOptions,
bindingMetadata: script ? script.bindings : undefined,
},
isProd: options.mode === 'production',
id,
compilerOptions: config.compilerOptions,
});
if (templateComp.errors.length) {
throw new ThrowableDiagnostic({
Expand Down Expand Up @@ -351,7 +368,8 @@ ${
modules: style.module,
preprocessLang: style.lang || 'css',
scoped: style.scoped,
map: style.src ? undefined : style.map,
inMap: style.src ? undefined : style.map,
isProd: options.mode === 'production',
id,
});
if (styleComp.errors.length) {
Expand Down

0 comments on commit b833a5f

Please sign in to comment.