4.72.4 • Published 11 months ago

vue-inbrowser-compiler v4.72.4

Weekly downloads
24,390
License
MIT
Repository
github
Last release
11 months ago

vue-inbrowser-compiler

Compile vue components code into vue components objects inside of your browser

install

yarn add vue-inbrowser-compiler

usage

This library is meant to help write components for vue that can be edited through text.

compile

Compiles a string of pseudo javascript code written in es2015. It returns the body of a function as a string. Once you execute the function, it will return a VueJS component.

prototype: compile(code: string, config: BubleConfig): {script: string, style: string}

import { compile } from 'vue-inbrowser-compiler'

/**
 * render a component
 */
function getComponent(code) {
  const conpiledCode = compile(
    code,
    // pass in config options to buble to set up the output
    {
      target: { ie: 11 }
    }
  )
  const func = new Function(conpiledCode.script)
  return func()
}

The formats of the code here are the same as vue-live and vue-styleguidist

pseudo jsx

Most common use case is a simple vue template.

<button color="blue">Test This Buttton</button>

will be transformed into

return {
  template: '<Button color="blue">Test This Buttton</Button>'
}

A more advanced use case if you want to use variables

// initialize variables here and use them below
let show = true
let today = new Date();

// starting from the first line that starts with a <tag>,
// the rest is considered a template
<input type="checkbox" v-model="show">
<date-picker
  style="text-align:center;"
  v-if="show"
  :value="today"/>

will turn into

let show = true
let today = new Date();

return {
    data(){
        return{
            show: show,
            today: today
        }
    }
    template: `<input type="checkbox" v-model="show">
<date-picker
  style="text-align:center;"
  v-if="show"
  :value="today"/>`
}

Vue apps

A simple way to make it explicit

new Vue({
  template: `
<div>
  <input v-model="value" type="checkbox">
  <h1 v-if="value">I am checked</h1>
</div>`,
  data() {
    return {
      value: false
    }
  }
})

Single File Components

<template>
  <div class="hello">
    <h1>Colored Text</h1>
    <button>{{ msg }}</button>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        msg: 'Push Me'
      }
    }
  }
</script>

<style>
  .hello {
    text-align: center;
    color: #900;
  }
</style>

isCodeVueSfc

Detects if the code given corresponds to a VueJS Single File Component. If there is a <template> or a <script> tag, it will return true, otherwise return false.

prototype: isCodeVueSfc(code: string):boolean

import { isCodeVueSfc } from 'vue-inbrowser-compiler'

if (isCodeVueSfc(code)) {
  doStuffForSFC(code)
} else {
  doStuffForJSFiles(code)
}

addScopedStyle

Takes the css style passed in first argument, scopes it using the suffix and adds it to the current page.

prototype: addScopedStyle(css: string, suffix: string):void

adaptCreateElement

In order to make JSX work with the compiler, you need to specify a pragma. Since tis pragma has a different form for VueJs than for ReactJs, we need to provide an adapter.

import { compile, adaptCreateElement } from 'vue-inbrowser-compiler'

/**
 * render a JSX component
 */
function getComponent(code) {
  const conpiledCode = compile(
    code,
    // in this config we set up the jsx pragma to a higher order function
    {
      jsx: '__pragma__(h)'
    }
  )
  const func = new Function('__pragma__', conpiledCode.script)
  // now pass the higher order function to the function call
  return func(adaptCreateElement)
}
4.56.5

1 year ago

4.69.0

1 year ago

4.71.1

12 months ago

4.60.0

1 year ago

4.62.2

1 year ago

4.64.1

1 year ago

4.62.0

1 year ago

4.69.2

1 year ago

4.72.0

12 months ago

4.72.4

11 months ago

4.72.1

12 months ago

4.56.2

1 year ago

4.55.0

1 year ago

4.54.1

2 years ago

4.53.1

2 years ago

4.50.0

2 years ago

4.44.24

2 years ago

4.44.23

2 years ago

4.44.22

2 years ago

4.44.21

2 years ago

4.44.20

2 years ago

4.44.17

2 years ago

4.44.0

2 years ago

4.44.14

2 years ago

4.44.13

2 years ago

4.44.12

2 years ago

4.43.3

2 years ago

4.44.16

2 years ago

4.43.2

2 years ago

4.44.15

2 years ago

4.43.1

2 years ago

4.43.0

2 years ago

4.42.0

2 years ago

4.41.2

3 years ago

4.41.0

3 years ago

4.40.0

3 years ago

4.39.0

3 years ago

4.38.3

3 years ago

4.37.0

3 years ago

4.33.6

3 years ago

4.33.5

3 years ago

4.33.4

3 years ago

4.33.2

4 years ago

4.33.0

4 years ago

4.32.1

4 years ago

4.31.1

4 years ago

4.27.0

4 years ago

4.23.3

4 years ago

4.16.0

4 years ago

4.14.0

4 years ago

4.2.2

4 years ago

4.0.1

4 years ago

4.0.0

4 years ago

4.0.0-beta.20

4 years ago

4.0.0-beta.8

4 years ago

4.0.0-beta.3

4 years ago

3.25.1-beta.1

4 years ago

3.23.0

5 years ago

3.22.1

5 years ago

3.21.0

5 years ago

3.19.0

5 years ago

3.16.3

5 years ago

3.16.2

5 years ago

3.16.1

5 years ago

3.16.0

5 years ago

3.15.1

5 years ago

3.14.4

5 years ago

3.14.2

5 years ago

3.14.1

5 years ago

3.14.0

5 years ago

3.13.10

5 years ago

3.13.8

5 years ago