Skip to content

Use .mjs extension for esm files #453

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 0 additions & 2 deletions extend-expect.js

This file was deleted.

17 changes: 9 additions & 8 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 All @@ -30,7 +31,6 @@
},
"files": [
"dist",
"extend-expect.js",
"matchers.js"
],
"keywords": [
Expand Down Expand Up @@ -58,6 +58,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
3 changes: 0 additions & 3 deletions src/extend-expect.js

This file was deleted.

4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
import './extend-expect'
import * as extensions from './matchers'

expect.extend(extensions)
4 changes: 2 additions & 2 deletions src/to-have-form-values.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import isEqualWith from 'lodash/isEqualWith'
import uniq from 'lodash/uniq'
import isEqualWith from 'lodash/isEqualWith.js'
import uniq from 'lodash/uniq.js'
import escape from 'css.escape'
import {
checkHtmlElement,
Expand Down
2 changes: 1 addition & 1 deletion src/to-have-value.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import isEqualWith from 'lodash/isEqualWith'
import isEqualWith from 'lodash/isEqualWith.js'
import {
checkHtmlElement,
compareArraysAsSet,
Expand Down
4 changes: 2 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import redent from 'redent'
import cssParse from 'css/lib/parse'
import isEqual from 'lodash/isEqual'
import cssParse from 'css/lib/parse/index.js'
import isEqual from 'lodash/isEqual.js'

class GenericTypeError extends Error {
constructor(expectedString, received, matcherFn, context) {
Expand Down
2 changes: 1 addition & 1 deletion tests/setup-env.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {plugins} from 'pretty-format'
import '../src/extend-expect'
import '../src/index'

expect.addSnapshotSerializer(plugins.ConvertAnsi)