Skip to content

Commit

Permalink
fix: improve dts writing to prevent loops
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed May 24, 2022
1 parent a4b4371 commit feafa4c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
3 changes: 2 additions & 1 deletion playground/package.json
@@ -1,7 +1,8 @@
{
"private": true,
"scripts": {
"dev": "vite --open"
"dev": "vite --open",
"build": "vite build"
},
"dependencies": {
"@vueuse/core": "^8.5.0",
Expand Down
41 changes: 32 additions & 9 deletions src/core/ctx.ts
Expand Up @@ -4,6 +4,7 @@ import { slash, throttle, toArray } from '@antfu/utils'
import { createFilter } from '@rollup/pluginutils'
import { isPackageExists } from 'local-pkg'
import type { Import } from 'unimport'
// @ts-expect-error types
import { vueTemplateAddon } from 'unimport/addons'
import { createUnimport, scanDirExports } from 'unimport'
import MagicString from 'magic-string'
Expand Down Expand Up @@ -73,12 +74,32 @@ export function createContext(options: Options = {}, root = process.cwd()) {
})
}

const writeConfigFiles = throttle(500, false, () => {
if (dts)
fs.writeFile(dts, generateDTS(dts), 'utf-8')
if (eslintrc.enabled && eslintrc.filepath)
fs.writeFile(eslintrc.filepath, generateESLintConfigs(unimport.getImports(), eslintrc), 'utf-8')
})
function generateESLint() {
return generateESLintConfigs(unimport.getImports(), eslintrc)
}

const writeConfigFilesThrottled = throttle(500, false, writeConfigFiles)

let lastDTS: string | undefined
let lastESLint: string | undefined
function writeConfigFiles() {
const promises: any[] = []
if (dts) {
const content = generateDTS(dts)
if (content !== lastDTS) {
lastDTS = content
promises.push(fs.writeFile(dts, generateDTS(dts), 'utf-8'))
}
}
if (eslintrc.enabled && eslintrc.filepath) {
const content = generateESLint()
if (content !== lastESLint) {
lastESLint = content
promises.push(fs.writeFile(eslintrc.filepath, content, 'utf-8'))
}
}
return Promise.all(promises)
}

async function scanDirs() {
if (dirs?.length) {
Expand All @@ -91,7 +112,7 @@ export function createContext(options: Options = {}, root = process.cwd()) {
] as Import[]
})
}
writeConfigFiles()
writeConfigFilesThrottled()
}

async function transform(code: string, id: string) {
Expand All @@ -102,7 +123,7 @@ export function createContext(options: Options = {}, root = process.cwd()) {
if (!s.hasChanged())
return

writeConfigFiles()
writeConfigFilesThrottled()

return {
code: s.toString(),
Expand All @@ -118,9 +139,11 @@ export function createContext(options: Options = {}, root = process.cwd()) {
dirs,
filter,
scanDirs,
generateConfigFiles: writeConfigFiles,
writeConfigFiles,
writeConfigFilesThrottled,
transform,
generateDTS,
generateESLint,
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/core/unplugin.ts
Expand Up @@ -16,6 +16,9 @@ export default createUnplugin<Options>((options) => {
async buildStart() {
await ctx.scanDirs()
},
async buildEnd() {
await ctx.writeConfigFiles()
},
vite: {
async handleHotUpdate({ file }) {
if (ctx.dirs?.some(dir => file.startsWith(dir)))
Expand Down

0 comments on commit feafa4c

Please sign in to comment.