Giter VIP home page Giter VIP logo

vue-pug-plugin's Introduction

vue-pug-plugin

A plugin that transforms pug templates into HTML specifically for use in Vue component templates.

The motivation for this plugin is to add first-class pug language support in the context of Vue components. Instead of writing an ugly mish-mash of pug and Vue syntax in your component, eg:

<template lang="pug">
ul
  li(v-for="item in items")
    a(v-if="item.type == 'link'" :href="item.url") some link title: {{item.title}}
    p(v-else) {{item.content}}
</template>

<script> 
  // ...Vue component JS

With vue-pug-plugin you can rely on the proper, first-class native pug syntax for iteration and conditionals, as well as var interpolation, eg:

<template lang="pug">
ul
  for item in items
    li
      if item.type == 'link'
        a(:href="item.url") some link title: #{item.title}
      else
        p= item.content
</template>

<script>
  // ...Vue component JS

Note that since pug natively allows for and if/else if/else blocks with multiple children inside them, but Vue's approach of attaching control logic to individual elements is necessarily singular, if your native pug blocks have multiple children, a template tag will be inserted to transparently make it Vue-friendly, eg:

if foo == 1
  h1 Hello
  p It's foo!

Gets translated to:

template(v-if="foo == 1")
  h1 Hello
  p It's foo!

Installation

Note pug is a peer dependency, so make sure to install both:

npm install -D vue-pug-plugin pug

Usage

You can use vue-pug-plugin with any build tool or bundler, long as you can pass pug compiler options through to the pug preprocessor. All that is required is that you pass the default export from vue-pug-plugin to the pug options plugins array

// CommonJS require syntax
const vuePugPlugin = require('vue-pug-plugin')

// ES6 import syntax
import vuePugPlugin from 'vue-pug-plugin'

Rollup / Vite

As an example, when using Rollup or Vite, add the imported vuePugPlugin to the vue() plugins pug template config:

export default {
  plugins: [
    vue({
      template: {
        preprocessOptions: { // 'preprocessOptions' is passed through to the pug compiler 
          plugins: [
            vuePugPlugin
          ]
        }
      }
    })
  ]
}

Webpack / Laravel Mix

For use with Webpack or Laravel Mix, use vue-pug-loader instead

Vue variable interpolation

You can continue to use Vue-style variable interpolation syntax (eg {{ foo }}) if you wish, but you may also prefer to use pug interpolation syntax instead.

If you prefer to stick with native pug interpolation syntax, any instance of pug buffered code will get be automatically converted to Vue antlers syntax. For example:

p= foo

Will become:

p {{foo}}

This also applies to pug string interpolation, for example:

p some normal text #{foo} hey there

Will become:

p some normal text {{foo}} hey there

Note that for Vue attribute variables you should continue to wrap them in string literals, eg:

//- correct
a(:href="someVueVar + '.com'")

//- incorrect
a(:href=someVueVar + '.com')

If you use unbuffered code, that will not be transformed, instead it will be left in the code for compile-time. If you want to output a variable from that unbuffered code in your pug template at compile-time, you can use unescaped buffered code and unescaped string interpolation. For example:

- var unbuffered = 'foo'

p!= unbuffered // <--- will insert 'foo' at compile-time, not dynamically via Vue client-side

The majority of the time when using pug inside a Vue template, you only really care about Vue data/variables, hence why the more common pug buffered/escaped symbols are transformed into the Vue antlers syntax

Loops & Vue iteration keys

As expected, you can manually specify the :key attribute on the child of a for block. For example:

for item, i in items
  p(:key="i") foo

However, if you use key as the loop index variable name, :key="key" will automatically be inserted on the looping element. For example:

for item, key in items
  p foo

Will translate to:

p(v-for="(item, key) in items" :key="key") foo

Any other loop index variable name (eg for item, index..., for item, i... etc) will not add the :key attribute to the looping element.

Importantly, if a native pug for block has multiple children, since a template wrapper will be automatically inserted, if you are using Vue 3 and need to attach a :key attribute to the inserted template looping element, you should also use key as the name of the loop index variable. For example:

for item, key in items
  p foo
  p bar

Will translate to:

template(v-for="(item, key) in items" :key="key") 
  p foo
  p bar

If you are using Vue 2 and a pug for block has multiple children, you cannot add :key to a template tag, in which case you should not rely on this automatic behaviour. Instead, manually add the :key attribute to each child element, or use your own wrapper element with something like :key="index" specified. See here for more information on the difference between Vue 2/3 and the handling of the :key attribute on template tags

vue-pug-plugin's People

Contributors

constgen avatar matthewjumpsoffbuildings avatar sodatea avatar yyx990803 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

vue-pug-plugin's Issues

Volar support

Unsupported correct? I don't see where to inject this plugin and they depend on sourcemaps anyway.

Integration with eslint

Is there a eslint plugin to allow this new syntax?

I'm getting errors like:
ESLint: Using pug each loops is forbidden.(vue-pug/no-pug-control-flow)

Error `plugin[name] is not a function` encountered when building with Vite/Vue3/Pug3

Hi, I'm having trouble getting this plugin to work. I'm packaging my app with Vite and followed the instructions on how to add the plugin. Whenever I try to run the dev server or build, I get the error that "plugin[name]" is not a function, and I confess I'm too inexperienced with the platform to say exactly why.

My Vite config is very simple, not too different from the example except that I'm using Typescript and ES Modules, neither of which seem like they should interfere with this.

import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import * as vuePugPlugin from "vue-pug-plugin";

export default defineConfig({
  root: "./src",
  build: {
    outDir: "../dist",
    emptyOutDir: true
  },
  plugins: [
    vue({
      template: {
        preprocessOptions: {
          plugins: [{
              preCodeGen: vuePugPlugin
            }]
        }
      }
    })
  ]
});

The error I get is the following:

[plugin:vite:vue] plugin[name] is not a function

/src/components/Expansion.vue
    at /node_modules/pug/lib/index.js:45:39
    at Array.reduce (<anonymous>)
    at applyPlugins (/node_modules/pug/lib/index.js:44:18)
    at compileBody (/node_modules/pug/lib/index.js:198:9)
    at Object.exports.compile (/node_modules/pug/lib/index.js:269:16)
    at /node_modules/@vue/compiler-sfc/dist/compiler-sfc.cjs.js:2317:58
    at /node_modules/@vue/compiler-sfc/dist/compiler-sfc.cjs.js:1439:5
    at new Promise (<anonymous>)
    at promisify (/node_modules/@vue/compiler-sfc/dist/compiler-sfc.cjs.js:1432:10)
    at Function.exports.pug.render (/node_modules/@vue/compiler-sfc/dist/compiler-sfc.cjs.js:2302:10

Sorry, I know this is an old project and I understand if it's not being supported, but I would appreciate any insight on this issue.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.