Skip to content

Commit

Permalink
docs: update
Browse files Browse the repository at this point in the history
  • Loading branch information
hemengke1997 committed Sep 8, 2023
1 parent d15a31b commit c10aa4d
Show file tree
Hide file tree
Showing 5 changed files with 1,395 additions and 1,372 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
dist
.idea
46 changes: 24 additions & 22 deletions README-zh.md → README-en.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
# vite-plugin-public-typescript

**中文** | [English](./README.md)
![npm][npm-img]


**English** | [中文](./README-zh.md)

## Features

- 运行时和构建时,使用 esbuild 把指定文件夹中的 typescript 转化为 javascript,打包在 public 中
- 支持 HMR
- 输出带有 hash 的 js 文件,无需担心缓存
- 可自定义 esbuild 构建选项,指定目标浏览器范围
- Transform typescript to javascript at runtime and build time
- Support HMR
- Output js with hash, no worry about cache
- Customize esbuild build options, specify target browser ranges

## Install

```bash
pnpm add vite-plugin-public-typescript -D
```


## Preview

<img src="./screenshots/ts.gif" />
Expand All @@ -32,10 +36,10 @@ export default defineConfig({

### SPA

`SPA` 应用中,我们可以通过 vite 的 `transformIndexHtml` hook 注入 script
你也可以使用 [`vite-plugin-html`](https://github.com/vbenjs/vite-plugin-html),这会使得注入更加简单
For `SPA`, you can inject script in vite `transformIndexHtml` hook.
Or you can use [`vite-plugin-html`](https://github.com/vbenjs/vite-plugin-html) that make injecting easy

完整示例请参考:[spa playground](./playground/spa/vite.config.ts)
For full example, please see [spa playground](./playground/spa/vite.config.ts)

#### vite config

Expand Down Expand Up @@ -72,16 +76,17 @@ export default defineConfig({

### SSR

`SSR` 应用中,我们可以很轻松改变需要渲染的 html,往里面注入 script,因为实质上 `html` 就只是一个字符串而已
We can easily change the html in SSR mode, because `html` is just a string template

完整示例请参考:[ssr playground](./playground/ssr/index.html)
For full example, please see [ssr playground](./playground/ssr/index.html)

#### vite config

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

// https://vitejs.dev/config/
export default defineConfig({
plugins: [
publicTypescript({
Expand All @@ -103,21 +108,18 @@ const html = template

## Options

| 参数 | 类型 | 默认值 | 描述 |
| -------------- | -------------- | ------------------ | ---------------------------------------------- |
| ssrBuild | `boolean` | `false` | 当前打包环境是否是 ssr |
| inputDir | `string` | `publicTypescript` | 存放公共 typescript 的目录 |
| outputDir | `string` | `/` | 输出公共 javascript 的目录,相对于 `publicDir` |
| manifestName | `string` | `manifest` | manifest 的文件名 |
| hash | `boolean` | `true` | js 是否生成 hash |
| esbuildOptions | `BuildOptions` | `{}` | esbuild 构建选项 |

## Example project

[vite-react-ssr-boilerplate](https://github.com/hemengke1997/vite-react-ssr-boilerplate)
| Parameter | Types | Default | Description |
| -------------- | -------------- | ------------------ | ----------------------------------------------------- |
| ssrBuild | `boolean` | `true` | whether is ssrBuild |
| inputDir | `string` | `publicTypescript` | input public typescript dir |
| outputDir | `string` | `/` | output public javascript dir, relative to `publicDir` |
| manifestName | `string` | `manifest` | js manifest fileName |
| hash | `boolean` | `true` | whether generate js fileName with hash |
| esbuildOptions | `BuildOptions` | `{}` | esbuild BuildOptions |

## License

MIT


[npm-img]: https://img.shields.io/npm/v/vite-plugin-public-typescript.svg
67 changes: 40 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,39 @@
# vite-plugin-public-typescript

**English** | [中文](./README-zh.md)
![npm][npm-img]

## Features
**中文** | [English](./README.md)

- Transform typescript to javascript at runtime and build time
- Support HMR
- Output js with hash, no worry about cache
- Customize esbuild build options, specify target browser ranges
**在vite的运行时或构建时打包指定目录下的typescript文件,供独立使用**

> 如果你希望项目中所有脚本都使用typescript编写,那么你应该试试此插件
## 应用场景

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

## 功能

- 运行时和构建时,把指定文件夹中的`typescript`文件编译为`javascript`,浏览器可直接使用
- 输出带有`hash`的js文件,无需担心缓存
- 自定义编译选项,指定目标浏览器范围,无需担心兼容性
- 支持vite环境变量
- 支持`HMR`
- 生产可用

## Install

```bash
pnpm add vite-plugin-public-typescript -D
```


## Preview

<img src="./screenshots/ts.gif" />

## Usage
## 用法

```typescript
import { defineConfig } from 'vite'
Expand All @@ -31,12 +44,17 @@ export default defineConfig({
})
```

## 例子

示例请参考 [playground](playground/spa/vite.config.ts)


### SPA

For `SPA`, you can inject script in vite `transformIndexHtml` hook.
Or you can use [`vite-plugin-html`](https://github.com/vbenjs/vite-plugin-html) that make injecting easy
`SPA` 应用中,我们可以通过 vite `transformIndexHtml` hook 注入 script
你也可以使用 [`vite-plugin-html`](https://github.com/vbenjs/vite-plugin-html),这会使得注入更加简单

For full example, please see [spa playground](./playground/spa/vite.config.ts)
完整示例请参考:[spa playground](./playground/spa/vite.config.ts)

#### vite config

Expand Down Expand Up @@ -73,17 +91,16 @@ export default defineConfig({

### SSR

We can easily change the html in SSR mode, because `html` is just a string template
`SSR` 应用中,我们可以很轻松改变需要渲染的 html,往里面注入 script,因为实质上 `html` 就只是一个字符串而已

For full example, please see [ssr playground](./playground/ssr/index.html)
完整示例请参考:[ssr playground](./playground/ssr/index.html)

#### vite config

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

// https://vitejs.dev/config/
export default defineConfig({
plugins: [
publicTypescript({
Expand All @@ -103,20 +120,16 @@ const html = template
.replace('<!--app-prehead-->', `<script src=${manifest.ssr}></script>`)
```

## Options

| Parameter | Types | Default | Description |
| -------------- | -------------- | ------------------ | ----------------------------------------------------- |
| ssrBuild | `boolean` | `true` | whether is ssrBuild |
| inputDir | `string` | `publicTypescript` | input public typescript dir |
| outputDir | `string` | `/` | output public javascript dir, relative to `publicDir` |
| manifestName | `string` | `manifest` | js manifest fileName |
| hash | `boolean` | `true` | whether generate js fileName with hash |
| esbuildOptions | `BuildOptions` | `{}` | esbuild BuildOptions |

## Example project
## 配置项

[vite-react-ssr-boilerplate](https://github.com/hemengke1997/vite-react-ssr-boilerplate)
| 参数 | 类型 | 默认值 | 描述 |
| -------------- | -------------- | ------------------ | ---------------------------------------------- |
| ssrBuild | `boolean` | `false` | 当前打包环境是否是 ssr |
| inputDir | `string` | `publicTypescript` | 存放公共 typescript 的目录 |
| outputDir | `string` | `/` | 输出公共 javascript 的目录,相对于 `publicDir` |
| manifestName | `string` | `manifest` | manifest 的文件名 |
| hash | `boolean` | `true` | js 是否生成 hash |
| esbuildOptions | `BuildOptions` | `{}` | esbuild 构建选项 |

## License

Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vite-plugin-public-typescript",
"type": "module",
"version": "1.3.3",
"description": "vite plugin generate javascript from typescript with esbuild",
"description": "A vite plugin that compiles typescript to javascript for direct use in the browser.",
"author": "hemengke <https://github.com/hemengke1997>",
"license": "MIT",
"homepage": "https://github.com/hemengke1997/vite-plugin-public-typescript",
Expand All @@ -14,7 +14,8 @@
"vite",
"public",
"typescript",
"public-typescript"
"public-typescript",
"vite browser"
],
"exports": {
".": {
Expand Down
Loading

0 comments on commit c10aa4d

Please sign in to comment.