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 1 commit
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
25 changes: 18 additions & 7 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
import path from 'path'
import pkg from './package.json'

export default [
{
input: {
index: 'src/index.js',
matchers: 'src/matchers.js',
},
input: 'src/index.js',

Choose a reason for hiding this comment

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

Maybe it's better to provide an array for an input? So rollup can share code between files, otherwise matchers are duplicated.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not terribly familiar with rollup. How can I specify an array for input, when each one should have a specific different output?

Copy link

@sheremet-va sheremet-va Aug 11, 2022

Choose a reason for hiding this comment

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

There is an example in Vitest repo: https://github.com/vitest-dev/vitest/blob/main/packages/vitest/rollup.config.js

You will need two entries, something like this:

export default [
	  {
	    input: entries,
	    output: {
	      dir: 'dist',
	      entryFileNames: '[name].mjs',
	      format: 'esm'
	    }
	  },
	  {
	    input: entries,
	    output: {
	      dir: 'dist',
	      entryFileNames: '[name].js',
	      format: 'cjs'
	    }
	  }
]

output: [
{
dir: path.dirname(pkg.exports['./matchers'].import),
file: pkg.exports['.'].import,
format: 'esm',
},
{
dir: path.dirname(pkg.exports['./matchers'].require),
file: pkg.exports['.'].require,
format: 'cjs',
},
],
external: id =>
!id.startsWith('\0') && !id.startsWith('.') && !id.startsWith('/'),
},
{
input: 'src/matchers.js',
output: [
{
file: pkg.exports['./matchers'].import,
format: 'esm',
},
{
file: pkg.exports['./matchers'].require,
format: 'cjs',
},
],
Expand Down