2.4.2 • Published 14 days ago

vite-plugin-public-typescript v2.4.2

Weekly downloads
-
License
MIT
Repository
github
Last release
14 days ago

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

  • If you want to execute some scripts before the page render
  • If you don't want to inject script code into index.html in a hard-coded way
  • If you want third-party scripts to have hash cache
  • If you want your project to be all typescript
  • ...

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

Install

pnpm add vite-plugin-public-typescript -D

Scenes

  • Independent third-party scripts, such as sentry, google analytics, baidu statistics, etc.
  • Scripts that you want to execute before the page is fully loaded, such as lib-flexible, etc.
  • Initialize global functions
  • ...

Features

  • Output js files with hash, no need to worry about cache
  • Default esbuild compilation, blazo fast!
  • Support babel compilation, no need to worry about browser compatibility
  • Support vite environment variables
  • Support vite HMR
  • Support different output modes (memory mode and file mode)
  • Support CSR and SSR

Options

OptionTypeDefaultDescription
inputDirstringpublic-typescriptThe directory where the typescript is stored
outputDirstringpublic-typescriptThe directory where the javascript is stored
manifestNamestringmanifestThe name of the manifest file
hashbooleantrueWhether the compiled js generates hash
esbuildOptionsBuildOptions{}esbuild build options
sideEffectsbooleantrueWhether to compile third-party libraries
destinationstringmemoryOutput mode: memory mode | file mode
cacheDirstringnode_modules/.vite-plugin-public-typescriptThe directory where the manifest cache is stored
basestringvite config baseResource base url
publicDirstringvite config publicDirpublic directory
babelboolean | ESBuildPluginBabelOptionsfalsebabel compilation (if you need to be compatible with browsers below es6, please turn it on)

Usage

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

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

get manifest in client

import { manifest } from 'vite-plugin-public-typescript/client'

console.log(manifest)

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

2.4.1

14 days ago

2.4.2

14 days ago

2.4.0

25 days ago

2.3.0

3 months ago

2.2.5

4 months ago

2.2.4

5 months ago

2.2.1

6 months ago

2.2.0

7 months ago

2.0.2

7 months ago

2.2.3

6 months ago

2.2.2

6 months ago

2.0.1

7 months ago

2.0.0

7 months ago

2.1.1

7 months ago

2.1.0

7 months ago

1.5.5

8 months ago

1.5.4

8 months ago

1.5.3

8 months ago

1.5.2

8 months ago

1.5.1

8 months ago

1.5.0

8 months ago

1.4.1

8 months ago

1.4.0

8 months ago

1.2.0

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.3.3

1 year ago

1.2.4

1 year ago

1.3.2

1 year ago

1.2.3

1 year ago

1.3.1

1 year ago

1.2.2

1 year ago

1.1.3

1 year ago

1.3.0

1 year ago

1.2.1

1 year ago

1.1.2

1 year ago

1.0.10

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

2 years ago

0.0.11

2 years ago

0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago