Skip to content

Commit dc46bc5

Browse files
committed
Use ESM and update vfile
* **Breaking**: this updates `vfile`, which importantly renames ~~`vfile.contents`~~ to `vfile.value` * Inconsequential: this updates `trough`, which removes support for Promise-like objects, in favor of only support actual promises. To update, instead of returning an object with a `then` function, return and *actual* promise
1 parent 9bda1fa commit dc46bc5

File tree

19 files changed

+191
-224
lines changed

19 files changed

+191
-224
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-
unified.js
7-
unified.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-
unified.js
3-
unified.min.js
4-
*.json
52
*.md

index.js

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
'use strict'
2-
3-
var bail = require('bail')
4-
var buffer = require('is-buffer')
5-
var extend = require('extend')
6-
var plain = require('is-plain-obj')
7-
var trough = require('trough')
8-
var vfile = require('vfile')
1+
import {bail} from 'bail'
2+
import isBuffer from 'is-buffer'
3+
import extend from 'extend'
4+
import isPlainObj from 'is-plain-obj'
5+
import {trough} from 'trough'
6+
import {VFile} from 'vfile'
97

108
// Expose a frozen processor.
11-
module.exports = unified().freeze()
9+
export const unified = base().freeze()
1210

1311
var slice = [].slice
1412
var own = {}.hasOwnProperty
@@ -42,15 +40,15 @@ function pipelineStringify(p, ctx) {
4240

4341
if (result === undefined || result === null) {
4442
// Empty.
45-
} else if (typeof result === 'string' || buffer(result)) {
46-
ctx.file.contents = result
43+
} else if (typeof result === 'string' || isBuffer(result)) {
44+
ctx.file.value = result
4745
} else {
4846
ctx.file.result = result
4947
}
5048
}
5149

5250
// Function to create the first processor.
53-
function unified() {
51+
function base() {
5452
var attachers = []
5553
var transformers = trough()
5654
var namespace = {}
@@ -80,7 +78,7 @@ function unified() {
8078

8179
// Create a new processor based on the processor in the current scope.
8280
function processor() {
83-
var destination = unified()
81+
var destination = base()
8482
var index = -1
8583

8684
while (++index < attachers.length) {
@@ -172,7 +170,7 @@ function unified() {
172170
if (value === null || value === undefined) {
173171
// Empty.
174172
} else if (typeof value === 'function') {
175-
addPlugin.apply(null, arguments)
173+
addPlugin(...arguments)
176174
} else if (typeof value === 'object') {
177175
if ('length' in value) {
178176
addList(value)
@@ -202,7 +200,7 @@ function unified() {
202200
addPlugin(value)
203201
} else if (typeof value === 'object') {
204202
if ('length' in value) {
205-
addPlugin.apply(null, value)
203+
addPlugin(...value)
206204
} else {
207205
addPreset(value)
208206
}
@@ -229,7 +227,7 @@ function unified() {
229227
var entry = find(plugin)
230228

231229
if (entry) {
232-
if (plain(entry[1]) && plain(value)) {
230+
if (isPlainObj(entry[1]) && isPlainObj(value)) {
233231
value = extend(true, entry[1], value)
234232
}
235233

@@ -355,7 +353,7 @@ function unified() {
355353
function executor(resolve, reject) {
356354
var file = vfile(doc)
357355

358-
pipeline.run(processor, {file: file}, done)
356+
pipeline.run(processor, {file}, done)
359357

360358
function done(error) {
361359
if (error) {
@@ -454,3 +452,7 @@ function assertDone(name, asyncName, complete) {
454452
)
455453
}
456454
}
455+
456+
function vfile(doc) {
457+
return doc instanceof VFile ? doc : new VFile(doc)
458+
}

package.json

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
"Vse Mozhet Byt <vsemozhetbyt@gmail.com>",
3737
"Richard Littauer <richard.littauer@gmail.com>"
3838
],
39+
"sideEffects": false,
40+
"type": "module",
3941
"types": "types/ts3.4/index.d.ts",
4042
"typesVersions": {
4143
">=4.0": {
@@ -47,39 +49,34 @@
4749
"files": [
4850
"types/ts3.4/index.d.ts",
4951
"types/ts4.0/index.d.ts",
50-
"index.js",
51-
"lib"
52+
"index.d.ts",
53+
"index.js"
5254
],
5355
"dependencies": {
54-
"bail": "^1.0.0",
56+
"bail": "^2.0.0",
5557
"extend": "^3.0.0",
5658
"is-buffer": "^2.0.0",
57-
"is-plain-obj": "^2.0.0",
58-
"trough": "^1.0.0",
59-
"vfile": "^4.0.0"
59+
"is-plain-obj": "^4.0.0",
60+
"trough": "^2.0.0",
61+
"vfile": "^5.0.0"
6062
},
6163
"devDependencies": {
62-
"browserify": "^17.0.0",
6364
"c8": "^7.0.0",
6465
"dtslint": "^4.0.0",
6566
"prettier": "^2.0.0",
6667
"remark-cli": "^9.0.0",
6768
"remark-preset-wooorm": "^8.0.0",
6869
"tape": "^5.0.0",
69-
"tinyify": "^3.0.0",
70-
"xo": "^0.38.0"
70+
"xo": "^0.39.0"
7171
},
7272
"scripts": {
7373
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
74-
"build-bundle": "browserify index.js -s unified -o unified.js",
75-
"build-mangle": "browserify index.js -s unified -p tinyify -o unified.min.js",
76-
"build": "npm run build-bundle && npm run build-mangle",
77-
"test-api": "node test",
78-
"test-coverage": "c8 --check-coverage --lines 100 --functions 100 --branches 100 --reporter lcov tape test",
79-
"test-types": "npm run test-types-3.4 && npm run test-types-4.0",
80-
"test-types-3.4": "dtslint types/ts3.4",
81-
"test-types-4.0": "dtslint types/ts4.0",
82-
"test": "npm run format && npm run build && npm run test-coverage && npm run test-types"
74+
"test-api": "node --conditions development test/index.js",
75+
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node --conditions development test/index.js",
76+
"xxx-test-types": "npm run test-types-3.4 && npm run test-types-4.0",
77+
"xxx-test-types-3.4": "dtslint types/ts3.4",
78+
"xxx-test-types-4.0": "dtslint types/ts4.0",
79+
"test": "npm run format && npm run test-coverage"
8380
},
8481
"prettier": {
8582
"tabWidth": 2,
@@ -91,8 +88,9 @@
9188
},
9289
"xo": {
9390
"prettier": true,
94-
"esnext": false,
9591
"rules": {
92+
"no-var": "off",
93+
"prefer-arrow-callback": "off",
9694
"guard-for-in": "off",
9795
"no-unreachable-loop": "off",
9896
"unicorn/prefer-number-properties": "off",
@@ -101,8 +99,7 @@
10199
"unicorn/prefer-type-error": "off"
102100
},
103101
"ignores": [
104-
"types",
105-
"unified.js"
102+
"types/"
106103
]
107104
},
108105
"remarkConfig": {

readme.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ Support this effort and give back by sponsoring on [OpenCollective][collective]!
8080

8181
## Install
8282

83+
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
84+
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
85+
8386
[npm][]:
8487

8588
```sh
@@ -317,6 +320,9 @@ tree and discards the origin tree.
317320

318321
## API
319322

323+
This package exports the following identifiers: `unified`.
324+
There is no default export.
325+
320326
### `processor()`
321327

322328
[*Processor*][processors] describing how to *process* text.
@@ -638,7 +644,7 @@ The returned promise is rejected with a fatal error, or resolved with the
638644
processed [*file*][file].
639645

640646
The parsed, transformed, and compiled value is exposed on
641-
[`file.contents`][vfile-contents] or `file.result` (see notes).
647+
[`file.value`][vfile-value] or `file.result` (see notes).
642648

643649
###### Note
644650

@@ -651,7 +657,7 @@ unified typically compiles by serializing: most [*compiler*][compiler]s return
651657
Some compilers, such as the one configured with [`rehype-react`][rehype-react],
652658
return other values (in this case, a React tree).
653659
If you’re using a compiler that serializes, the result is available at
654-
`file.contents`.
660+
`file.value`.
655661
Otherwise, the result is available at `file.result`.
656662

657663
###### Example
@@ -758,7 +764,7 @@ An error is thrown if asynchronous [*plugin*][plugin]s are configured.
758764
([`VFile`][vfile]) — Processed [*file*][file]
759765

760766
The parsed, transformed, and compiled value is exposed on
761-
[`file.contents`][vfile-contents] or `file.result` (see notes).
767+
[`file.value`][vfile-value] or `file.result` (see notes).
762768

763769
###### Note
764770

@@ -772,7 +778,7 @@ unified typically compiles by serializing: most [*compiler*][compiler]s return
772778
Some compilers, such as the one configured with [`rehype-react`][rehype-react],
773779
return other values (in this case, a React tree).
774780
If you’re using a compiler that serializes, the result is available at
775-
`file.contents`.
781+
`file.value`.
776782
Otherwise, the result is available at `file.result`.
777783

778784
###### Example
@@ -1285,7 +1291,7 @@ work on [`ware`][ware], as it was a huge initial inspiration.
12851291

12861292
[vfile]: https://github.com/vfile/vfile
12871293

1288-
[vfile-contents]: https://github.com/vfile/vfile#vfilecontents
1294+
[vfile-value]: https://github.com/vfile/vfile#vfilevalue
12891295

12901296
[vfile-utilities]: https://github.com/vfile/vfile#related-tools
12911297

test/async-function.js

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

75
test('async function transformer () {}', function (t) {
8-
var givenFile = vfile('alpha')
6+
var givenFile = new VFile('alpha')
97
var givenNode = {type: 'bravo'}
108
var modifiedNode = {type: 'charlie'}
119

test/core.js

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

64
test('unified()', function (t) {
75
var count

test/data.js

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

64
test('data(key[, value])', function (t) {
75
var processor = unified()

test/freeze.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
'use strict'
2-
3-
var test = require('tape')
4-
var simple = require('./util/simple.js')
5-
var unified = require('..')
1+
import test from 'tape'
2+
import {SimpleCompiler, SimpleParser} from './util/simple.js'
3+
import {unified} from '../index.js'
64

75
test('freeze()', function (t) {
86
var frozen = unified().use(config).freeze()
97
var unfrozen = frozen()
108

119
function config() {
12-
this.Parser = simple.Parser
13-
this.Compiler = simple.Compiler
10+
this.Parser = SimpleParser
11+
this.Compiler = SimpleCompiler
1412
}
1513

1614
t.doesNotThrow(function () {

test/index.js

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,11 @@
1-
'use strict'
2-
31
/* eslint-disable import/no-unassigned-import */
4-
require('./core.js')
5-
require('./freeze.js')
6-
require('./data.js')
7-
require('./use.js')
8-
require('./parse.js')
9-
require('./run.js')
10-
require('./stringify.js')
11-
require('./process.js')
12-
13-
var asyncfunctions = false
14-
15-
try {
16-
eval('typeof async function() {}') // eslint-disable-line no-eval
17-
asyncfunctions = true
18-
} catch (_) {}
19-
20-
console.log('asyncfunctions:', asyncfunctions)
21-
if (asyncfunctions) {
22-
require('./async-function.js')
23-
}
24-
2+
import './core.js'
3+
import './freeze.js'
4+
import './data.js'
5+
import './use.js'
6+
import './parse.js'
7+
import './run.js'
8+
import './stringify.js'
9+
import './process.js'
10+
import './async-function.js'
2511
/* eslint-enable import/no-unassigned-import */

0 commit comments

Comments
 (0)