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

vite-plugin-public-typescript

v3.2.0

Published

vite plugin for inject public typescript into html. (Think it as compiled public javascript)

Downloads

1,758

Readme

vite-plugin-public-typescript

中文 | English

在html中注入ts脚本的vite插件

编译指定目录下的typescript文件,注入到html中使用

在线示例

Demo

为什么要使用此插件

  • 如果你想在页面渲染之前就执行一些js代码
  • 如果你不想在硬编码式地在 index.html 中注入 script 代码
  • 如果你希望脚本有hash缓存
  • 如果你希望项目全部使用typescript
  • ...

vite-plugin-public-typescript 为优雅解决这些问题而生

安装

pnpm add vite-plugin-public-typescript -D

Scenarios

  • 独立的第三方脚本,如 sentrygoogle analytics百度统计
  • 希望在页面完全加载前就执行的脚本,如 lib-flexible
  • 初始化全局函数

功能

  • 输出带有 hash 的js文件,无需担心缓存
  • 默认 esbuild 编译,速度超快!
  • 支持 babel 编译,无需担心浏览器兼容性
  • 支持 vite 环境变量
  • 支持 vite HMR
  • 支持不同的输出方式(内存模式和文件模式)
  • 支持 CSR 和 SSR 应用

配置项

| 参数 | 类型 | 默认值 | 描述 | | -------------- | -------------------------------------- | --------------------------------------------- | ---------------------------------------------- | | inputDir | string | public-typescript | 存放需要编译的 typescript 的目录 | | publicDir | string | vite config 中的 publicDir | public 目录 | | outputDir | string | '' | 输出公共 javascript 的目录,相对于 publicDir | | esbuildOptions | BuildOptions | {} | esbuild 构建选项 | | babel | boolean | ESBuildPluginBabelOptions | false | babel编译(如果需要兼容es6以下浏览器,请开启) | | manifestName | string | manifest | manifest 的文件名 | | hash | boolean | true | 编译后的 js 是否生成 hash | | destination | string | memory | 输出模式:内存模式 | 文件模式 | | cacheDir | string | node_modules/.vite-plugin-public-typescript | 存放manifest缓存的目录 | | base | string | vite config 中的 base | 资源 base url |

用法

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

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

获取manifest

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

console.log(manifest)

SPA

SPA 应用中,我们可以通过 injectScripts 插件往 index.html 中注入 script

完整示例请参考: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

SSR 应用中,我们可以很轻松改变需要渲染的 html,往里面注入 script,因为实质上 html 就只是一个字符串而已

完整示例请参考: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