Skip to content

Commit a57fdd1

Browse files
committed
Use ESM
1 parent 4c6aea2 commit a57fdd1

File tree

6 files changed

+43
-52
lines changed

6 files changed

+43
-52
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
.DS_Store
22
*.log
3-
.nyc_output/
43
coverage/
54
node_modules/
65
yarn.lock

.prettierignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
coverage/
2-
*.json
32
*.md

index.js

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
'use strict'
1+
import buffer from 'is-buffer'
2+
import {load} from 'js-yaml'
23

3-
var buffer = require('is-buffer')
4-
var yamlParse = require('js-yaml').load
5-
6-
module.exports = matter
7-
8-
function matter(file, options) {
4+
export function matter(file, options) {
95
var settings = options || {}
106
var strip = settings.strip
117
var yamlOptions = settings.yaml || {}
@@ -15,14 +11,14 @@ function matter(file, options) {
1511
)
1612

1713
if (match) {
18-
file.data.matter = yamlParse(
14+
file.data.matter = load(
1915
match[1],
2016
Object.assign({}, yamlOptions, {filename: file.path})
2117
)
2218

2319
if (strip) {
2420
doc = doc.slice(match[0].length)
25-
file.contents = buffer(file.contents) ? Buffer.from(doc) : doc
21+
file.value = buffer(file.value) ? Buffer.from(doc) : doc
2622
}
2723
} else {
2824
file.data.matter = {}

package.json

+14-21
Original file line numberDiff line numberDiff line change
@@ -20,44 +20,38 @@
2020
"type": "opencollective",
2121
"url": "https://opencollective.com/unified"
2222
},
23-
"types": "types/index.d.ts",
2423
"author": "Titus Wormer <[email protected]> (https://wooorm.com)",
2524
"contributors": [
2625
"Titus Wormer <[email protected]> (https://wooorm.com)"
2726
],
27+
"sideEffects": false,
28+
"type": "module",
2829
"main": "index.js",
30+
"types": "types/index.d.ts",
2931
"files": [
30-
"index.js",
31-
"types/index.d.ts"
32+
"types/index.d.ts",
33+
"index.js"
3234
],
3335
"dependencies": {
3436
"is-buffer": "^2.0.0",
3537
"js-yaml": "^4.0.0"
3638
},
3739
"devDependencies": {
3840
"@types/js-yaml": "^4.0.0",
39-
"dtslint": "^4.0.0",
40-
"nyc": "^15.0.0",
41+
"c8": "^7.0.0",
4142
"prettier": "^2.0.0",
4243
"remark-cli": "^9.0.0",
4344
"remark-preset-wooorm": "^8.0.0",
4445
"tape": "^5.0.0",
45-
"to-vfile": "^6.0.0",
46-
"vfile": "^4.0.0",
47-
"xo": "^0.38.0"
46+
"to-vfile": "^7.0.0",
47+
"vfile": "^5.0.0",
48+
"xo": "^0.39.0"
4849
},
4950
"scripts": {
5051
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
51-
"test-api": "node test",
52-
"test-coverage": "nyc --reporter lcov tape test.js",
53-
"test-types": "dtslint types",
54-
"test": "npm run format && npm run test-coverage && npm run test-types"
55-
},
56-
"nyc": {
57-
"check-coverage": true,
58-
"lines": 100,
59-
"functions": 100,
60-
"branches": 100
52+
"test-api": "node test.js",
53+
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
54+
"test": "npm run format && npm run test-coverage"
6155
},
6256
"prettier": {
6357
"tabWidth": 2,
@@ -69,10 +63,9 @@
6963
},
7064
"xo": {
7165
"prettier": true,
72-
"esnext": false,
7366
"rules": {
74-
"guard-for-in": "off",
75-
"unicorn/prefer-includes": "off"
67+
"no-var": "off",
68+
"prefer-arrow-callback": "off"
7669
},
7770
"ignores": [
7871
"types/"

readme.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ Parse the YAML front matter in a [`vfile`][vfile].
1212

1313
## Install
1414

15+
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
16+
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
17+
1518
[npm][]:
1619

1720
```sh
@@ -32,8 +35,8 @@ title: Hello, world!
3235
And our script, `example.js`, looks like so:
3336

3437
```js
35-
var vfile = require('to-vfile')
36-
var matter = require('vfile-matter')
38+
import {toVFile as vfile} from 'to-vfile'
39+
import {matter} from 'vfile-matter'
3740

3841
var file = vfile.readSync('example.html')
3942

@@ -55,6 +58,9 @@ Now, running our script (`node example`) yields:
5558

5659
## API
5760

61+
This package exports the following identifiers: `matter`.
62+
There is no default export.
63+
5864
### `matter(file[, options])`
5965

6066
Parse the YAML front matter in a [`vfile`][vfile], and add it as

test.js

+16-18
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
'use strict'
1+
import test from 'tape'
2+
import buffer from 'is-buffer'
3+
import {CORE_SCHEMA} from 'js-yaml'
4+
import {toVFile as vfile} from 'to-vfile'
5+
import {matter} from './index.js'
26

3-
var test = require('tape')
4-
var buffer = require('is-buffer')
5-
var vfile = require('to-vfile')
6-
var matter = require('.')
7-
var CORE_SCHEMA = require('js-yaml').CORE_SCHEMA
8-
9-
var yaml = '---\nkey: value\nlist:\n - 1\n - 2\n---'
7+
var someYaml = '---\nkey: value\nlist:\n - 1\n - 2\n---'
108
var doc = 'Here is a document\nMore of the document\nOther lines\n'
11-
var both = yaml + '\n' + doc
9+
var both = someYaml + '\n' + doc
1210

1311
test('vfile-matter', function (t) {
14-
var file = vfile({contents: both})
12+
var file = vfile({value: both})
1513

1614
t.equal(matter(file), file, 'should return the given file')
1715

@@ -21,25 +19,25 @@ test('vfile-matter', function (t) {
2119
'should add data'
2220
)
2321

24-
file = matter(vfile({contents: doc}))
22+
file = matter(vfile({value: doc}))
2523
t.deepEqual(file.data, {matter: {}}, 'should support no matter')
2624

27-
file = matter(vfile({contents: both}), {strip: true})
25+
file = matter(vfile({value: both}), {strip: true})
2826
t.deepEqual(String(file), doc, 'should strip matter')
2927

30-
file = matter(vfile({contents: yaml}), {strip: true})
28+
file = matter(vfile({value: someYaml}), {strip: true})
3129
t.deepEqual(String(file), '', 'should strip matter completely')
3230

33-
file = matter(vfile({contents: doc}), {strip: true})
31+
file = matter(vfile({value: doc}), {strip: true})
3432
t.deepEqual(String(file), doc, 'should support no matter w/ strip')
3533

36-
file = matter(vfile({contents: Buffer.from(both)}), {strip: true})
37-
t.ok(buffer(file.contents), 'should supporting buffers')
34+
file = matter(vfile({value: Buffer.from(both)}), {strip: true})
35+
t.ok(buffer(file.value), 'should supporting buffers')
3836

3937
file = matter(vfile(), {strip: true})
40-
t.ok(file.contents === undefined, 'should supporting empties')
38+
t.ok(file.value === undefined, 'should supporting empties')
4139

42-
file = matter(vfile({contents: '---\ndate: 2021-01-01\n---\n'}), {
40+
file = matter(vfile({value: '---\ndate: 2021-01-01\n---\n'}), {
4341
yaml: {schema: CORE_SCHEMA}
4442
})
4543
t.deepEqual(

0 commit comments

Comments
 (0)