Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(all): add types to exports for node16/nodenext module resolution #2094

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 34 additions & 0 deletions .changeset/tasty-waves-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
'@web/browser-logs': patch
'@web/config-loader': patch
'@web/dev-server': patch
'@web/dev-server-core': patch
'@web/dev-server-esbuild': patch
'@web/dev-server-hmr': patch
'@web/dev-server-import-maps': patch
'@web/dev-server-legacy': patch
'@web/dev-server-rollup': patch
'@web/dev-server-storybook': patch
'@web/parse5-utils': patch
'@web/polyfills-loader': patch
'@web/rollup-plugin-copy': patch
'@web/rollup-plugin-html': patch
'@web/rollup-plugin-import-meta-assets': patch
'@web/rollup-plugin-polyfills-loader': patch
'rollup-plugin-workbox': patch
'@web/test-runner-browserstack': patch
'@web/test-runner-chrome': patch
'@web/test-runner-cli': patch
'@web/test-runner-commands': patch
'@web/test-runner-core': patch
'@web/test-runner-coverage-v8': patch
'@web/test-runner-junit-reporter': patch
'@web/test-runner-playwright': patch
'@web/test-runner-puppeteer': patch
'@web/test-runner-saucelabs': patch
'@web/test-runner-selenium': patch
'@web/test-runner-visual-regression': patch
'@web/test-runner-webdriver': patch
---

Add explicit types export to work with node16 module resolution (typescript ≥ 4.7)
10 changes: 8 additions & 2 deletions packages/browser-logs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@
"main": "dist/index.js",
"exports": {
".": {
"import": "./index.mjs",
"require": "./dist/index.js"
"import": {
"types": "./index.d.ts",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These files should be named index.d.mts... and if they are named correctly and siblings of the files they describe, then they don't actually need to be in the package exports. See some discussion here and some docs here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would work for me, but it would break all packages in typescript when not using node16/nodenext module resolution, because in that mode typescript only looks for .d.ts files.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That shouldn't be true. My understanding, reiterated in the issue I liked to, is that TypeScript only looks for .d.mts declarations files for .mjs files, and .d.cts declaration files for .cjs files. IOW, the declaration file must be named after the file its describing always, and there wasn't event a version of TypeScript that even supported .mjs files with .d.ts declarations.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're correct, but that doesn't stop it from being broken. If typescript isn't configured to use Node16/NodeNext resolution, typescript looks for the d.ts file extension but it doesn't look for .d.mts or .d.cts file extensions. That makes sense as Node doesn't autocomplete the .cjs or .mjs extensions either.

The @web/dev-server-core package has an export ./test-helpers. To support this export in non-Node16/NodeNext resolutions the package must have a ./test-helpers.d.ts file.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To support this export in non-Node16/NodeNext resolutions the package must have a ./test-helpers.d.ts file.

You’re referring to resolution in the TypeScript mode called node, which in 5.0 has been renamed to node10 to reflect what it really is. Node 10 will fail at runtime on an import of "@web/dev-server-core/test-helpers", so putting a .d.ts file at ./test-helpers.d.ts to make TypeScript think it will resolve is strictly wrong. Users who are running on a Node version newer than v10 should not be using that setting anymore.

Of course, taking my pedant hat off, users are still using that setting even though the vast majority of them are no longer using Node 10. It’s up to you to decide how and if you want to protect those users from their own misconfiguration. Placing the phantom .d.ts file there works. Other strategies are demonstrated at https://github.com/andrewbranch/example-subpath-exports-ts-compat. Particularly interesting is that you can use typesVersions to affect TypeScript node10 resolution almost exactly the same way that exports affects the modern TypeScript resolution modes.

Whatever strategy you decide to go with, you can use https://arethetypeswrong.github.io to test it. I can confirm that the issue pointed out here by @justinfagnani is indeed a problem that will be recognized by this tool.

"default": "./index.mjs"
},
"require": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
}
},
"engines": {
Expand Down
10 changes: 8 additions & 2 deletions packages/config-loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,14 @@
"types": "dist/index.d.ts",
"exports": {
".": {
"import": "./index.mjs",
"require": "./src/index.js"
"import": {
"types": "./index.d.ts",
"default": "./index.mjs"
},
"require": {
"types": "./src/index.d.ts",
"default": "./src/index.js"
}
}
}
}
25 changes: 20 additions & 5 deletions packages/dev-server-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,30 @@
"main": "dist/index.js",
"exports": {
".": {
"import": "./index.mjs",
"require": "./dist/index.js"
"import": {
"types": "./index.d.ts",
"default": "./index.mjs"
},
"require": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
},
"./dist/dom5": {
"require": "./dist/dom5/index.js"
"require": {
"types": "./dist/dom5/index.d.ts",
"default": "./dist/dom5/index.js"
}
},
"./test-helpers": {
"import": "./test-helpers.mjs",
"require": "./dist/test-helpers.js"
"import": {
"types": "./test-helpers.d.ts",
"default": "./test-helpers.mjs"
},
"require": {
"types": "./dist/test-helpers.d.ts",
"default": "./dist/test-helpers.js"
}
}
},
"engines": {
Expand Down
10 changes: 8 additions & 2 deletions packages/dev-server-esbuild/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@
"main": "dist/index.js",
"exports": {
".": {
"import": "./index.mjs",
"require": "./dist/index.js"
"import": {
"types": "./index.d.ts",
"default": "./index.mjs"
},
"require": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
}
},
"engines": {
Expand Down
10 changes: 8 additions & 2 deletions packages/dev-server-hmr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@
"main": "dist/index.js",
"exports": {
".": {
"import": "./index.mjs",
"require": "./dist/index.js"
"import": {
"types": "./index.d.ts",
"default": "./index.mjs"
},
"require": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
}
},
"engines": {
Expand Down
10 changes: 8 additions & 2 deletions packages/dev-server-import-maps/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@
"main": "dist/index.js",
"exports": {
".": {
"import": "./index.mjs",
"require": "./dist/index.js"
"import": {
"types": "./index.d.ts",
"default": "./index.mjs"
},
"require": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
}
},
"engines": {
Expand Down
10 changes: 8 additions & 2 deletions packages/dev-server-legacy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@
"main": "dist/index.js",
"exports": {
".": {
"import": "./index.mjs",
"require": "./dist/index.js"
"import": {
"types": "./index.d.ts",
"default": "./index.mjs"
},
"require": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
}
},
"engines": {
Expand Down
10 changes: 8 additions & 2 deletions packages/dev-server-rollup/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@
"main": "dist/index.js",
"exports": {
".": {
"import": "./index.mjs",
"require": "./dist/index.js"
"import": {
"types": "./index.d.ts",
"default": "./index.mjs"
},
"require": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
}
},
"engines": {
Expand Down
10 changes: 8 additions & 2 deletions packages/dev-server-storybook/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@
},
"exports": {
".": {
"import": "./index.mjs",
"require": "./dist/index.js"
"import": {
"types": "./index.d.ts",
"default": "./index.mjs"
},
"require": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
}
},
"engines": {
Expand Down
10 changes: 8 additions & 2 deletions packages/dev-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@
},
"exports": {
".": {
"import": "./index.mjs",
"require": "./dist/index.js"
"import": {
"types": "./index.d.ts",
"default": "./index.mjs"
},
"require": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
}
},
"engines": {
Expand Down
10 changes: 8 additions & 2 deletions packages/parse5-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@
"main": "src/index.js",
"exports": {
".": {
"import": "./index.mjs",
"require": "./src/index.js"
"import": {
"types": "./index.d.ts",
"default": "./index.mjs"
},
"require": {
"types": "./src/index.d.ts",
"default": "./src/index.js"
}
}
},
"engines": {
Expand Down
10 changes: 8 additions & 2 deletions packages/polyfills-loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@
"main": "dist/index.js",
"exports": {
".": {
"import": "./index.mjs",
"require": "./dist/index.js"
"import": {
"types": "./index.d.ts",
"default": "./index.mjs"
},
"require": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
}
},
"engines": {
Expand Down
10 changes: 8 additions & 2 deletions packages/rollup-plugin-copy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@
"module": "index.mjs",
"exports": {
".": {
"import": "./index.mjs",
"require": "./src/copy.js"
"import": {
"types": "./index.d.ts",
"default": "./index.mjs"
},
"require": {
"types": "./src/copy.d.ts",
"default": "./src/copy.js"
}
}
},
"engines": {
Expand Down
10 changes: 8 additions & 2 deletions packages/rollup-plugin-html/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@
"main": "dist/index.js",
"exports": {
".": {
"import": "./index.mjs",
"require": "./dist/index.js"
"import": {
"types": "./index.d.ts",
"default": "./index.mjs"
},
"require": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
}
},
"engines": {
Expand Down
10 changes: 8 additions & 2 deletions packages/rollup-plugin-import-meta-assets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@
"main": "src/index.js",
"exports": {
".": {
"import": "./index.mjs",
"require": "./src/index.js"
"import": {
"types": "./index.d.ts",
"default": "./index.mjs"
},
"require": {
"types": "./src/index.d.ts",
"default": "./src/index.js"
}
}
},
"engines": {
Expand Down
10 changes: 8 additions & 2 deletions packages/rollup-plugin-polyfills-loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@
"main": "dist/index.js",
"exports": {
".": {
"import": "./index.mjs",
"require": "./dist/index.js"
"import": {
"types": "./index.d.ts",
"default": "./index.mjs"
},
"require": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
}
},
"engines": {
Expand Down
10 changes: 8 additions & 2 deletions packages/rollup-plugin-workbox/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@
"module": "index.mjs",
"exports": {
".": {
"import": "./index.mjs",
"require": "./dist/index.js"
"import": {
"types": "./index.d.ts",
"default": "./index.mjs"
},
"require": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
}
},
"scripts": {
Expand Down
10 changes: 8 additions & 2 deletions packages/test-runner-browserstack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@
"main": "dist/index.js",
"exports": {
".": {
"import": "./index.mjs",
"require": "./dist/index.js"
"import": {
"types": "./index.d.ts",
"default": "./index.mjs"
},
"require": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
}
},
"engines": {
Expand Down
10 changes: 8 additions & 2 deletions packages/test-runner-chrome/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@
"main": "dist/index.js",
"exports": {
".": {
"import": "./index.mjs",
"require": "./dist/index.js"
"import": {
"types": "./index.d.ts",
"default": "./index.mjs"
},
"require": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
}
},
"engines": {
Expand Down
10 changes: 8 additions & 2 deletions packages/test-runner-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@
"main": "dist/index.js",
"exports": {
".": {
"import": "./index.mjs",
"require": "./dist/index.js"
"import": {
"types": "./index.d.ts",
"default": "./index.mjs"
},
"require": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
}
},
"engines": {
Expand Down