Skip to content

Commit

Permalink
fix: aviod infinite loop
Browse files Browse the repository at this point in the history
  • Loading branch information
hemengke1997 committed Dec 19, 2022
1 parent f91e550 commit 869f9e2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
8 changes: 6 additions & 2 deletions README-zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ import type { HtmlTagDescriptor } from 'vite'
import { defineConfig } from 'vite'
import { publicTypescript } from 'vite-plugin-public-typescript'
import react from '@vitejs/plugin-react'
import fs from 'node:fs'
import path from 'node:path'

// https://vitejs.dev/config/
export default defineConfig({
Expand All @@ -70,13 +72,15 @@ export default defineConfig({
{
name: 'add-script',
async transformIndexHtml(html) {
const { spa } = await import('./publicTypescript/manifest.json')
const manifest =
JSON.parse(fs.readFileSync(path.resolve(__dirname, './publicTypescript/manifest.json'), 'utf-8') || '{}') ||
{}

const tags: HtmlTagDescriptor[] = [
{
tag: 'script',
attrs: {
src: spa,
src: manifest.spa,
},
injectTo: 'head-prepend',
},
Expand Down
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,14 @@ Or you can use [`vite-plugin-html`](https://github.com/vbenjs/vite-plugin-html)
For full example, please see [spa playground](./playground/spa/vite.config.ts)

#### vite config

```typescript
import type { HtmlTagDescriptor } from 'vite'
import { defineConfig } from 'vite'
import { publicTypescript } from 'vite-plugin-public-typescript'
import react from '@vitejs/plugin-react'
import fs from 'node:fs'
import path from 'node:path'

// https://vitejs.dev/config/
export default defineConfig({
Expand All @@ -69,13 +72,15 @@ export default defineConfig({
{
name: 'add-script',
async transformIndexHtml(html) {
const { spa } = await import('./publicTypescript/manifest.json')
const manifest =
JSON.parse(fs.readFileSync(path.resolve(__dirname, './publicTypescript/manifest.json'), 'utf-8') || '{}') ||
{}

const tags: HtmlTagDescriptor[] = [
{
tag: 'script',
attrs: {
src: spa,
src: manifest.spa,
},
injectTo: 'head-prepend',
},
Expand All @@ -97,6 +102,7 @@ We can easily change the html in SSR mode, because `html` is just a string templ
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'
Expand All @@ -115,15 +121,15 @@ export default defineConfig({
```

#### server.js

```js
import manifest from './publicTypescript/custom-manifest.json' assert { type: 'json' }

const html = template
// inject js
.replace(`<!--app-prehead-->`, `<script src=${manifest.ssr}></script>`)
// inject js
.replace(`<!--app-prehead-->`, `<script src=${manifest.ssr}></script>`)
```


## Options

| Parameter | Types | Default | Description |
Expand Down
8 changes: 6 additions & 2 deletions playground/spa/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { defineConfig, HtmlTagDescriptor } from 'vite'
import { publicTypescript } from 'vite-plugin-public-typescript'
import react from '@vitejs/plugin-react'
import fs from 'node:fs'
import path from 'node:path'

// https://vitejs.dev/config/
export default defineConfig({
Expand All @@ -15,13 +17,15 @@ export default defineConfig({
{
name: 'add-script',
async transformIndexHtml(html) {
const { spa } = await import('./publicTypescript/manifest.json')
const manifest =
JSON.parse(fs.readFileSync(path.resolve(__dirname, './publicTypescript/manifest.json'), 'utf-8') || '{}') ||
{}

const tags: HtmlTagDescriptor[] = [
{
tag: 'script',
attrs: {
src: spa,
src: manifest.spa,
},
injectTo: 'head-prepend',
},
Expand Down

0 comments on commit 869f9e2

Please sign in to comment.