Skip to content

Commit

Permalink
fix: vite base config
Browse files Browse the repository at this point in the history
  • Loading branch information
hemengke1997 committed Sep 20, 2023
1 parent b59312d commit e615d50
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 46 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@
"watcher": "^2.3.0"
},
"devDependencies": {
"@minko-fe/eslint-config": "^1.2.33",
"@minko-fe/tsconfig": "^1.2.33",
"@minko-fe/eslint-config": "^1.2.35",
"@minko-fe/tsconfig": "^1.2.35",
"@types/debug": "^4.1.8",
"@types/fs-extra": "^11.0.1",
"@types/mock-fs": "^4.13.1",
Expand Down
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 @@
{
"test": "/out/test.e29e5748.js",
"haha": "/out/haha.bdaaba63.js",
"index": "/out/index.a7db5a4b.js"
"test": "/spa/out/test.e29e5748.js",
"haha": "/spa/out/haha.bdaaba63.js",
"index": "/spa/out/index.e69fd919.js"
}
1 change: 0 additions & 1 deletion playground/spa/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useState } from 'react'
import manifest from '../public-typescript/manifest.json'
import reactLogo from './assets/react.svg'
import './App.css'

Expand Down
1 change: 1 addition & 0 deletions playground/spa/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import manifest from './public-typescript/manifest.json'

// https://vitejs.dev/config/
export default defineConfig((env) => ({
base: '/spa',
define: {
haha: JSON.stringify('custom define!'),
app: JSON.stringify({ hello: 'world' }),
Expand Down
48 changes: 24 additions & 24 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 10 additions & 9 deletions src/helper/AbsCacheProcessor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { normalizePath } from 'vite'
import createDebug from 'debug'
import type { ManifestCache } from './ManifestCache'
import type { TGlobalConfig } from './GlobalConfigBuilder'

const debug = createDebug('vite-plugin-public-typescript:AbsCacheProcessor ===> ')

Expand Down Expand Up @@ -45,23 +46,23 @@ export abstract class AbsCacheProcessor {
await this.addNewJs({ code, tsFileName, contentHash })
}

setCache(
args: IAddFile,
config: {
outputDir: string
},
) {
setCache(args: IAddFile, config: TGlobalConfig) {
const { contentHash, code = '', tsFileName } = args
const { outputDir } = config
const {
outputDir,
viteConfig: { base },
} = config

const outputDirPrefix = base + outputDir

function getOutputPath(p: string, hash?: string) {
hash = hash ? `.${hash}` : ''
return normalizePath(`${p}/${tsFileName}${hash}.js`)
}

let outputPath = getOutputPath(outputDir)
let outputPath = getOutputPath(outputDirPrefix)
if (contentHash) {
outputPath = getOutputPath(outputDir, contentHash)
outputPath = getOutputPath(outputDirPrefix, contentHash)
}

this.cache.set({
Expand Down
8 changes: 4 additions & 4 deletions src/helper/FileCacheProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { assert } from './assert'
import { globalConfigBuilder } from './GlobalConfigBuilder'
import { AbsCacheProcessor } from './AbsCacheProcessor'
import type { IAddFile, IDeleteFile } from './AbsCacheProcessor'
import { findAllOldJsFile, writeFile } from './utils'
import { findAllOldJsFile, stripBase, writeFile } from './utils'
import type { ManifestCache } from './ManifestCache'

const debug = createDebug('FileCacheProcessor ===> ')
Expand Down Expand Up @@ -67,14 +67,14 @@ export class FileCacheProcessor extends AbsCacheProcessor {
async addNewJs(args: IAddFile): Promise<void> {
const { code = '' } = args
const {
viteConfig: { publicDir },
viteConfig: { publicDir, base },
} = globalConfigBuilder.get()

const outPath = this.setCache(args, globalConfigBuilder.get())

const fp = normalizePath(path.join(publicDir, outPath))
const fp = normalizePath(path.join(publicDir, stripBase(outPath, base)))

await fs.ensureDir(path.dirname(fp))
fs.ensureDirSync(path.dirname(fp))

writeFile(fp, code)
}
Expand Down
8 changes: 7 additions & 1 deletion src/helper/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,14 @@ export function validateOptions(options: Required<VPPTPluginOptions>) {
}
}

export function stripBase(path: string, base: string): string {
const devBase = base[base.length - 1] === '/' ? base : `${base}/`
return path.startsWith(devBase) ? path.slice(devBase.length - 1) : path
}

// normalize dir path
export function normalizeDirPath(dir: string) {
export function normalizeAssetsDirPath(dir: string, base: string) {
dir = stripBase(dir, base)
if (dir.startsWith('/')) {
dir = dir.slice(1)
}
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
findAllOldJsFile,
getInputDir,
isEmptyObject,
normalizeDirPath,
normalizeAssetsDirPath,
reloadPage,
removeOldJsFiles,
validateOptions,
Expand Down Expand Up @@ -258,7 +258,7 @@ export default function publicTypescript(options: VPPTPluginOptions = {}) {
Object.keys(c).forEach((key) => {
this.emitFile({
type: 'asset',
fileName: normalizeDirPath(`${c[key].path}`),
fileName: normalizeAssetsDirPath(`${c[key].path}`, viteConfig.base),
source: c[key]._code,
})
})
Expand Down

0 comments on commit e615d50

Please sign in to comment.