Skip to content

Commit

Permalink
fix: auto remove outuputDir error
Browse files Browse the repository at this point in the history
  • Loading branch information
hemengke1997 committed Sep 15, 2023
1 parent b6315fa commit f42879e
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 19 deletions.
4 changes: 2 additions & 2 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.8a447dce.js"
"index": "/out/index.a7db5a4b.js",
"test": "/out/test.61522cc4.js"
}
2 changes: 1 addition & 1 deletion playground/spa/public-typescript/test.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
console.log('test')
console.log('test1')
3 changes: 2 additions & 1 deletion playground/spa/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ export default defineConfig((env) => ({
inputDir: 'public-typescript',
manifestName: 'manifest',
hash: true,
outputDir: '/out',
outputDir: 'out',
destination: 'memory',
}),
injectScripts([
{
Expand Down
19 changes: 10 additions & 9 deletions src/helper/FileCacheProcessor.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import path from 'path'
import fs from 'fs-extra'
import glob from 'tiny-glob'
import { normalizePath } from 'vite'
import createDebug from 'debug'
import { assert } from './assert'
import { globalConfigBuilder } from './GlobalConfigBuilder'
import { AbsCacheProcessor } from './AbsCacheProcessor'
import type { IAddFile, IDeleteFile } from './AbsCacheProcessor'
import { writeFile } from './utils'
import { findAllOldJsFile, writeFile } from './utils'
import type { ManifestCache } from './ManifestCache'

const debug = createDebug('FileCacheProcessor ===> ')
Expand All @@ -20,19 +19,21 @@ export class FileCacheProcessor extends AbsCacheProcessor {
}

async deleteOldJs(args: IDeleteFile): Promise<void> {
const { tsFileName, jsFileName = '' } = args
const { tsFileName, jsFileName = '', silent } = args

const {
outputDir,
cache,
viteConfig: { publicDir },
} = globalConfigBuilder.get()

let oldFiles: string[] = []
try {
fs.ensureDirSync(path.join(publicDir, outputDir))

oldFiles = await glob(normalizePath(path.join(publicDir, `${outputDir}/${tsFileName}.?(*.)js`)))
oldFiles = await findAllOldJsFile({
outputDir,
publicDir,
tsFileNames: [tsFileName],
})
} catch (e) {
console.error(e)
}
Expand All @@ -41,7 +42,7 @@ export class FileCacheProcessor extends AbsCacheProcessor {

assert(Array.isArray(oldFiles))

debug('cache:', cache.get())
debug('cache:', this.cache.get())

if (oldFiles.length) {
for (const f of oldFiles) {
Expand All @@ -51,14 +52,14 @@ export class FileCacheProcessor extends AbsCacheProcessor {
} // skip repeat js file
if (fs.existsSync(f)) {
debug('deleteOldJsFile - file exists:', f, tsFileName)
cache.remove(tsFileName)
this.cache.remove(tsFileName, { disableWatch: silent })
debug('deleteOldJsFile - cache removed:', tsFileName)
fs.remove(f)
debug('deleteOldJsFile -file removed:', f)
}
}
} else {
cache.remove(tsFileName)
this.cache.remove(tsFileName, { disableWatch: silent })
debug('cache removed:', tsFileName)
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/helper/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { WebSocketServer } from 'vite'
import { normalizePath } from 'vite'
import fs from 'fs-extra'
import createDebug from 'debug'
import glob from 'tiny-glob'
import type { VPPTPluginOptions } from '..'
import { globalConfigBuilder } from './GlobalConfigBuilder'
import { assert } from './assert'
Expand Down Expand Up @@ -179,3 +180,18 @@ export function addCodeHeader(code: string) {
export function getInputDir(resolvedRoot: string, originInputDir: string, suffix = '') {
return normalizePath(path.resolve(resolvedRoot, `${originInputDir}${suffix}`))
}

export async function findAllOldJsFile(args: { publicDir: string; outputDir: string; tsFileNames: string[] }) {
const { publicDir, outputDir, tsFileNames } = args
const dir = path.join(publicDir, outputDir)
const oldFiles: string[] = []
if (fs.existsSync(dir)) {
for (const tsFileName of tsFileNames) {
const old = await glob(normalizePath(path.join(publicDir, `${outputDir}/${tsFileName}.?(*.)js`)))
if (old.length) {
oldFiles.push(...old)
}
}
}
return oldFiles
}
20 changes: 14 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
_isPublicTypescript,
addCodeHeader,
eq,
findAllOldJsFile,
getInputDir,
isEmptyObject,
normalizeDirPath,
Expand Down Expand Up @@ -227,16 +228,23 @@ export default function publicTypescript(options: VPPTPluginOptions = {}) {

const { tsFilesGlob } = globalConfigBuilder.get()

const fileNames = tsFilesGlob.map((file) => path.parse(file).name)
const tsFileNames = tsFilesGlob.map((file) => path.parse(file).name)

debug('buildStart - tsFilesGlob:', tsFilesGlob)
debug('buildStart - fileNames:', fileNames)
debug('buildStart - tsFileNames:', tsFileNames)

if (opts.destination === 'memory') {
// delete output dir
const dir = path.join(viteConfig.publicDir, opts.outputDir)
if (fs.existsSync(dir)) {
fs.removeSync(dir)
const oldFiles = await findAllOldJsFile({
outputDir: opts.outputDir,
publicDir: viteConfig.publicDir,
tsFileNames,
})
if (oldFiles.length) {
for (const f of oldFiles) {
if (fs.existsSync(f)) {
fs.removeSync(f)
}
}
}
}

Expand Down

0 comments on commit f42879e

Please sign in to comment.