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

Use .mjs extension for esm files #453

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
"name": "@testing-library/jest-dom",
"version": "0.0.0-semantically-released",
"description": "Custom jest matchers to test the state of the DOM",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"main": "dist/index.js",
"module": "dist/index.mjs",
"type": "commonjs",
"exports": {
".": {
"require": "./dist/cjs/index.js",
"import": "./dist/esm/index.js"
"require": "./dist/index.js",
"import": "./dist/index.mjs"
},
"./matchers": {
"require": "./dist/cjs/matchers.js",
"import": "./dist/esm/matchers.js"
"require": "./dist/matchers.js",
"import": "./dist/matchers.mjs"
}
},
"engines": {
Expand All @@ -20,7 +21,7 @@
"yarn": ">=1"
},
"scripts": {
"build": "rollup -c",
"build": "rimraf dist && rollup -c",
"format": "kcd-scripts format",
"lint": "kcd-scripts lint",
"setup": "npm install && npm run validate -s",
Expand Down Expand Up @@ -58,6 +59,7 @@
"jsdom": "^16.2.1",
"kcd-scripts": "^11.1.0",
"pretty-format": "^25.1.0",
"rimraf": "^3.0.2",
"rollup": "^2.68.0"
},
"eslintConfig": {
Expand Down
33 changes: 18 additions & 15 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import path from 'path'
import pkg from './package.json'
const entries = ['src/index.js', 'src/matchers.js']

export default [
{
input: {
index: 'src/index.js',
matchers: 'src/matchers.js',
input: entries,
output: {
dir: 'dist',
entryFileNames: '[name].mjs',

Choose a reason for hiding this comment

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

You can add chunkFileNames: '[name]-[hash].mjs', so chunks will also have correct filepath.

You can also add preserveModules: true, then rollup will not generate "chunks", it will have the same file structure as your source code.

I guess the first solution is closest to how it was, so 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good eye, thanks for catching that. I've taken your first suggestion, and mirrored it on the .js as well, just for consistency / to be explicit.

chunkFileNames: '[name]-[hash].mjs',
format: 'esm',
},
external: id =>
!id.startsWith('\0') && !id.startsWith('.') && !id.startsWith('/'),
},
{
input: entries,
output: {
dir: 'dist',
entryFileNames: '[name].js',
chunkFileNames: '[name]-[hash].js',
format: 'cjs',
},
output: [
{
dir: path.dirname(pkg.exports['./matchers'].import),
format: 'esm',
},
{
dir: path.dirname(pkg.exports['./matchers'].require),
format: 'cjs',
},
],
external: id =>
!id.startsWith('\0') && !id.startsWith('.') && !id.startsWith('/'),
},
Expand Down