Skip to content

Commit 17cb83f

Browse files
authored
feat: Add ESM (#2508)
1 parent c495b7c commit 17cb83f

29 files changed

+92
-75
lines changed

.eslintrc.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@
6161
"jsdoc/tag-lines": 0,
6262

6363
"node/no-unsupported-features/es-syntax": 0,
64-
"node/no-missing-import": [2, { "tryExtensions": [".js", ".json", ".ts"] }]
64+
"node/file-extension-in-import": ["error", "always"],
65+
"node/no-missing-import": 0
6566
},
6667
"settings": {
6768
"jsdoc": {

benchmark/benchmark.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import Suites from './suite';
2-
import type { Cheerio } from '../src/cheerio';
1+
import Suites from './suite'; // eslint-disable-line node/file-extension-in-import
2+
import type { Cheerio } from '../src/cheerio.js';
33
import type { Element } from 'domhandler';
44

55
const suites = new Suites();

benchmark/suite.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Suite, Event } from 'benchmark';
55
// @ts-expect-error `jsdom` types currently collide with `parse5` types.
66
import { JSDOM } from 'jsdom';
77
import { Script } from 'vm';
8-
import cheerio from '../src';
8+
import cheerio from '../lib/index.js';
99

1010
const documentDir = path.join(__dirname, 'documents');
1111
const jQuerySrc = fs.readFileSync(

package.json

+19-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@
2626
"homepage": "https://cheerio.js.org/",
2727
"main": "lib/index.js",
2828
"types": "lib/index.d.ts",
29+
"module": "lib/esm/index.js",
30+
"exports": {
31+
".": {
32+
"require": "./lib/index.js",
33+
"import": "./lib/esm/index.js"
34+
},
35+
"./lib/slim": {
36+
"require": "./lib/slim.js",
37+
"import": "./lib/esm/slim.js"
38+
}
39+
},
2940
"files": [
3041
"lib"
3142
],
@@ -80,10 +91,12 @@
8091
"format:prettier": "npm run format:prettier:raw -- --write",
8192
"format:prettier:raw": "prettier \"**/*.{{m,c,}js,ts,md,json,yml}\" --ignore-path .gitignore",
8293
"build:docs": "typedoc --hideGenerator src/index.ts",
83-
"benchmark": "ts-node benchmark/benchmark.ts --regex \"^(?!.*highmem)\"",
94+
"benchmark": "npm run build:cjs && ts-node benchmark/benchmark.ts --regex \"^(?!.*highmem)\"",
8495
"update-sponsors": "ts-node scripts/fetch-sponsors.ts",
8596
"bench": "npm run benchmark",
86-
"build": "tsc",
97+
"build": "npm run build:cjs && npm run build:esm",
98+
"build:cjs": "tsc --sourceRoot https://raw.githubusercontent.com/cheeriojs/cheerio/$(git rev-parse HEAD)/src/",
99+
"build:esm": "npm run build:cjs -- --module esnext --target es2019 --outDir lib/esm && echo '{\"type\":\"module\"}' > lib/esm/package.json",
87100
"prepublishOnly": "npm run build",
88101
"prepare": "husky install"
89102
},
@@ -107,6 +120,9 @@
107120
"testPathIgnorePatterns": [
108121
"/__fixtures__/"
109122
],
110-
"coverageProvider": "v8"
123+
"coverageProvider": "v8",
124+
"moduleNameMapper": {
125+
"^(.*)\\.js$": "$1"
126+
}
111127
}
112128
}

src/__tests__/deprecated.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* removed in the next major release of Cheerio, but their stability should be
44
* maintained until that time.
55
*/
6-
import * as fixtures from '../__fixtures__/fixtures';
6+
import * as fixtures from '../__fixtures__/fixtures.js';
77
import cheerio from '..';
88

99
describe('deprecated APIs', () => {

src/__tests__/xml.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import cheerio from '..';
2-
import type { CheerioOptions } from '../options';
2+
import type { CheerioOptions } from '../options.js';
33

44
function xml(str: string, options?: CheerioOptions) {
55
options = { xml: true, ...options };

src/api/attributes.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import cheerio from '..';
2-
import type { Cheerio } from '../cheerio';
2+
import type { Cheerio } from '../cheerio.js';
33
import type { Element } from 'domhandler';
44
import {
55
script,
@@ -9,7 +9,7 @@ import {
99
chocolates,
1010
inputs,
1111
mixedText,
12-
} from '../__fixtures__/fixtures';
12+
} from '../__fixtures__/fixtures.js';
1313

1414
describe('$(...)', () => {
1515
let $: typeof cheerio;

src/api/attributes.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
* @module cheerio/attributes
55
*/
66

7-
import { text } from '../static';
8-
import { isTag, domEach, camelCase, cssCase } from '../utils';
7+
import { text } from '../static.js';
8+
import { isTag, domEach, camelCase, cssCase } from '../utils.js';
99
import type { AnyNode, Element } from 'domhandler';
10-
import type { Cheerio } from '../cheerio';
10+
import type { Cheerio } from '../cheerio.js';
1111
import { innerText, textContent } from 'domutils';
1212
const hasOwn = Object.prototype.hasOwnProperty;
1313
const rspace = /\s+/;

src/api/css.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import cheerio from '..';
2-
import type { Cheerio } from '../cheerio';
2+
import type { Cheerio } from '../cheerio.js';
33
import type { Element } from 'domhandler';
4-
import { mixedText } from '../__fixtures__/fixtures';
4+
import { mixedText } from '../__fixtures__/fixtures.js';
55

66
describe('$(...)', () => {
77
describe('.css', () => {

src/api/css.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { domEach, isTag } from '../utils';
1+
import { domEach, isTag } from '../utils.js';
22
import type { Element, AnyNode } from 'domhandler';
3-
import type { Cheerio } from '../cheerio';
3+
import type { Cheerio } from '../cheerio.js';
44

55
/**
66
* Get the value of a style property for the first element in the set of matched elements.

src/api/forms.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import cheerio from '../../src';
2-
import type { CheerioAPI } from '../load';
3-
import { forms } from '../__fixtures__/fixtures';
2+
import type { CheerioAPI } from '../load.js';
3+
import { forms } from '../__fixtures__/fixtures.js';
44

55
describe('$(...)', () => {
66
let $: CheerioAPI;

src/api/forms.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { AnyNode } from 'domhandler';
2-
import type { Cheerio } from '../cheerio';
3-
import { isTag } from '../utils';
2+
import type { Cheerio } from '../cheerio.js';
3+
import { isTag } from '../utils.js';
44

55
/*
66
* https://github.com/jquery/jquery/blob/2.1.3/src/manipulation/var/rcheckableType.js

src/api/manipulation.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { load } from '../../src';
22
import type { CheerioAPI, Cheerio } from '..';
3-
import { fruits, divcontainers, mixedText } from '../__fixtures__/fixtures';
3+
import { fruits, divcontainers, mixedText } from '../__fixtures__/fixtures.js';
44
import type { AnyNode, Element } from 'domhandler';
55

66
describe('$(...)', () => {

src/api/manipulation.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
*/
66

77
import { ParentNode, AnyNode, Element, Text, hasChildren } from 'domhandler';
8-
import { update as updateDOM } from '../parse';
9-
import { text as staticText } from '../static';
10-
import { domEach, cloneDom, isTag, isHtml, isCheerio } from '../utils';
8+
import { update as updateDOM } from '../parse.js';
9+
import { text as staticText } from '../static.js';
10+
import { domEach, cloneDom, isTag, isHtml, isCheerio } from '../utils.js';
1111
import { removeElement } from 'domutils';
12-
import type { Cheerio } from '../cheerio';
13-
import type { BasicAcceptedElems, AcceptedElems } from '../types';
12+
import type { Cheerio } from '../cheerio.js';
13+
import type { BasicAcceptedElems, AcceptedElems } from '../types.js';
1414

1515
/**
1616
* Create an array of nodes, recursing into arrays and parsing strings if necessary.

src/api/traversing.spec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import cheerio from '../../src';
2-
import { Cheerio } from '../cheerio';
3-
import type { CheerioAPI } from '../load';
2+
import { Cheerio } from '../cheerio.js';
3+
import type { CheerioAPI } from '../load.js';
44
import { AnyNode, Element, Text, isText } from 'domhandler';
55
import {
66
food,
@@ -11,7 +11,7 @@ import {
1111
forms,
1212
mixedText,
1313
vegetables,
14-
} from '../__fixtures__/fixtures';
14+
} from '../__fixtures__/fixtures.js';
1515

1616
function getText(el: Cheerio<Element>) {
1717
if (!el.length) return undefined;

src/api/traversing.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@ import {
1111
isDocument,
1212
Document,
1313
} from 'domhandler';
14-
import type { Cheerio } from '../cheerio';
14+
import type { Cheerio } from '../cheerio.js';
1515
import * as select from 'cheerio-select';
16-
import { domEach, isTag, isCheerio } from '../utils';
17-
import { contains } from '../static';
16+
import { domEach, isTag, isCheerio } from '../utils.js';
17+
import { contains } from '../static.js';
1818
import {
1919
getChildren,
2020
getSiblings,
2121
nextElementSibling,
2222
prevElementSibling,
2323
uniqueSort,
2424
} from 'domutils';
25-
import type { FilterFunction, AcceptedFilters } from '../types';
25+
import type { FilterFunction, AcceptedFilters } from '../types.js';
2626
const reSiblingSelector = /^\s*[~+]/;
2727

2828
/**

src/cheerio.spec.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { parseDOM } from 'htmlparser2';
22
import cheerio from '.';
3-
import * as utils from './utils';
4-
import { fruits, food, noscript } from './__fixtures__/fixtures';
5-
import type { Cheerio } from './cheerio';
3+
import * as utils from './utils.js';
4+
import { fruits, food, noscript } from './__fixtures__/fixtures.js';
5+
import type { Cheerio } from './cheerio.js';
66
import type { Element } from 'domhandler';
7-
import type { CheerioOptions } from './options';
7+
import type { CheerioOptions } from './options.js';
88

99
declare module '.' {
1010
interface Cheerio<T> {

src/cheerio.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import type { InternalOptions } from './options';
1+
import type { InternalOptions } from './options.js';
22
import type { AnyNode, Document, ParentNode } from 'domhandler';
3-
import type { BasicAcceptedElems } from './types';
3+
import type { BasicAcceptedElems } from './types.js';
44

5-
import * as Attributes from './api/attributes';
6-
import * as Traversing from './api/traversing';
7-
import * as Manipulation from './api/manipulation';
8-
import * as Css from './api/css';
9-
import * as Forms from './api/forms';
5+
import * as Attributes from './api/attributes.js';
6+
import * as Traversing from './api/traversing.js';
7+
import * as Manipulation from './api/manipulation.js';
8+
import * as Css from './api/css.js';
9+
import * as Forms from './api/forms.js';
1010

1111
type AttributesType = typeof Attributes;
1212
type TraversingType = typeof Traversing;

src/index.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import * as cheerio from './index';
2-
import * as statics from './static';
1+
import * as cheerio from './index.js';
2+
import * as statics from './static.js';
33

44
describe('index', () => {
55
it('should export all static methods', () => {

src/index.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,30 @@
33
*
44
* @category Cheerio
55
*/
6-
export type { Cheerio } from './cheerio';
6+
export type { Cheerio } from './cheerio.js';
77

88
/**
99
* Types used in signatures of Cheerio methods.
1010
*
1111
* @category Cheerio
1212
*/
13-
export * from './types';
13+
export * from './types.js';
1414
export type {
1515
CheerioOptions,
1616
HTMLParser2Options,
1717
Parse5Options,
18-
} from './options';
18+
} from './options.js';
1919
/**
2020
* Re-exporting all of the node types.
2121
*
2222
* @category DOM Node
2323
*/
2424
export type { Node, NodeWithChildren, Element, Document } from 'domhandler';
2525

26-
export type { CheerioAPI } from './load';
27-
import { getLoad } from './load';
28-
import { getParse } from './parse';
29-
import { renderWithParse5, parseWithParse5 } from './parsers/parse5-adapter';
26+
export type { CheerioAPI } from './load.js';
27+
import { getLoad } from './load.js';
28+
import { getParse } from './parse.js';
29+
import { renderWithParse5, parseWithParse5 } from './parsers/parse5-adapter.js';
3030
import renderWithHtmlparser2 from 'dom-serializer';
3131
import { parseDocument as parseWithHtmlparser2 } from 'htmlparser2';
3232

@@ -83,9 +83,9 @@ import { filters, pseudos, aliases } from 'cheerio-select';
8383
*/
8484
export const select = { filters, pseudos, aliases };
8585

86-
export * from './static';
86+
export * from './static.js';
8787

88-
import * as staticMethods from './static';
88+
import * as staticMethods from './static.js';
8989

9090
/**
9191
* In order to promote consistency with the jQuery library, users are encouraged

src/load.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import {
33
InternalOptions,
44
default as defaultOptions,
55
flatten as flattenOptions,
6-
} from './options';
7-
import * as staticMethods from './static';
8-
import { Cheerio } from './cheerio';
9-
import { isHtml, isCheerio } from './utils';
6+
} from './options.js';
7+
import * as staticMethods from './static.js';
8+
import { Cheerio } from './cheerio.js';
9+
import { isHtml, isCheerio } from './utils.js';
1010
import type { AnyNode, Document, Element, ParentNode } from 'domhandler';
11-
import type { SelectorType, BasicAcceptedElems } from './types';
11+
import type { SelectorType, BasicAcceptedElems } from './types.js';
1212

1313
type StaticType = typeof staticMethods;
1414

src/parse.spec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type { Document, Element } from 'domhandler';
2-
import { getParse } from './parse';
3-
import defaultOpts from './options';
2+
import { getParse } from './parse.js';
3+
import defaultOpts from './options.js';
44

55
import { parseDocument as parseWithHtmlparser2 } from 'htmlparser2';
6-
import { parseWithParse5 } from './parsers/parse5-adapter';
6+
import { parseWithParse5 } from './parsers/parse5-adapter.js';
77

88
const parse = getParse((content, options, isDocument, context) =>
99
options.xmlMode || options._useHtmlParser2

src/parse.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
ParentNode,
66
isDocument as checkIsDocument,
77
} from 'domhandler';
8-
import type { InternalOptions } from './options';
8+
import type { InternalOptions } from './options.js';
99

1010
/*
1111
* Parser

src/parsers/parse5-adapter.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { AnyNode, Document, isDocument, ParentNode } from 'domhandler';
22
import { parse as parseDocument, parseFragment, serializeOuter } from 'parse5';
33
import { adapter as htmlparser2Adapter } from 'parse5-htmlparser2-tree-adapter';
4-
import type { InternalOptions } from '../options';
4+
import type { InternalOptions } from '../options.js';
55

66
export function parseWithParse5(
77
content: string,

src/slim.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ export type {
1616
*
1717
* @category Cheerio
1818
*/
19-
export * from './types';
19+
export * from './types.js';
2020

21-
import { getLoad } from './load';
22-
import { getParse } from './parse';
21+
import { getLoad } from './load.js';
22+
import { getParse } from './parse.js';
2323
import render from 'dom-serializer';
2424
import { parseDocument } from 'htmlparser2';
2525

src/static.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as fixtures from './__fixtures__/fixtures';
1+
import * as fixtures from './__fixtures__/fixtures.js';
22
import cheerio, { CheerioAPI } from '.';
33

44
describe('cheerio', () => {

src/static.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import type { BasicAcceptedElems } from './types';
1+
import type { BasicAcceptedElems } from './types.js';
22
import type { CheerioAPI, Cheerio } from '.';
33
import { AnyNode, Document, isText, hasChildren } from 'domhandler';
44
import {
55
InternalOptions,
66
CheerioOptions,
77
default as defaultOptions,
88
flatten as flattenOptions,
9-
} from './options';
9+
} from './options.js';
1010
import { ElementType } from 'htmlparser2';
1111

1212
/**

0 commit comments

Comments
 (0)