Skip to content

Commit

Permalink
fix: vite load js error
Browse files Browse the repository at this point in the history
  • Loading branch information
hemengke1997 committed Sep 13, 2023
1 parent 2b6eda6 commit 4effcbd
Show file tree
Hide file tree
Showing 10 changed files with 1,516 additions and 638 deletions.
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
strict-peer-dependencies=true
auto-install-peers=false
ignore-workspace-root-check=true
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"lint": "eslint .",
"preinstall": "npx only-allow pnpm",
"up": "pnpm update --i --L",
"taze": "taze -I -r -w",
"version": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0",
"bump": "bumpp package.json -c --all -p -t"
},
Expand All @@ -65,6 +66,7 @@
"conventional-changelog-cli": "^2.2.2",
"eslint": "^8.49.0",
"mock-fs": "^5.2.0",
"taze": "^0.11.2",
"tsup": "^7.2.0",
"typescript": "^5.2.2",
"vite": "^4.4.9",
Expand Down
11 changes: 6 additions & 5 deletions playground/spa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"private": true,
"scripts": {
"dev": "vite",
"debug": "cross-env DEBUG='MemoryCacheProcessor*,index*,ManifestCache*' vite",
"debug": "cross-env DEBUG=vite* vite",
"build": "vite build",
"preview": "vite preview"
},
Expand All @@ -14,12 +14,13 @@
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/react": "^18.0.28",
"@types/react-dom": "^18.0.11",
"@vitejs/plugin-react": "^3.1.0",
"@types/react": "^18.2.21",
"@types/react-dom": "^18.2.7",
"@vitejs/plugin-react": "^4.0.4",
"cross-env": "^7.0.3",
"typescript": "^4.9.5",
"vite": "4.2.1",
"vite": "4.4.9",
"vite-plugin-html": "^3.2.0",
"vite-plugin-public-typescript": "workspace:*"
}
}
6 changes: 3 additions & 3 deletions playground/spa/public-typescript/manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"haha": "/js/haha.bdaaba63.js",
"index": "/js/index.cccf1b56.js",
"test": "/js/test.40879d01.js"
"haha": "/haha.bdaaba63.js",
"test": "/test.40879d01.js",
"index": "/index.cccf1b56.js"
}
70 changes: 48 additions & 22 deletions playground/spa/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,33 @@
import type { HtmlTagDescriptor } from 'vite'
import type { HtmlTagDescriptor, PluginOption } from 'vite'
import { defineConfig } from 'vite'
import { publicTypescript } from 'vite-plugin-public-typescript'
import react from '@vitejs/plugin-react'
import { createHtmlPlugin } from 'vite-plugin-html'
import manifest from './public-typescript/manifest.json'

function setupHtml() {
const tags: Parameters<typeof createHtmlPlugin>[0] = {
minify: false,
inject: {
tags: [],
},
}

tags.inject?.tags?.push(
...([
{
tag: 'script',
attrs: {
src: manifest.index,
},
injectTo: 'head-prepend',
},
] as HtmlTagDescriptor[]),
)
const htmlPlugin: PluginOption[] = createHtmlPlugin(tags)
return htmlPlugin
}

// https://vitejs.dev/config/
export default defineConfig({
define: {
Expand All @@ -12,33 +36,35 @@ export default defineConfig({
},
plugins: [
react(),
publicTypescript({
inputDir: 'public-typescript',
manifestName: 'manifest',
hash: true,
outputDir: '/js',
destination: 'memory',
}),

{
name: 'add-script',
async transformIndexHtml(html) {
const tags: HtmlTagDescriptor[] = [
{
tag: 'script',
attrs: {
src: manifest.test,
transformIndexHtml: {
order: 'pre',
handler(html) {
const tags: HtmlTagDescriptor[] = [
{
tag: 'script',
attrs: {
src: manifest.test,
},
injectTo: 'head-prepend',
},
injectTo: 'body',
},
]
]

return {
html,
tags,
}
return {
html,
tags,
}
},
},
},
setupHtml(),
publicTypescript({
inputDir: 'public-typescript',
manifestName: 'manifest',
hash: true,
destination: 'memory',
}),
],
clearScreen: true,
})
6 changes: 3 additions & 3 deletions playground/ssr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
"compression": "^1.7.4",
"express": "^4.18.2",
"lib": "file: ./../lib",
"sirv": "^2.0.2"
"sirv": "^2.0.3"
},
"devDependencies": {
"@types/express": "^4.17.17",
"@types/node": "^18.15.5",
"@types/node": "^18.17.15",
"cross-env": "^7.0.3",
"typescript": "^4.9.5",
"vite": "^4.2.1",
"vite": "^4.4.9",
"vite-plugin-public-typescript": "workspace:*"
}
}
Loading

0 comments on commit 4effcbd

Please sign in to comment.