Skip to content

Commit 0cb2fdc

Browse files
committed
Use ESM
1 parent 0e3981a commit 0cb2fdc

File tree

14 files changed

+73
-113
lines changed

14 files changed

+73
-113
lines changed

.gitignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
.DS_Store
22
*.log
3-
.nyc_output/
43
coverage/
54
node_modules/
6-
hast-util-to-parse5.js
7-
hast-util-to-parse5.min.js
85
yarn.lock

.prettierignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
11
coverage/
2-
hast-util-to-parse5.js
3-
hast-util-to-parse5.min.js
4-
*.json
52
*.md

index.js

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,14 @@
1-
'use strict'
2-
3-
var xtend = require('xtend')
4-
var html = require('property-information/html')
5-
var svg = require('property-information/svg')
6-
var find = require('property-information/find')
7-
var toH = require('hast-to-hyperscript')
8-
var ns = require('web-namespaces')
9-
var zwitch = require('zwitch')
10-
11-
module.exports = toParse5
12-
13-
var one = zwitch('type', {
14-
handlers: {
15-
root: root,
16-
element: element,
17-
text: text,
18-
comment: comment,
19-
doctype: doctype
20-
}
21-
})
1+
import {html, svg, find} from 'property-information'
2+
import {toH} from 'hast-to-hyperscript'
3+
import {webNamespaces} from 'web-namespaces'
4+
import {zwitch} from 'zwitch'
5+
6+
var own = {}.hasOwnProperty
7+
8+
var one = zwitch('type', {handlers: {root, element, text, comment, doctype}})
229

2310
// Transform a tree from hast to Parse5’s AST.
24-
function toParse5(tree, space) {
11+
export function toParse5(tree, space) {
2512
return one(tree, space === 'svg' ? svg : html)
2613
}
2714

@@ -62,7 +49,7 @@ function comment(node, schema) {
6249
}
6350

6451
function element(node, schema) {
65-
return toH(h, xtend(node, {children: []}), {space: schema.space})
52+
return toH(h, Object.assign({}, node, {children: []}), {space: schema.space})
6653

6754
function h(name, attrs) {
6855
var values = []
@@ -73,9 +60,13 @@ function element(node, schema) {
7360
var p5
7461

7562
for (key in attrs) {
63+
if (!own.call(attrs, key) || attrs[key] === false) {
64+
continue
65+
}
66+
7667
info = find(schema, key)
7768

78-
if (attrs[key] === false || (info.boolean && !attrs[key])) {
69+
if (info.boolean && !attrs[key]) {
7970
continue
8071
}
8172

@@ -91,7 +82,7 @@ function element(node, schema) {
9182
value.prefix = key.slice(0, index)
9283
}
9384

94-
value.namespace = ns[info.space]
85+
value.namespace = webNamespaces[info.space]
9586
}
9687

9788
values.push(value)
@@ -115,7 +106,7 @@ function patch(node, p5, parentSchema) {
115106

116107
if (node.type === 'element') {
117108
if (schema.space === 'html' && node.tagName === 'svg') schema = svg
118-
p5.namespaceURI = ns[schema.space]
109+
p5.namespaceURI = webNamespaces[schema.space]
119110
}
120111

121112
if (node.type === 'element' || node.type === 'root') {

package.json

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,36 +24,33 @@
2424
"contributors": [
2525
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
2626
],
27+
"sideEffects": false,
28+
"type": "module",
29+
"main": "index.js",
2730
"files": [
2831
"index.js"
2932
],
3033
"dependencies": {
31-
"hast-to-hyperscript": "^9.0.0",
32-
"property-information": "^5.0.0",
33-
"web-namespaces": "^1.0.0",
34-
"xtend": "^4.0.0",
35-
"zwitch": "^1.0.0"
34+
"hast-to-hyperscript": "^10.0.0",
35+
"property-information": "^6.0.0",
36+
"web-namespaces": "^2.0.0",
37+
"zwitch": "^2.0.0"
3638
},
3739
"devDependencies": {
38-
"browserify": "^17.0.0",
40+
"c8": "^7.0.0",
3941
"json-stringify-safe": "^5.0.0",
40-
"nyc": "^15.0.0",
4142
"parse5": "^6.0.0",
4243
"prettier": "^2.0.0",
4344
"remark-cli": "^9.0.0",
4445
"remark-preset-wooorm": "^8.0.0",
4546
"tape": "^5.0.0",
46-
"tinyify": "^3.0.0",
47-
"xo": "^0.38.0"
47+
"xo": "^0.39.0"
4848
},
4949
"scripts": {
5050
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
51-
"build-bundle": "browserify . -s hastUtilToParse5 -o hast-util-to-parse5.js",
52-
"build-mangle": "browserify . -s hastUtilToParse5 -o hast-util-to-parse5.min.js -p tinyify",
53-
"build": "npm run build-bundle && npm run build-mangle",
54-
"test-api": "node test",
55-
"test-coverage": "nyc --reporter lcov tape test",
56-
"test": "npm run format && npm run build && npm run test-coverage"
51+
"test-api": "node test/index.js",
52+
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test/index.js",
53+
"test": "npm run format && npm run test-coverage"
5754
},
5855
"prettier": {
5956
"tabWidth": 2,
@@ -65,20 +62,10 @@
6562
},
6663
"xo": {
6764
"prettier": true,
68-
"esnext": false,
6965
"rules": {
70-
"unicorn/prefer-includes": "off",
71-
"guard-for-in": "off"
72-
},
73-
"ignores": [
74-
"hast-util-to-parse5.js"
75-
]
76-
},
77-
"nyc": {
78-
"check-coverage": true,
79-
"lines": 100,
80-
"functions": 100,
81-
"branches": 100
66+
"no-var": "off",
67+
"prefer-arrow-callback": "off"
68+
}
8269
},
8370
"remarkConfig": {
8471
"plugins": [

readme.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
1616
## Install
1717

18+
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
19+
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
20+
1821
[npm][]:
1922

2023
```sh
@@ -24,7 +27,7 @@ npm install hast-util-to-parse5
2427
## Use
2528

2629
```js
27-
var toParse5 = require('hast-util-to-parse5')
30+
import {toParse5} from 'hast-util-to-parse5'
2831

2932
var ast = toParse5({
3033
type: 'element',
@@ -48,6 +51,9 @@ Yields:
4851

4952
## API
5053

54+
This package exports the following identifiers: `toParse5`.
55+
There is no default export.
56+
5157
### `toParse5(tree[, space])`
5258

5359
Transform a [**hast**][hast] [*tree*][tree] to [Parse5’s AST][ast].

test/comment.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
'use strict'
2-
3-
var test = require('tape')
4-
var parse5 = require('parse5')
5-
var toParse5 = require('..')
1+
import test from 'tape'
2+
import parse5 from 'parse5'
3+
import {toParse5} from '../index.js'
64

75
test('comment', function (t) {
86
var actual = toParse5({type: 'comment', value: 'Alpha'})

test/doctype.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
'use strict'
2-
3-
var test = require('tape')
4-
var parse5 = require('parse5')
5-
var toParse5 = require('..')
1+
import test from 'tape'
2+
import parse5 from 'parse5'
3+
import {toParse5} from '../index.js'
64

75
test('doctype', function (t) {
86
t.test('should transform a doctype (legacy)', function (st) {

test/element.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use strict'
2-
3-
var test = require('tape')
4-
var parse5 = require('parse5')
5-
var json = require('./json')
6-
var toParse5 = require('..')
1+
import test from 'tape'
2+
import parse5 from 'parse5'
3+
import {json} from './json.js'
4+
import {toParse5} from '../index.js'
75

86
test('element', function (t) {
97
t.test('should transform elements', function (st) {

test/index.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
/* eslint-disable import/no-unassigned-import */
44

5-
require('./root')
6-
require('./doctype')
7-
require('./text')
8-
require('./comment')
9-
require('./element')
10-
require('./position')
11-
require('./svg')
5+
import './root.js'
6+
import './doctype.js'
7+
import './text.js'
8+
import './comment.js'
9+
import './element.js'
10+
import './position.js'
11+
import './svg.js'
1212

1313
/* eslint-enable import/no-unassigned-import */

test/json.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
'use strict'
1+
import stringify from 'json-stringify-safe'
22

3-
var stringify = require('json-stringify-safe')
4-
5-
module.exports = json
6-
7-
function json(value) {
3+
export function json(value) {
84
return JSON.parse(stringify(value))
95
}

test/position.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use strict'
2-
3-
var test = require('tape')
4-
var parse5 = require('parse5')
5-
var json = require('./json')
6-
var toParse5 = require('..')
1+
import test from 'tape'
2+
import parse5 from 'parse5'
3+
import {json} from './json.js'
4+
import {toParse5} from '../index.js'
75

86
test('position', function (t) {
97
var actual = toParse5({

test/root.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use strict'
2-
3-
var test = require('tape')
4-
var parse5 = require('parse5')
5-
var json = require('./json')
6-
var toParse5 = require('..')
1+
import test from 'tape'
2+
import parse5 from 'parse5'
3+
import {json} from './json.js'
4+
import {toParse5} from '../index.js'
75

86
test('root', function (t) {
97
t.test('should transform a root (quirks)', function (st) {

test/svg.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use strict'
2-
3-
var test = require('tape')
4-
var parse5 = require('parse5')
5-
var json = require('./json')
6-
var toParse5 = require('..')
1+
import test from 'tape'
2+
import parse5 from 'parse5'
3+
import {json} from './json.js'
4+
import {toParse5} from '../index.js'
75

86
test('svg', function (t) {
97
t.test('should transform SVG in HTML', function (st) {

test/text.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
'use strict'
2-
3-
var test = require('tape')
4-
var parse5 = require('parse5')
5-
var toParse5 = require('..')
1+
import test from 'tape'
2+
import parse5 from 'parse5'
3+
import {toParse5} from '../index.js'
64

75
test('text', function (t) {
86
var expected = parse5.parseFragment('Alpha').childNodes[0]

0 commit comments

Comments
 (0)