npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vue-tsx.macro

v0.3.5

Published

make tsx work for vue2

Downloads

89

Readme

vue-tsx.macro

Make TSX (JSX for Typescript) work for Vue 2.

HOW to use with vue-cli?

SUGGESTION: Try vue-cli-plugin-tsx to simplify the steps!

  • Use vue-cli to create a project with typescript and jsx support

  • NOT COMPATIBLE with @vue/cli-plugin-typescript.

    yarn remove @vue/cli-plugin-typescript
  • Install babel-plugin-macros and vue-tsx.macro

    yarn add -D babel-plugin-macros vue-tsx.macro
  • Setup webpack to resolve .ts / .tsx through babel with typescript preset and macros plugin.

    Here is an example to config webpack through vue-cli plugin.

    // modified from @vue/cli-plugin-typescript
    const path = require('path')
    
    module.exports = (api, options) => {
      const useThreads = process.env.NODE_ENV === 'production' && options.parallel
    
      api.chainWebpack(config => {
        config.resolveLoader.modules.prepend(
          path.join(__dirname, '../node_modules'),
        )
    
        if (!options.pages) {
          config
            .entry('app')
            .clear()
            .add('./src/main.ts')
        }
    
        config.resolve.extensions.merge(['.ts', '.tsx'])
    
        const tsRule = config.module.rule('ts').test(/\.ts$/)
        const tsxRule = config.module.rule('tsx').test(/\.tsx$/)
    
        // add a loader to both *.ts & vue<lang="ts">
        const addLoader = ({ loader, options }) => {
          tsRule
            .use(loader)
            .loader(loader)
            .options(options)
          tsxRule
            .use(loader)
            .loader(loader)
            .options(options)
        }
    
        addLoader({
          loader: 'cache-loader',
          options: api.genCacheConfig(
            'ts-babel-loader',
            {
              '@babel/core': require('@babel/core/package.json').version,
              '@babel/preset-typescript': require('@babel/preset-typescript/package.json')
                .version,
              typescript: require('typescript/package.json').version,
              modern: !!process.env.VUE_CLI_MODERN_BUILD,
              browserslist: api.service.pkg.browserslist,
            },
            ['tsconfig.json', 'babel.config.js', '.browserslistrc'],
          ),
        })
    
        if (useThreads) {
          addLoader({
            loader: 'thread-loader',
          })
        }
    
        addLoader({
          loader: 'babel-loader',
        })
    
        if (!process.env.VUE_CLI_TEST) {
          // this plugin does not play well with jest + cypress setup (tsPluginE2e.spec.js) somehow
          // so temporarily disabled for vue-cli tests
          config
            .plugin('fork-ts-checker')
            .use(require('fork-ts-checker-webpack-plugin'), [
              {
                async: true,
                vue: true,
                formatter: 'codeframe',
                useTypescriptIncrementalApi: true,
                // https://github.com/TypeStrong/ts-loader#happypackmode-boolean-defaultfalse
                checkSyntacticErrors: useThreads,
              },
            ])
        }
      })
    }

    And update package.json to let @vue/cli-service know this plugin:

    {
      name: "...",
      ...,
      "vuePlugins": {
        "service": [
          "configs/cli-typescript-plugin.js"
        ]
      }
    }
    

    Then add @babel/preset-app and babel-plugin-macros to babel config, e.g.:

    // babel.config.js
    module.exports = {
      presets: ['@vue/app', '@babel/typescript'],
      plugins: ['macros'],
    }

    At last update tsconfig.json to disable emitting files, e.g.:

    {
      "compilerOptions": {
        "target": "esnext",
        "module": "esnext",
        "strict": true,
        "jsx": "preserve",
        "noEmit": true,
        "moduleResolution": "node",
        "resolveJsonModule": true,
        "experimentalDecorators": true,
        "esModuleInterop": true,
        "allowSyntheticDefaultImports": true,
        "forceConsistentCasingInFileNames": true,
        "baseUrl": ".",
        "paths": {
          "@/*": ["src/*"]
        },
        "lib": ["esnext", "dom", "dom.iterable", "scripthost"]
      },
      "include": ["src"],
      "exclude": ["node_modules"]
    }
  • All setups are done. We can write TSX for vue with ALMOST ALL typescript benefits now.

    import LogoAsset from '@/assets/logo.png'
    import { component, type as t, EVENTS, SLOTS } from 'vue-tsx.macro'
    import HelloWorld from '@/components/HelloWorld.vue'
    
    const Component = component({
      props: {
        // optional prop with type string | undefined.
        propWithVuePropType: String,
        // required prop with type number
        propWithVuePropDef: {
          type: Number,
          required: true,
        },
        // optional prop with type { a: number; b?: string } | undefined
        propWithTSType: t<{ a: number; b?: string }>(),
        // required prop
        propWithRequiredTSType: {
          type: t<number[]>(),
          required: true,
        },
      },
    
      // Declare component's events with their payload types.
      // This field will be removed by macro.
      [EVENTS]: {
        onEventWithStringPayload: String,
        onEventWithTSPayload: t<{ count: number }>(),
      },
    
      // Declare component's scoped slots' scope (param) types.
      // Single required child of function.
      [SLOTS]: {
        default: {
          scope: Number,
          required: true,
        }
      },
    
      render() {
        return (
          <div>
            {this.propWithTSType ? this.propWithTSType.a : undefined}
            <HelloWorld />
            {this.$scopedSlots.default(this.propWithVuePropDef)}
          </div>
        )
      },
    })
    
    const Home = component({
      // Because Vue supports function child only if it's the only child.
      // It means if we only declare scoped slots with only one default one,
      // the component can accept a function.
      render() {
        return (
          <div
            class='home'
            on={{
              click: event => {
                console.log(event.target)
              },
            }}>
            <img alt='123' src={LogoAsset} />
            <Component propWithRequiredTSType={[1, 2]} propWithVuePropDef={123} onEventWithStringPayload={data => console.log(data)}>
              {() => [<hr />]}
            </Component>
          </div>
        )
      },
    })
    
    export default Home