Skip to content

Commit

Permalink
feat!: Add esm named export support (closes #160 closes #139)
Browse files Browse the repository at this point in the history
Added wrapper for esm over CJS module and removed nonworking separate ESM build (per advice from url below).

Note: Essentially a breaking change as `exports` package.json property prevents direct import from files in dist

see: https://redfin.engineering/node-modules-at-war-why-commonjs-and-es-modules-cant-get-along-9617135eeca1
  • Loading branch information
nonara authored and Ron S committed Oct 10, 2021
1 parent 2b65583 commit 0d4b922
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 5 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
test/
dist/
node_modules/
esm/
11 changes: 11 additions & 0 deletions esm/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import nhp from '../dist/index.js'

export const CommentNode = nhp.CommentNode;
export const HTMLElement = nhp.HTMLElement;
export const parse = nhp.parse;
export const valid = nhp.valid;
export const Node = nhp.Node;
export const TextNode = nhp.TextNode;
export const NodeType = nhp.NodeType;

export default nhp;
3 changes: 3 additions & 0 deletions esm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@
"version": "4.1.5",
"description": "A very fast HTML parser, generating a simplified DOM, with basic element query support.",
"main": "dist/index.js",
"module": "dist/esm/index.js",
"types": "dist/index.d.ts",
"scripts": {
"test": "mocha",
"lint": "eslint ./src/*.ts ./src/**/*.ts",
"clean": "del-cli ./dist/",
"ts:cjs": "tsc -m commonjs",
"ts:amd": "tsc -t es5 -m amd -d false --outFile ./dist/main.js",
"ts:esm": "tsc -t es2019 -m esnext -d false --outDir ./dist/esm/",
"build": "npm run lint && npm run clean && npm run ts:cjs && npm run ts:amd && npm run ts:esm",
"build": "npm run lint && npm run clean && npm run ts:cjs && npm run ts:amd",
"dev": "tsc -w & mocha -w ./test/*.js",
"pretest": "tsc -m commonjs",
"pretest": "tsc -m commonjs && cd test/assets && yarn install",
"release": "yarn build && np",
"prepare": "npm run build"
},
Expand Down Expand Up @@ -60,6 +58,7 @@
"should": "latest",
"spec": "latest",
"travis-cov": "latest",
"ts-node": "^10.2.1",
"typescript": "next"
},
"config": {
Expand All @@ -84,5 +83,9 @@
"url": "https://github.com/taoqf/node-fast-html-parser/issues"
},
"homepage": "https://github.com/taoqf/node-fast-html-parser",
"sideEffects": false
"sideEffects": false,
"exports": {
"require": "./dist/index.js",
"import": "./esm/index.js"
}
}
4 changes: 4 additions & 0 deletions test/assets/cjs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { parse } from 'node-html-parser'

const res = parse('<a href="#">parse succeeded</a>');
console.log(res.firstChild.text);
8 changes: 8 additions & 0 deletions test/assets/cjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "cjs-test",
"private": true,
"type": "commonjs",
"peerDependencies": {
"node-html-parser": "*"
}
}
9 changes: 9 additions & 0 deletions test/assets/cjs/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"files": [ "index.ts" ],
"extends": "../tsconfig.base.json",
"compilerOptions": {
"noEmit": true,
"module": "CommonJS",
"moduleResolution": "node"
}
}
4 changes: 4 additions & 0 deletions test/assets/esm/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { parse } from 'node-html-parser'

const res = parse('<a href="#">parse succeeded</a>');
console.log(res.firstChild.text);
8 changes: 8 additions & 0 deletions test/assets/esm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "esm-test",
"private": true,
"type": "module",
"peerDependencies": {
"node-html-parser": "*"
}
}
9 changes: 9 additions & 0 deletions test/assets/esm/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"files": [ "index.ts" ],
"extends": "../tsconfig.base.json",
"compilerOptions": {
"noEmit": true,
"module": "ESNext",
"moduleResolution": "node"
}
}
9 changes: 9 additions & 0 deletions test/assets/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"private": true,
"workspaces": [
"assets/*"
],
"dependencies": {
"node-html-parser": "link:../.."
}
}
7 changes: 7 additions & 0 deletions test/assets/tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"skipDefaultLibCheck": true,
"skipLibCheck": true,
"noLib": true
}
}
20 changes: 20 additions & 0 deletions test/import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const { execSync } = require('child_process');
const path = require('path');

describe(`Module Import`, function () {
this.timeout(20000);

it(`ESM project can import and use named exports`, () => {
execSync('node --loader ts-node/esm index.ts', {
cwd: path.resolve(__dirname, 'assets/esm'),
stdio: "pipe"
}).toString().should.eql('parse succeeded\n')
});

it(`CommonJS project can import and use named exports`, () => {
execSync('node -r ts-node/register index.ts', {
cwd: path.resolve(__dirname, 'assets/cjs'),
stdio: "pipe"
}).toString().should.eql('parse succeeded\n')
});
});

0 comments on commit 0d4b922

Please sign in to comment.