Skip to content

Commit

Permalink
docs: update
Browse files Browse the repository at this point in the history
  • Loading branch information
hemengke1997 committed Sep 15, 2023
1 parent 070e178 commit 60327a6
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 19 deletions.
37 changes: 23 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@

<!-- **中文** | [English](./README.md) -->

**在vite的运行时或构建时打包指定目录下的typescript文件,供开发者独立使用**
**在vite的运行时或构建时编译指定目录下的typescript文件,供开发者独立使用**

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

- 假设你想在页面渲染之前就执行一些js代码,应该怎么办?
- 假设你不想在硬编码式地在 `index.html` 中注入 `script` 代码,应该怎么办?
- 假设你希望第三方脚本也有hash缓存,应该怎么办?
- ...

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

## 应用场景

Expand All @@ -29,6 +36,20 @@
pnpm add vite-plugin-public-typescript -D
```

## 配置项

| 参数 | 类型 | 默认值 | 描述 |
| -------------- | -------------- | ------------------- | ---------------------------------------------- |
| inputDir | `string` | `public-typescript` | 存放需要编译的 `typescript` 的目录 |
| outputDir | `string` | `/` | 输出公共 javascript 的目录,相对于 `publicDir` |
| manifestName | `string` | `manifest` | `manifest` 的文件名 |
| hash | `boolean` | `true` | 编译后的 `js` 是否生成 `hash ` |
| esbuildOptions | `BuildOptions` | `{}` | esbuild 构建选项 |
| ssrBuild | `boolean` | `false` | 当前打包环境是否是 ssr |
| sideEffects | `boolean` | `false` |`typescript` 文件中有导入第三方库,则开启 |
| destination | `string` | `memory` | 输出模式:内存模式 \| 文件模式 |


## 用法

```typescript
Expand Down Expand Up @@ -118,18 +139,6 @@ const html = template
.replace('<!--app-prehead-->', `<script src=${manifest.ssr}></script>`)
```

## 配置项

| 参数 | 类型 | 默认值 | 描述 |
| -------------- | -------------- | ------------------- | ---------------------------------------------- |
| inputDir | `string` | `public-typescript` | 存放需要编译的 `typescript` 的目录 |
| outputDir | `string` | `/` | 输出公共 javascript 的目录,相对于 `publicDir` |
| manifestName | `string` | `manifest` | `manifest` 的文件名 |
| hash | `boolean` | `true` | 编译后的 `js` 是否生成 `hash ` |
| esbuildOptions | `BuildOptions` | `{}` | esbuild 构建选项 |
| ssrBuild | `boolean` | `false` | 当前打包环境是否是 ssr |
| sideEffects | `boolean` | `false` |`typescript` 文件中有导入第三方库,则开启 |
| destination | `string` | `memory` | 输出模式:内存模式 \| 文件模式 |

## License

Expand Down
3 changes: 2 additions & 1 deletion playground/spa/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ export default defineConfig((env) => ({
injectScripts([
{
attrs: {
src: manifest.haha,
'src': manifest.haha,
'data-n': '1',
},
injectTo: 'head',
},
Expand Down
2 changes: 0 additions & 2 deletions playground/ssr/public-typescript/ssr.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
console.log('this is ssr')

export {}
1 change: 1 addition & 0 deletions playground/ssr/public/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 12 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { assert } from './helper/assert'
import { globalConfigBuilder } from './helper/GlobalConfigBuilder'
import { initCacheProcessor } from './helper/processor'
import { ManifestCache } from './helper/ManifestCache'
import { getScriptInfo, nodeIsElement, traverseHtml } from './helper/html'
import { VPPT_DATA_ATTR, getScriptInfo, nodeIsElement, traverseHtml } from './helper/html'
import { injectScripts } from './plugins/inject-script'

const debug = createDebug('vite-plugin-public-typescript:index ===> ')
Expand Down Expand Up @@ -288,10 +288,20 @@ export default function publicTypescript(options: VPPTPluginOptions = {}) {
cacheItem = c[fileName]
}
if (cacheItem) {
const attrs = node.attrs
.reduce((acc, attr) => {
if (attr.name === VPPT_DATA_ATTR) {
return acc
}
acc += ` ${attr.name}="${attr.value}"`
return acc
}, '')
.trim()

s.update(
node.sourceCodeLocation!.startOffset,
node.sourceCodeLocation!.endOffset,
`<script src="${cacheItem?.path}"></script>`,
`<script ${attrs}></script>`,
)
} else {
s.remove(node.sourceCodeLocation!.startOffset, node.sourceCodeLocation!.endOffset)
Expand Down

0 comments on commit 60327a6

Please sign in to comment.