1. vue-component-compiler
Compiler for single file Vue components
vue-component-compiler
Package: vue-component-compiler
Created by: vuejs
Last modified: Tue, 28 Jun 2022 19:12:49 GMT
Version: 2.0.0
License: MIT
Downloads: 41
Repository: https://github.com/vuejs/vue-component-compiler

Install

npm install vue-component-compiler
yarn add vue-component-compiler

vue-component-compiler

npm version Build Status

This module compile a single file Vue component like the one below into a CommonJS module that can be used in Browserify/Webpack/Component/Duo builds.

Currently supported preprocessors are:

  • stylus
  • less
  • scss (via node-sass)
  • jade
  • coffee-script
  • myth
  • es6 (via 6to5 aka babel)

Also see Registering Custom Pre-Processors.

Example

 // app.vue
<style>
  .red {
    color: #f00;
  }
</style>

<template>
  <h1 class="red">{{msg}}</h1>
</template>

<script>
  module.exports = {
    data: function () {
      return {
        msg: 'Hello world!'
      }
    }
  }
</script>

You can also mix preprocessor languages in the component file:

 // app.vue
<style lang="stylus">
.red
  color #f00
</style>

<template lang="jade">
h1(class="red") {{msg}}
</template>

<script lang="coffee">
module.exports =
  data: ->
    msg: 'Hello world!'
</script>

And you can import using the src attribute:

 <style lang="stylus" src="style.styl"></style>
<template src="template.html"></template>
<script src="./scripts/main.js"></script>

API

 var compiler = require('vue-component-compiler')
// filePath should be an absolute path, and is optional if
// the fileContent doesn't contain src imports
compiler.compile(fileContent, filePath, function (err, result) {
  // result is a common js module string
})

Registering Custom Pre-Processors

Create a vue.config.js file at where your build command is run (usually the root level of your project):

 module.exports = function (compiler) {
  
  // register a compile function for <script lang="es">
  compiler.register({
    lang: 'es',
    type: 'script',
    compile: function (content, cb) {
      // transform the content...
      cb(null, content)
    }
  })

}

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code