Giter Club home page Giter Club logo

sfc-composer's Introduction

sfc-composer

npm version npm downloads Github Actions Codecov

๐Ÿ‘จโ€๐Ÿ”ฌ Pre-compiler helpers for Single File Components

Usage

Install package:

# npm
npm install sfc-composer

# yarn
yarn add sfc-composer

# pnpm
pnpm install sfc-composer

Import:

// ESM
import { MagicVueSFC, createVueSFC } from 'sfc-composer'

// CommonJS
const { createVueSFC, MagicVueSFC } = require('sfc-composer')

API

MagicSFC

MagicSFC is the root interface supplied to be extended by framework-specific utils.

It exposes:

  • getSourcemap(options?: SourceMapOptions): SourceMap

Generates a version 3 sourcemap like MagicString.

  • getTransformResult(): TransformResult

Compatible with Vite transform() hook result.

  • parse(): void

Refresh the current content of the MagicSFC instance. Should be implemented by child classes.

sfc-composer currently only exposes MagicVueSFC on top of MagicSFC.

Learn more about all the usages by looking at the tests!

MagicVueSFC

MagicVueSFC acts as a wrapper of vue/compiler-sfc and MagicString.

It makes it very easy to apply changes to specific Vue SFCs <blocks> without the hassle of handling character offsets in the MagicString.

It does so by creating a proxy of every block from the SFC parsed by vue/compiler-sfc offering all of the MagicString methods for each block.

const sfc = new MagicVueSFC('<template><div>Hello World!</div></template>\n<script>\nexport default {\n  name: "MyComponent",\n};\n</script>')

sfc.script.overwrite(27, 38, 'UpdatedComponent')

console.log(sfc.toString())
// ^ '<template><div>Hello World!</div></template>\n<script>\nexport default {\n  name: "UpdatedComponent",\n};\n</script>'

Learn more about all the usages by looking at the tests!

createVueSFC

createVueSFC helps in creating a MagicVueSFC from an object-like format.

It can be useful to create components programmatically or to recompose components from existing ones.

The input format is compatible with the SFCDescriptor format that is given by vue/compiler-sfc.

import { createVueSFC } from 'sfc-composer'

const MagicVueSFC = createVueSFC({
  template: {
    content: '<div>{{ msg }}</div>',
  },
  script: {
    content: `export default {
  data() {
    return {
      msg: "Hello, world!",
    };
  },
};`,
  },
  scriptSetup: {
    content: 'const setupMsg = \'Hello from setup!\';',
  },
  styles: [
    {
      content: `.text {
  color: red;
}`,
    },
  ],
})

Learn more about all the usages by looking at the tests!

Development

  • Clone this repository
  • Install latest LTS version of Node.js
  • Enable Corepack using corepack enable
  • Install dependencies using pnpm install
  • Run interactive tests using pnpm dev

License

Made with ๐Ÿ’š

Published under MIT License.

sfc-composer's People

Contributors

tahul avatar

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.