Skip to content

Commit

Permalink
refactor: code
Browse files Browse the repository at this point in the history
  • Loading branch information
hemengke1997 committed Sep 14, 2023
1 parent b36a39d commit d821547
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
10 changes: 10 additions & 0 deletions src/helper/ManifestCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,14 @@ export class ManifestCache<T extends TDefaultCache = TDefaultCache> {

return orderdCache
}

findCacheItemByPath(path: string) {
const k = Object.keys(this.cache).find((key) => {
if (this.cache[key].path === path) {
return true
}
return false
})
return k ? this.cache[k] : null
}
}
11 changes: 0 additions & 11 deletions src/helper/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import createDebug from 'debug'
import type { VPPTPluginOptions } from '..'
import { globalConfigBuilder } from './GlobalConfigBuilder'
import { assert } from './assert'
import type { TDefaultCache } from './ManifestCache'

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

Expand Down Expand Up @@ -172,16 +171,6 @@ export function normalizeDirPath(dir: string) {
return dir
}

export function findCacheItemByPath(cache: TDefaultCache, id: string) {
const k = Object.keys(cache).find((key) => {
if (cache[key].path === id) {
return true
}
return false
})
return k ? cache[k] : null
}

export function addCodeHeader(code: string) {
return `// gen via vite-plugin-public-typescript (only show in serve mode);
${code}`
Expand Down
9 changes: 3 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
_isPublicTypescript,
addCodeHeader,
eq,
findCacheItemByPath,
getInputDir,
isEmptyObject,
normalizeDirPath,
Expand Down Expand Up @@ -274,7 +273,7 @@ export default function publicTypescript(options: VPPTPluginOptions = {}) {

if (vppt?.value && src?.value) {
const c = cache.get()
let cacheItem = findCacheItemByPath(c, src.value)
let cacheItem = cache.findCacheItemByPath(src.value)

if (!cacheItem) {
const fileName = path.basename(src.value).split('.')[0]
Expand Down Expand Up @@ -312,8 +311,7 @@ export default function publicTypescript(options: VPPTPluginOptions = {}) {
apply: 'serve',
enforce: 'post',
load(id) {
const c = cache.get()
const cacheItem = findCacheItemByPath(c, id)
const cacheItem = cache.findCacheItemByPath(id)
if (cacheItem) {
return {
code: '',
Expand All @@ -325,8 +323,7 @@ export default function publicTypescript(options: VPPTPluginOptions = {}) {
server.middlewares.use((req, res, next) => {
try {
if (req?.url?.endsWith('.js') && req.url.startsWith('/')) {
const c = cache.get()
const cacheItem = findCacheItemByPath(c, req.url)
const cacheItem = cache.findCacheItemByPath(req.url)
if (cacheItem) {
return send(req, res, addCodeHeader(cacheItem._code || ''), 'js', {
cacheControl: 'no-cache',
Expand Down

0 comments on commit d821547

Please sign in to comment.