diff --git a/.eslintignore b/.eslintignore
index 6372315..5dff5c2 100644
--- a/.eslintignore
+++ b/.eslintignore
@@ -1,3 +1,4 @@
test/
dist/
node_modules/
+esm/
\ No newline at end of file
diff --git a/esm/index.js b/esm/index.js
new file mode 100755
index 0000000..46edf2a
--- /dev/null
+++ b/esm/index.js
@@ -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;
\ No newline at end of file
diff --git a/esm/package.json b/esm/package.json
new file mode 100755
index 0000000..aead43d
--- /dev/null
+++ b/esm/package.json
@@ -0,0 +1,3 @@
+{
+ "type": "module"
+}
\ No newline at end of file
diff --git a/package.json b/package.json
index 23e7e52..caee529 100644
--- a/package.json
+++ b/package.json
@@ -3,7 +3,6 @@
"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",
@@ -11,10 +10,9 @@
"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"
},
@@ -60,6 +58,7 @@
"should": "latest",
"spec": "latest",
"travis-cov": "latest",
+ "ts-node": "^10.2.1",
"typescript": "next"
},
"config": {
@@ -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"
+ }
}
diff --git a/test/assets/cjs/index.ts b/test/assets/cjs/index.ts
new file mode 100755
index 0000000..eaca320
--- /dev/null
+++ b/test/assets/cjs/index.ts
@@ -0,0 +1,4 @@
+import { parse } from 'node-html-parser'
+
+const res = parse('parse succeeded');
+console.log(res.firstChild.text);
\ No newline at end of file
diff --git a/test/assets/cjs/package.json b/test/assets/cjs/package.json
new file mode 100755
index 0000000..59937a6
--- /dev/null
+++ b/test/assets/cjs/package.json
@@ -0,0 +1,8 @@
+{
+ "name": "cjs-test",
+ "private": true,
+ "type": "commonjs",
+ "peerDependencies": {
+ "node-html-parser": "*"
+ }
+}
diff --git a/test/assets/cjs/tsconfig.json b/test/assets/cjs/tsconfig.json
new file mode 100755
index 0000000..4bfd375
--- /dev/null
+++ b/test/assets/cjs/tsconfig.json
@@ -0,0 +1,9 @@
+{
+ "files": [ "index.ts" ],
+ "extends": "../tsconfig.base.json",
+ "compilerOptions": {
+ "noEmit": true,
+ "module": "CommonJS",
+ "moduleResolution": "node"
+ }
+}
\ No newline at end of file
diff --git a/test/assets/esm/index.ts b/test/assets/esm/index.ts
new file mode 100755
index 0000000..eaca320
--- /dev/null
+++ b/test/assets/esm/index.ts
@@ -0,0 +1,4 @@
+import { parse } from 'node-html-parser'
+
+const res = parse('parse succeeded');
+console.log(res.firstChild.text);
\ No newline at end of file
diff --git a/test/assets/esm/package.json b/test/assets/esm/package.json
new file mode 100755
index 0000000..d1878b7
--- /dev/null
+++ b/test/assets/esm/package.json
@@ -0,0 +1,8 @@
+{
+ "name": "esm-test",
+ "private": true,
+ "type": "module",
+ "peerDependencies": {
+ "node-html-parser": "*"
+ }
+}
diff --git a/test/assets/esm/tsconfig.json b/test/assets/esm/tsconfig.json
new file mode 100755
index 0000000..a979782
--- /dev/null
+++ b/test/assets/esm/tsconfig.json
@@ -0,0 +1,9 @@
+{
+ "files": [ "index.ts" ],
+ "extends": "../tsconfig.base.json",
+ "compilerOptions": {
+ "noEmit": true,
+ "module": "ESNext",
+ "moduleResolution": "node"
+ }
+}
\ No newline at end of file
diff --git a/test/assets/package.json b/test/assets/package.json
new file mode 100755
index 0000000..a421db9
--- /dev/null
+++ b/test/assets/package.json
@@ -0,0 +1,9 @@
+{
+ "private": true,
+ "workspaces": [
+ "assets/*"
+ ],
+ "dependencies": {
+ "node-html-parser": "link:../.."
+ }
+}
\ No newline at end of file
diff --git a/test/assets/tsconfig.base.json b/test/assets/tsconfig.base.json
new file mode 100755
index 0000000..c82fd8f
--- /dev/null
+++ b/test/assets/tsconfig.base.json
@@ -0,0 +1,7 @@
+{
+ "compilerOptions": {
+ "skipDefaultLibCheck": true,
+ "skipLibCheck": true,
+ "noLib": true
+ }
+}
\ No newline at end of file
diff --git a/test/import.js b/test/import.js
new file mode 100755
index 0000000..754bad4
--- /dev/null
+++ b/test/import.js
@@ -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')
+ });
+});
\ No newline at end of file