Skip to content

Commit

Permalink
refactor: better patch for vue jsx types (vuejs/core#1033)
Browse files Browse the repository at this point in the history
  • Loading branch information
sibbng committed Mar 8, 2022
1 parent d0580cf commit 239d0a1
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions scripts/postinstall.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { readFile, readdir, writeFile } from 'fs/promises'
import { createRequire } from 'module'
import { join } from 'path'
import { dirname, join } from 'path'
const require = createRequire(import.meta.url)
const runtime = require.resolve('@vue/runtime-core')
const runtimeDist = join(runtime, '../dist')
const files = await readdir(runtimeDist)
const files = (await readdir(runtimeDist)).filter(file => file.endsWith('.js'))
const exports = ['setCurrentInstance', 'unsetCurrentInstance', 'currentInstance', 'createHook']
const getExports = isEsm => exports.map(e => isEsm ? `export { ${e} };` : `exports.${e} = ${e};`).join('\n')
for (const file of files) {
let content = await readFile(join(runtimeDist, file), 'utf8')
if (file.endsWith('js') && !content.match(/export.*setCurrentInstance/))
content += `\n${getExports(file.includes('esm'))}`

if (file.endsWith('d.ts') && !content.match(/VNodeDisabled/))
content = content.replace(/export declare interface VNode/g, 'export declare interface VNodeDisabled')

if (content.match(/export.*setCurrentInstance/))
continue
content += `\n${getExports(file.includes('esm'))}`
writeFile(join(runtimeDist, file), content, 'utf8')
}

// Remove Vue JSX Types
const runtimedom = require.resolve('@vue/runtime-dom')
const runtimedomTypes = join(dirname(runtimedom), './dist/runtime-dom.d.ts')
const runtimedomContent = await readFile(runtimedomTypes, 'utf8')
writeFile(runtimedomTypes, runtimedomContent.replace(/export { }[\s\S]*export {}/g, 'export {}'), 'utf8')

0 comments on commit 239d0a1

Please sign in to comment.