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

Publish ESM and CJS #519

Merged
merged 7 commits into from
Aug 22, 2023
Merged
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
57 changes: 52 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,59 @@
"name": "@testing-library/jest-dom",
"version": "0.0.0-semantically-released",
"description": "Custom jest matchers to test the state of the DOM",
"main": "dist/index.js",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"exports": {
".": {
"require": {
"types": "./types/index.d.ts",
"default": "./dist/index.js"
},
"import": {
"types": "./types/index.d.ts",
"default": "./dist/index.mjs"
}
},
"./jest-globals": {
"require": {
"types": "./types/jest-globals.d.ts",
"default": "./dist/jest-globals.js"
},
"import": {
"types": "./types/jest-globals.d.ts",
"default": "./dist/jest-globals.mjs"
}
},
"./matchers": {
"require": {
"types": "./types/matchers.d.ts",
"default": "./dist/matchers.js"
},
"import": {
"types": "./types/matchers.d.ts",
"default": "./dist/matchers.mjs"
}
},
"./vitest": {
"require": {
"types": "./types/vitest.d.ts",
"default": "./dist/vitest.js"
},
"import": {
"types": "./types/vitest.d.ts",
"default": "./dist/vitest.mjs"
}
},
"./package.json": "./package.json"
},
"types": "types/index.d.ts",
"engines": {
"node": ">=14",
"npm": ">=6",
"yarn": ">=1"
},
"scripts": {
"build": "kcd-scripts build",
"build": "rollup -c",
"format": "kcd-scripts format",
"lint": "kcd-scripts lint",
"setup": "npm install && npm run validate -s",
Expand All @@ -36,25 +80,28 @@
"author": "Ernesto Garcia <gnapse@gmail.com> (http://gnapse.github.io)",
"license": "MIT",
"dependencies": {
"@adobe/css-tools": "^4.0.1",
"@babel/runtime": "^7.9.2",
"aria-query": "^5.0.0",
"chalk": "^3.0.0",
"@adobe/css-tools": "^4.0.1",
"css.escape": "^1.5.1",
"dom-accessibility-api": "^0.5.6",
"lodash": "^4.17.15",
"redent": "^3.0.0"
},
"devDependencies": {
"@jest/globals": "^29.6.2",
"@rollup/plugin-commonjs": "^25.0.4",
"expect": "^29.6.2",
"jest-environment-jsdom-sixteen": "^1.0.3",
"jest-watch-select-projects": "^2.0.0",
"jsdom": "^16.2.1",
"kcd-scripts": "^14.0.0",
"pretty-format": "^25.1.0",
"vitest": "^0.34.1",
"typescript": "^5.1.6"
"rollup": "^3.28.1",
"rollup-plugin-delete": "^2.0.0",
"typescript": "^5.1.6",
"vitest": "^0.34.1"
},
"peerDependencies": {
"@jest/globals": ">= 28",
Expand Down
32 changes: 32 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const del = require('rollup-plugin-delete')
const commonjs = require('@rollup/plugin-commonjs')

const entries = [
'./src/index.js',
'./src/jest-globals.js',
'./src/matchers.js',
'./src/vitest.js',
]

module.exports = [
{
input: entries,
output: [
{
dir: 'dist',
entryFileNames: '[name].mjs',
chunkFileNames: '[name]-[hash].mjs',
format: 'esm',
},
{
dir: 'dist',
entryFileNames: '[name].js',
chunkFileNames: '[name]-[hash].js',
format: 'cjs',
},
],
external: id =>
!id.startsWith('\0') && !id.startsWith('.') && !id.startsWith('/'),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Out of curiosity, when would an import start with \0?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is a convention for rollup plugins that create virtual modules. https://rollupjs.org/plugin-development/#conventions

We don't have any such plugins enabled here, so it could probably be removed if desired, but I kept it just to be safe. Honestly I don't remember writing this originally, I may have copied it from somewhere else that suggested it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks for the explanation. It's probably fine to keep in, I just haven't used rollup in several years and it stuck out to me. Esbuild has the convenient packages=external option and Rollup doesn't appear to have anything similar.

plugins: [del({targets: 'dist/*'}), commonjs()],
},
]
6 changes: 6 additions & 0 deletions src/jest-globals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* istanbul ignore file */

import globals from '@jest/globals'
import * as extensions from './matchers'

globals.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
2 changes: 1 addition & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import redent from 'redent'
import isEqual from 'lodash/isEqual'
import isEqual from 'lodash/isEqual.js'
import {parse} from '@adobe/css-tools'

class GenericTypeError extends Error {
Expand Down
6 changes: 6 additions & 0 deletions src/vitest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* istanbul ignore file */

import {expect} from 'vitest'
import * as extensions from './matchers'

expect.extend(extensions)