Skip to content

Commit

Permalink
fix(build): ensure correct typing for node esm
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed May 18, 2023
1 parent fecd5c4 commit d621d4c
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 15 deletions.
1 change: 1 addition & 0 deletions packages/vue/compiler-sfc/index.d.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '@vue/compiler-sfc'
3 changes: 1 addition & 2 deletions packages/vue/compiler-sfc/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"main": "index.js",
"module": "index.mjs",
"types": "index.d.ts"
"module": "index.mjs"
}
29 changes: 21 additions & 8 deletions packages/vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,35 @@
],
"exports": {
".": {
"types": "./dist/vue.d.ts",
"import": {
"types": "./dist/vue.d.mts",
"node": "./index.mjs",
"default": "./dist/vue.runtime.esm-bundler.js"
},
"require": "./index.js"
"require": {
"types": "./dist/vue.d.ts",
"default": "./index.js"
}
},
"./server-renderer": {
"types": "./server-renderer/index.d.ts",
"import": "./server-renderer/index.mjs",
"require": "./server-renderer/index.js"
"import": {
"types": "./server-renderer/index.d.mts",
"default": "./server-renderer/index.mjs"
},
"require": {
"types": "./server-renderer/index.d.ts",
"default": "./server-renderer/index.js"
}
},
"./compiler-sfc": {
"types": "./compiler-sfc/index.d.ts",
"import": "./compiler-sfc/index.mjs",
"require": "./compiler-sfc/index.js"
"import": {
"types": "./compiler-sfc/index.d.mts",
"default": "./compiler-sfc/index.mjs"
},
"require": {
"types": "./compiler-sfc/index.d.ts",
"default": "./compiler-sfc/index.js"
}
},
"./jsx-runtime": {
"types": "./jsx-runtime/index.d.ts",
Expand Down
1 change: 1 addition & 0 deletions packages/vue/server-renderer/index.d.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '@vue/server-renderer'
5 changes: 2 additions & 3 deletions packages/vue/server-renderer/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"main": "index.js",
"module": "index.mjs",
"types": "index.d.ts"
}
"module": "index.mjs"
}
25 changes: 23 additions & 2 deletions rollup.dts.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @ts-check
import { parse } from '@babel/parser'
import { existsSync, readdirSync, readFileSync } from 'fs'
import { existsSync, readdirSync, readFileSync, writeFileSync } from 'fs'
import MagicString from 'magic-string'
import dts from 'rollup-plugin-dts'
import { walk } from 'estree-walker'
Expand All @@ -25,7 +25,7 @@ export default targetPackages.map(pkg => {
file: `packages/${pkg}/dist/${pkg}.d.ts`,
format: 'es'
},
plugins: [dts(), patchTypes(pkg)],
plugins: [dts(), patchTypes(pkg), ...(pkg === 'vue' ? [copyMts()] : [])],
onwarn(warning, warn) {
// during dts rollup, everything is externalized by default
if (
Expand Down Expand Up @@ -232,3 +232,24 @@ function patchTypes(pkg) {
}
}
}

/**
* According to https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-7.html#packagejson-exports-imports-and-self-referencing
* the only way to correct provide types for both Node ESM and CJS is to have
* two separate declaration files, so we need to copy vue.d.ts to vue.d.mts
* upon build.
*
* @returns {import('rollup').Plugin}
*/
function copyMts() {
return {
name: 'copy-vue-mts',
writeBundle(_, bundle) {
writeFileSync(
'packages/vue/dist/vue.d.mts',
// @ts-ignore
bundle['vue.d.ts'].code
)
}
}
}

0 comments on commit d621d4c

Please sign in to comment.