Question presented to candidate:
"Most Vue projects are full of .vue files. What is a Single File Component, what goes in it, and what actually happens to that file before it reaches the browser?"
What a strong answer should cover:
- An SFC is a
.vuefile containing one component: a<template>block, a<script>and/or<script setup>block, and zero or more<style>blocks; custom blocks are allowed too. - It colocates markup, logic and styles by component (separation of concerns by feature, not by file type).
- Browsers cannot run
.vuefiles: a build plugin (@vitejs/plugin-vue, using@vue/compiler-sfc) compiles it into a JavaScript module whose template is a render function, and CSS. <script setup>is compile-time sugar: top-level bindings are exposed to the template, anddefineProps/defineEmitsare compiler macros.<style scoped>works by adding adata-v-xxxxattribute to elements and rewriting selectors;<style module>gives CSS modules;lang="ts"andlang="scss"select preprocessors.- Benefits: syntax highlighting and type checking in the editor, hot module replacement, scoped CSS, and compile-time template optimisations.
Clarifying questions expected:
- "Do you want the file format, or what the compiler turns it into?"
Code / implementation expected: Yes. A small SFC, ideally with what it compiles into.