-
Notifications
You must be signed in to change notification settings - Fork 7
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
improving esm support #102
base: master
Are you sure you want to change the base?
Changes from 24 commits
c377866
c4a247c
11d5a3d
8b802a2
38d82cc
aae91a7
0dc8393
7eec2f5
e05e14f
ae32bcd
4296cca
363f2cf
d79e21d
e575563
3785d20
55821c7
0d98636
4504ef4
6586665
684c6fc
9116dc1
2650c97
b25f8ab
9810d69
f25cb99
d113115
c3480f3
18b5fbf
a2f7342
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,19 @@ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
/** @type {import('jest').Config} */ | ||
const config = { | ||
// preset: 'ts-jest', | ||
transform: { | ||
'^.+\\.tsx?$': [ | ||
'ts-jest', | ||
{ | ||
tsconfig: './test/tsconfig.json', | ||
}, | ||
], | ||
}, | ||
testEnvironment: 'jsdom', | ||
// setupFiles: ['./test/env-setup.ts'], | ||
setupFilesAfterEnv: ['./test/test-setup.ts'], | ||
// node_modules is default. | ||
// testPathIgnorePatterns: ['/node_modules/', '/cypress/'], | ||
modulePathIgnorePatterns: ['/dist/'], | ||
|
||
globals: { | ||
'ts-jest': { | ||
diagnostics: false, | ||
}, | ||
}, | ||
}; | ||
export default config; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,61 +2,79 @@ | |
"name": "bind-event-listener", | ||
"version": "3.0.0", | ||
"private": false, | ||
"description": "Making binding and unbinding DOM events easier", | ||
"description": "Safe DOM Event binding and unbinding", | ||
"author": "Alex Reardon <[email protected]>", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/alexreardon/bind-event-listener.git" | ||
}, | ||
"keywords": [ | ||
"event listener", | ||
"event handler", | ||
"dom events", | ||
"dom", | ||
"ui", | ||
"event listener", | ||
"event handler" | ||
"bind" | ||
], | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/alexreardon/bind-event-listener.git" | ||
}, | ||
"sideEffects": false, | ||
"type": "module", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Andarist I have gone for the approach outlined in the Node conditional exports documentation. So the package is listed as a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am doing some testing now, but this seems really promising |
||
"exports": { | ||
"types": { | ||
"import": "./dist/index.d.js", | ||
"require": "./dist/index.d.cjs" | ||
}, | ||
"import": "./dist/index.js", | ||
"require": "./dist/index.cjs" | ||
}, | ||
"config": { | ||
"prettier_target": "src/**/*.{ts,js,jsx,md,json} test/**/*.{ts,js,jsx,md,json}" | ||
"prettier_target": "src/**/*.ts test/**/*.ts" | ||
}, | ||
"files": [ | ||
"/dist", | ||
"/src" | ||
], | ||
"size-limit": [ | ||
{ | ||
"path": "dist/index.js", | ||
"limit": "549B" | ||
alexreardon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"path": "./dist/bind.js", | ||
"limit": "95B" | ||
}, | ||
{ | ||
"path": "./dist/bind-all.js", | ||
"limit": "211B" | ||
} | ||
], | ||
"engines": { | ||
"node": ">=18" | ||
}, | ||
"scripts": { | ||
"typescript:check": "tsc --noEmit && tsc --noEmit --project ./test/tsconfig.json", | ||
"typescript:watch": "tsc --watch", | ||
"typescript:build": "tsc", | ||
"prettier:check": "prettier --debug-check $npm_package_config_prettier_target", | ||
"build": "yarn build:clean && yarn build:dist", | ||
"build:clean": "rimraf dist", | ||
"build:dist": "tsup", | ||
"check": "yarn check:typescript && yarn check:prettier && yarn check:dist", | ||
"check:prettier": "prettier --debug-check $npm_package_config_prettier_target", | ||
"check:typescript": "tsc --noEmit && tsc --noEmit --project ./test/tsconfig.json", | ||
"check:dist": "attw --pack . --ignore-rules no-resolution", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had to add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have added There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry this PR has changed so much 🤭 |
||
"prettier:write": "prettier --write $npm_package_config_prettier_target", | ||
"validate": "yarn typescript:check && yarn prettier:check", | ||
"test": "yarn jest", | ||
"size:check": "size-limit", | ||
"build:clean": "rimraf dist", | ||
"build": "yarn build:clean && yarn typescript:build", | ||
"test:size": "size-limit", | ||
"prepublishOnly": "yarn build" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"@arethetypeswrong/cli": "^0.15.3", | ||
"@size-limit/preset-small-lib": "^8.2.4", | ||
"@types/jest": "^29.5.1", | ||
"@types/node": "^18.16.0", | ||
"expect-type": "^0.15.0", | ||
"jest": "^29.5.0", | ||
"jest-environment-jsdom": "^29.5.0", | ||
"prettier": "^2.8.8", | ||
"prettier": "^3.2.5", | ||
"rimraf": "^5.0.0", | ||
"size-limit": "^8.2.4", | ||
"ts-expect": "^1.3.0", | ||
"ts-jest": "^29.1.0", | ||
"tsup": "^8.0.2", | ||
"typescript": "^5.0.4" | ||
}, | ||
"dependencies": {} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export { bind } from './bind'; | ||
export { bindAll } from './bind-all'; | ||
export { Binding, Listener, UnbindFn } from './types'; | ||
export type { Binding, Listener, UnbindFn } from './types'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
{ | ||
"extends": "../tsconfig.json", | ||
"compilerOptions": { | ||
"target": "ES2022", | ||
"moduleResolution": "node", | ||
"verbatimModuleSyntax": false, | ||
}, | ||
"include": ["**/*"] | ||
} | ||
"include": [ | ||
"**/*" | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,29 @@ | ||
{ | ||
"compileOnSave": true, | ||
"compilerOptions": { | ||
"outDir": "dist", | ||
"target": "es5", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shifting from es5 to es2022 (likely will be a |
||
"module": "commonjs", | ||
"moduleResolution": "node", | ||
"declaration": true, | ||
"strict": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"removeComments": true, | ||
/* Base Options: */ | ||
"esModuleInterop": true, | ||
"allowSyntheticDefaultImports": true | ||
"skipLibCheck": true, | ||
"target": "es2022", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think i'll update this to be "target": "es6" |
||
"allowJs": true, | ||
"moduleDetection": "force", | ||
"isolatedModules": true, | ||
"verbatimModuleSyntax": true, | ||
/* Strictness */ | ||
"strict": true, | ||
"noUncheckedIndexedAccess": true, | ||
"noImplicitOverride": true, | ||
/* If transpiling with TypeScript: */ | ||
"module": "es2022", | ||
/* AND if you're building for a library: */ | ||
"declaration": true, | ||
/* If your code runs in the DOM: */ | ||
"lib": [ | ||
"es2022", | ||
"dom", | ||
"dom.iterable" | ||
], | ||
}, | ||
"include": ["src/**/*"] | ||
} | ||
"include": [ | ||
"src/**/*" | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { defineConfig } from 'tsup'; | ||
|
||
export default defineConfig({ | ||
entry: ['src/*'], | ||
bundle: false, | ||
clean: true, | ||
format: ['cjs', 'esm'], | ||
dts: true, | ||
tsconfig: './tsconfig.json', | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is likely broken. You mention in the description of the PR that it introduces
exports
but it has not been done. Even if it would, you are specifyingtype: "module"
in thispackage.json
and yet your"main"
points to a CJS file with.js
extension. That can't work without an additionalpackage.json
"in-between".There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Not adding the
"exports"
was an oversight. How is it looking now?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better, question @Andarist: what changes do you think I need to make? I find it's hard to know what is the right way to make sure things will work for folks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No pressure for you to reply though. I'll keep researching :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
my reading of the node module documentation is that "
type": "module"
should mean that I can use esm everywhere.But I don't know if that is correct, or if that is only correct from a particular node version
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's correct since node 12.
yes, if you don't want to maintain compatibility with the CJS consumers. It might still be good to use
exports
in such a case as that limits what can be imported from your package (exports
is a strict allowlist for accessible files/specifiers). In fact, with that you don't even needmain
asexports
can substitute it just fineThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When you say "cjs" consumers, do you mean folks on node using "cjs"? (
require('bind-event-listener')
Would this package going esm prevent
require('bind-event-listener')
usage?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To take a step back: the intention of this PR is to give ESM consumers a better time. Right now esm consumers were relying on the bundler to wrap the cjs outputs with some lame code to get it working on esm.
Ideally folks using node or bundlers would just be able to have a good time 😊
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like I have some reading to do: https://nodejs.org/api/packages.html#dual-commonjses-module-packages
Seems like I might be able to get away with just making this package esm only
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. If you are going ESM-only then you make it impossible to
require
this package. At least until this experimental feature lands: https://nodejs.org/en/blog/announcements/v22-release-announce#support-requireing-synchronous-esm-graphs