Skip to content

Commit

Permalink
fix: skip ssr build, fix babel target bug
Browse files Browse the repository at this point in the history
  • Loading branch information
hemengke1997 committed Apr 26, 2024
1 parent 354a827 commit fdbf5f4
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 16 deletions.
1 change: 0 additions & 1 deletion README-zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ pnpm add vite-plugin-public-typescript -D
| manifestName | `string` | `manifest` | `manifest` 的文件名 |
| hash | `boolean` | `true` | 编译后的 `js` 是否生成 `hash ` |
| esbuildOptions | `BuildOptions` | `{}` | esbuild 构建选项 |
| ssrBuild | `boolean` | `false` | 当前打包环境是否是 ssr |
| ~~sideEffects~~ | `boolean` | `true` | 是否编译三方库(v2.0.0废弃⚠️) |
| destination | `string` | `memory` | 输出模式:内存模式 \| 文件模式 |
| cacheDir | `string` | `node_modules/.vite-plugin-public-typescript` | 存放manifest缓存的目录 |
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"author": "hemengke <https://github.com/hemengke1997>",
"files": [
"*.d.ts",
"CHANGELOG.md",
"README.md",
"dist",
"package.json"
Expand All @@ -38,7 +37,9 @@
"types": "./dist/client/index.d.ts",
"require": "./dist/client/index.cjs",
"import": "./dist/client/index.js"
}
},
"./package.json": "./package.json",
"./*": "./*"
},
"scripts": {
"dev": "simple-git-hooks && tsup --watch",
Expand All @@ -53,10 +54,10 @@
"up": "pnpm update --i --L",
"taze": "taze -I -w",
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0",
"bump": "bumpp package.json -c -p -t --all -x \"pnpm run changelog\""
"bump": "bumpp package.json -c --no-push -t --all -x \"pnpm run changelog\""
},
"peerDependencies": {
"vite": ">=4.0.0"
"vite": ">=4.0.0 || >=5.0.0"
},
"dependencies": {
"@antfu/install-pkg": "^0.1.1",
Expand Down
14 changes: 8 additions & 6 deletions playground/ssr/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,15 @@ export async function createServer(root = process.cwd(), isProd = process.env.NO

let html = template.replace(`<!--app-html-->`, appHtml)

html = injectScriptsToHtml(html, (manifest) => [
{
attrs: {
src: manifest.ssr,
html = injectScriptsToHtml(html, (manifest) => {
return [
{
attrs: {
src: manifest.ssr,
},
},
},
])
]
})

res.status(200).set({ 'Content-Type': 'text/html' }).end(html)
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion playground/ssr/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { defineConfig } from 'vite'
import { publicTypescript } from 'vite-plugin-public-typescript'

export default defineConfig({
plugins: [react(), publicTypescript()],
plugins: [react(), publicTypescript({ babel: true })],
build: {
minify: false,
},
Expand Down
2 changes: 1 addition & 1 deletion src/node/build/babel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function loadPlugin(path: string): Promise<any> {
const cached = loadedPlugin.get(path)
if (cached) return cached

const promise = import(path).then((module) => {
const promise = import(/* @vite-ignore */ path).then((module) => {
const value = module.default || module
loadedPlugin.set(path, value)
return value
Expand Down
8 changes: 6 additions & 2 deletions src/node/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,12 @@ export async function esbuildTypescript(buildOptions: IBuildOptions) {
if (enableBabel) {
const { default: browserslist } = await import('browserslist')
babelTarget = browserslist.loadConfig({ path: viteConfig.root }) || []
const { resolveToEsbuildTarget } = await import('esbuild-plugin-browserslist')
esbuildTarget = resolveToEsbuildTarget(browserslist(babelTarget), { printUnknownTargets: false })
if (!babelTarget.length) {
esbuildTarget = [DEFAULT_ESBUILD_TARGET]
} else {
const { resolveToEsbuildTarget } = await import('esbuild-plugin-browserslist')
esbuildTarget = resolveToEsbuildTarget(browserslist(babelTarget), { printUnknownTargets: false })
}
}

const esbuildPlugins = enableBabel
Expand Down
3 changes: 3 additions & 0 deletions src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ export default function publicTypescript(options: VPPTPluginOptions = {}) {
await initWatcher((file) => reloadPage(ws, file))
},
async buildStart() {
if (viteConfig.build.ssr) return

const manifestPath = manifestCache.manifestPath

fs.ensureFileSync(manifestPath)
Expand Down Expand Up @@ -198,6 +200,7 @@ export default function publicTypescript(options: VPPTPluginOptions = {}) {
await buildAllOnce(originFilesGlob)
},
generateBundle() {
if (viteConfig.build.ssr) return
if (opts.destination === 'memory') {
const c = manifestCache.all
Object.keys(c).forEach((key) => {
Expand Down
1 change: 0 additions & 1 deletion tests/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ describe('unit test', () => {
manifestName: 'manifest',
outputDir: '/',
sideEffects: false,
ssrBuild: false,
cacheDir: 'node_modules/.vite-plugin-public-typescript',
} as const

Expand Down

0 comments on commit fdbf5f4

Please sign in to comment.