Home

Awesome

<p align="center"> <a href="https://vitejs.dev" style="margin-right: 32px;" target="_blank" rel="noopener noreferrer"> <img width="140" src="https://vitejs.dev/logo.svg" alt="Vite logo" /> </a> <a href="https://www.typescriptlang.org/" target="_blank" rel="noopener noreferrer"> <img width="140" src="https://upload.wikimedia.org/wikipedia/commons/4/4c/Typescript_logo_2020.svg" alt="Typescript logo" /> </a> </p> <br/> <p align="center"> <a href="https://npmjs.com/package/vite-plugin-public-typescript"><img src="https://img.shields.io/npm/v/vite-plugin-public-typescript.svg" alt="npm package"></a> <a href="https://nodejs.org/en/about/previous-releases"><img src="https://img.shields.io/node/v/vite-plugin-public-typescript.svg" alt="node compatibility"></a> </p>

vite-plugin-public-typescript

English | 中文

A vite plugin inject typescript script into html

Compile typescript files in the specified directory then inject them into html

Online Demo

Demo

Why

vite-plugin-public-typescript is born to solve these problems elegantly

Install

pnpm add vite-plugin-public-typescript -D

Scenes

Features

Options

OptionTypeDefaultDescription
inputDirstringpublic-typescriptDirectory of input typescript files
publicDirstringpublicDir of vite's configVite's publicDir
outputDirstring''Directory of output javascript files after building
esbuildOptionsBuildOptions{}esbuild build options
babelboolean | ESBuildPluginBabelOptionsfalsebabel compilation (if you need to be compatible with browsers below es6, please turn it on)
manifestNamestringmanifestThe name of the manifest file
hashbooleantrueWhether the compiled js generates hash
destinationstringmemoryOutput mode: memory mode | file mode
cacheDirstringnode_modules/.vite-plugin-public-typescriptThe directory where the manifest cache is stored
basestringvite config baseResource base url

Usage

Note: The default value of 'inputDir' in 'publicTypescript' is 'public-typescript', you can also reconfigure this property. Then you need to create a folder with the same name in the same directory as' vite.config.ts' and create a '.ts' file inside it

// vite.config.ts
import { defineConfig } from 'vite'
import { publicTypescript } from 'vite-plugin-public-typescript'

export default defineConfig({
  plugins: [
    // If you use the default configuration, the source code location 'inputDir' defaults to 'public-typescript'
    publicTypescript(),
    injectScripts((manifest) => [
      {
        attrs: {
          // The file name in the directory, for example, test.ts --> manifest.test
          src: manifest.someScript,
        },
        injectTo: 'head',
      },
    ])
  ]
})

Typescript types

{
  "compilerOptions": {   
    "types": ["vite-plugin-public-typescript/virtual"]
  }
}

get manifest in client

Note: The code here can only be used in the project code, not in 'vite.config.ts' and other build time code, because it is generated after the build

import { manifest } from 'virtual:public-typescript-manifest'

console.log(manifest)

If you need to get the 'manifest' at build time, such as custom implementation of your own vite plugin 'injectScript', use the following code

import { getManifest } from 'vite-plugin-public-typescript';

console.log(getManifest())

SPA

In SPA applications, we can inject scripts into index.html via the injectScripts plugin.

For a full example, see: spa playground

vite config

import { defineConfig } from 'vite'
import { injectScripts, publicTypescript } from 'vite-plugin-public-typescript'

export default defineConfig({
  plugins: [
    publicTypescript(),
    injectScripts((manifest) => [
      {
        attrs: {
          src: manifest.spa,
        },
        injectTo: 'head-prepend',
      }
    ])
  ],
})

SSR

In an SSR application, we can easily change the html to be rendered by injecting a script into it, since the html is essentially just a string!

For a full example, see: ssr playground

vite config

import { defineConfig } from 'vite'
import { publicTypescript } from 'vite-plugin-public-typescript'

export default defineConfig({
  plugins: [
    publicTypescript(),
  ],
})

server.js

import { injectScriptsToHtml } from 'vite-plugin-public-typescript'

html = injectScriptsToHtml(html, (manifest) => [
  {
    attrs: {
      src: manifest.ssr,
    },
    injectTo: 'head-prepend',
  },
])

License

MIT