Skip to content

Commit

Permalink
fix: dynamic import manifest to aviod infinite loop
Browse files Browse the repository at this point in the history
  • Loading branch information
hemengke1997 committed Dec 19, 2022
1 parent 4c0ecab commit ddfa337
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 14 deletions.
32 changes: 32 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,35 @@
## [1.0.2](https://github.com/hemengke1997/vite-plugin-public-typescript/compare/v1.0.1...v1.0.2) (2022-11-26)


### Bug Fixes

* esbuild error ([8c1de9e](https://github.com/hemengke1997/vite-plugin-public-typescript/commit/8c1de9e12389bb83679faf935bc6906720ee14c8))



## [1.0.1](https://github.com/hemengke1997/vite-plugin-public-typescript/compare/v1.0.0...v1.0.1) (2022-11-25)


### Bug Fixes

* 🐛 esbuild error `The service is no longer running` ([76a3d20](https://github.com/hemengke1997/vite-plugin-public-typescript/commit/76a3d20a871946db37fde696c42f010a74231b9e))


### Performance Improvements

* fix CRLF warning on windows ([cab1e1f](https://github.com/hemengke1997/vite-plugin-public-typescript/commit/cab1e1ff9dbf7e2806c470274257cd011816e726))



## [0.0.11](https://github.com/hemengke1997/vite-plugin-public-typescript/compare/v0.0.10...v0.0.11) (2022-11-13)


### Bug Fixes

* throw esbuild error ([da1b327](https://github.com/hemengke1997/vite-plugin-public-typescript/commit/da1b327464b78d7a6e6bacde8453cb7798b5771a))



## [0.0.10](https://github.com/hemengke1997/vite-plugin-public-typescript/compare/v0.0.9...v0.0.10) (2022-11-10)


Expand Down
7 changes: 4 additions & 3 deletions README-zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ import type { HtmlTagDescriptor } from 'vite'
import { defineConfig } from 'vite'
import { publicTypescript } from 'vite-plugin-public-typescript'
import react from '@vitejs/plugin-react'
import manifest from './publicTypescript/manifest.json'

// https://vitejs.dev/config/
export default defineConfig({
Expand All @@ -70,12 +69,14 @@ export default defineConfig({
}),
{
name: 'add-script',
transformIndexHtml(html) {
async transformIndexHtml(html) {
const { spa } = await import('./publicTypescript/manifest.json')

const tags: HtmlTagDescriptor[] = [
{
tag: 'script',
attrs: {
src: manifest.spa,
src: spa,
},
injectTo: 'head-prepend',
},
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ import type { HtmlTagDescriptor } from 'vite'
import { defineConfig } from 'vite'
import { publicTypescript } from 'vite-plugin-public-typescript'
import react from '@vitejs/plugin-react'
import manifest from './publicTypescript/manifest.json'

// https://vitejs.dev/config/
export default defineConfig({
Expand All @@ -69,12 +68,14 @@ export default defineConfig({
}),
{
name: 'add-script',
transformIndexHtml(html) {
async transformIndexHtml(html) {
const { spa } = await import('./publicTypescript/manifest.json')

const tags: HtmlTagDescriptor[] = [
{
tag: 'script',
attrs: {
src: manifest.spa,
src: spa,
},
injectTo: 'head-prepend',
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"preinstall": "npx only-allow pnpm",
"upgrade:deps": "pnpm update --i --L",
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0 && git add CHANGELOG.md",
"bump": "pnpm changelog && bumpp package.json --commit --push --tag"
"bump": "pnpm changelog && bumpp package.json --commit --push --tag --all"
},
"repository": {
"type": "git",
Expand Down
7 changes: 4 additions & 3 deletions playground/spa/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { defineConfig, HtmlTagDescriptor } from 'vite'
import { publicTypescript } from 'vite-plugin-public-typescript'
import manifest from './publicTypescript/manifest.json'
import react from '@vitejs/plugin-react'

// https://vitejs.dev/config/
Expand All @@ -15,12 +14,14 @@ export default defineConfig({
}),
{
name: 'add-script',
transformIndexHtml(html) {
async transformIndexHtml(html) {
const { spa } = await import('./publicTypescript/manifest.json')

const tags: HtmlTagDescriptor[] = [
{
tag: 'script',
attrs: {
src: manifest['spa'],
src: spa,
},
injectTo: 'head-prepend',
},
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,5 @@ export function publicTypescript(options: VitePluginOptions): PluginOption {
},
}
}

export { esbuildTypescript } from './utils'
16 changes: 12 additions & 4 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ const noSideEffectsPlugin: Plugin = {
},
}

export async function build(options: BuildOptions) {
const { filePath, publicDir, esbuildOptions, outputDir, sideEffects } = options

const fileName = path.basename(filePath, path.extname(filePath))
export async function esbuildTypescript(options: BuildOptions) {
const { filePath, esbuildOptions, sideEffects } = options

const { plugins = [], ...rest } = esbuildOptions

Expand Down Expand Up @@ -70,6 +68,16 @@ export async function build(options: BuildOptions) {

const code = res!.outputFiles?.[0].text

return code
}

export async function build(options: BuildOptions) {
const { filePath, publicDir, outputDir } = options

const fileName = path.basename(filePath, path.extname(filePath))

const code = await esbuildTypescript(options)

await deleteOldFiles({
...options,
publicDir,
Expand Down

0 comments on commit ddfa337

Please sign in to comment.