Hone logo
Hone
Problems

Vue SSR Compiler Challenge

This challenge involves creating a simplified Server-Side Rendering (SSR) compiler for Vue.js components. You will build a tool that takes Vue SFCs (Single-File Components) and compiles them into server-renderable JavaScript functions, enabling dynamic content generation on the server before sending it to the client. This is fundamental for improving initial load performance, SEO, and enabling pre-rendering.

Problem Description

Your task is to develop a TypeScript-based tool that acts as a simplified Vue SSR compiler. This tool will process Vue Single-File Components (.vue files) and output JavaScript code that can be executed on the server to generate HTML.

Key Requirements:

  1. SFC Parsing: The compiler needs to parse a Vue SFC, extracting its template, script, and style sections. For this challenge, we will focus primarily on the template and script sections.
  2. Template Compilation: The HTML template within the SFC needs to be compiled into a VNode (Virtual DOM Node) creation function that the Vue server-renderer can understand. You do not need to implement the actual server renderer, but the output should be compatible with it.
  3. Script Integration: The JavaScript code from the <script> block should be preserved and made available for use within the compiled template function.
  4. Output Generation: The compiler should output a JavaScript string representing a function that, when executed on the server, returns the VNode representation of the Vue component.

Expected Behavior:

Given a .vue file content, the compiler should produce a JavaScript string that exports a function. This function, when called, should return an object structure that Vue's server renderer can interpret as a VNode tree.

Edge Cases:

  • Components with no <script> section.
  • Components with no <template> section (these should likely render to a placeholder or throw an error, for this challenge, an empty VNode is acceptable).
  • Basic HTML elements and attributes within the template.
  • Simple Vue template syntax like {{ interpolation }}.

Examples

Example 1:

Input: (Content of MyComponent.vue)

<template>
  <div>Hello, {{ name }}!</div>
</template>

<script lang="ts">
import { defineComponent } from 'vue';

export default defineComponent({
  data() {
    return {
      name: 'World'
    };
  }
});
</script>

Output: (Generated JavaScript string)

// This is a simplified representation of what a Vue SSR compiler might output.
// The actual output would involve more complex VNode structures and integration with Vue's internals.
// For this challenge, focus on generating a function that conceptually represents the component's structure.

export default function renderSSR() {
  // In a real scenario, this would involve creating VNodes.
  // For this simplified challenge, we'll represent the structure conceptually.
  // Assume `name` is accessible from the component instance.
  return {
    tag: 'div',
    children: 'Hello, ' + this.name + '!'
  };
}

Explanation: The compiler takes the template and interpolates the name data property. The output is a function that conceptually represents the DOM structure to be rendered on the server.

Example 2:

Input: (Content of SimpleDiv.vue)

<template>
  <div>
    <h1>Title</h1>
    <p>Some content.</p>
  </div>
</template>

<script lang="ts">
// No script section
</script>

Output: (Generated JavaScript string)

export default function renderSSR() {
  return {
    tag: 'div',
    children: [
      { tag: 'h1', children: 'Title' },
      { tag: 'p', children: 'Some content.' }
    ]
  };
}

Explanation: A component with only a template section is compiled into a VNode structure representing the nested HTML elements.

Example 3:

Input: (Content of EmptyComponent.vue)

<template>
</template>

<script lang="ts">
export default {
  // Empty component
};
</script>

Output: (Generated JavaScript string)

export default function renderSSR() {
  return null; // Or an empty VNode representation, e.g., { tag: null }
}

Explanation: An empty template results in a null or empty VNode, indicating no render output.

Constraints

  • Input Format: The input will be a string representing the content of a .vue file.
  • Script Language: The <script> block will be assumed to be TypeScript.
  • Template Syntax: Focus on basic HTML elements, attributes, and mustache {{ interpolation }}. Do not worry about directives like v-if, v-for, event handlers, or complex expressions.
  • Output Format: The output must be a TypeScript string that, when parsed as JavaScript, exports a default function.
  • Performance: For this challenge, extreme performance optimization is not the primary concern, but the compilation process should be reasonably efficient.

Notes

  • You will need a way to parse .vue files. Libraries like vue-template-compiler (for template parsing) and potentially a simple regex or string manipulation for script/style extraction might be useful, but you are encouraged to implement parsing logic as part of the challenge.
  • The goal is to simulate the logic of an SSR compiler, not to perfectly replicate Vue's internal VNode structure or compilation process. Focus on generating a function that describes the component's render output in a server-renderable format.
  • Consider how you will handle dynamic data within the template. The compiled function should conceptually access data from the component instance.
  • This challenge focuses on the "compiler" aspect. You are not implementing the actual Vue server renderer itself.
Loading editor...
typescript