-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: better patch for vue jsx types (vuejs/core#1033)
- Loading branch information
Showing
1 changed file
with
11 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |