Skip to content

Commit

Permalink
Use ESM and update vfile
Browse files Browse the repository at this point in the history
*   **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
  • Loading branch information
wooorm committed Jun 25, 2021
1 parent 9bda1fa commit dc46bc5
Show file tree
Hide file tree
Showing 19 changed files with 191 additions and 224 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
.DS_Store
*.log
.nyc_output/
coverage/
node_modules/
unified.js
unified.min.js
yarn.lock
3 changes: 0 additions & 3 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
coverage/
unified.js
unified.min.js
*.json
*.md
36 changes: 19 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
'use strict'

var bail = require('bail')
var buffer = require('is-buffer')
var extend = require('extend')
var plain = require('is-plain-obj')
var trough = require('trough')
var vfile = require('vfile')
import {bail} from 'bail'
import isBuffer from 'is-buffer'
import extend from 'extend'
import isPlainObj from 'is-plain-obj'
import {trough} from 'trough'
import {VFile} from 'vfile'

// Expose a frozen processor.
module.exports = unified().freeze()
export const unified = base().freeze()

var slice = [].slice
var own = {}.hasOwnProperty
Expand Down Expand Up @@ -42,15 +40,15 @@ function pipelineStringify(p, ctx) {

if (result === undefined || result === null) {
// Empty.
} else if (typeof result === 'string' || buffer(result)) {
ctx.file.contents = result
} else if (typeof result === 'string' || isBuffer(result)) {
ctx.file.value = result
} else {
ctx.file.result = result
}
}

// Function to create the first processor.
function unified() {
function base() {
var attachers = []
var transformers = trough()
var namespace = {}
Expand Down Expand Up @@ -80,7 +78,7 @@ function unified() {

// Create a new processor based on the processor in the current scope.
function processor() {
var destination = unified()
var destination = base()
var index = -1

while (++index < attachers.length) {
Expand Down Expand Up @@ -172,7 +170,7 @@ function unified() {
if (value === null || value === undefined) {
// Empty.
} else if (typeof value === 'function') {
addPlugin.apply(null, arguments)
addPlugin(...arguments)
} else if (typeof value === 'object') {
if ('length' in value) {
addList(value)
Expand Down Expand Up @@ -202,7 +200,7 @@ function unified() {
addPlugin(value)
} else if (typeof value === 'object') {
if ('length' in value) {
addPlugin.apply(null, value)
addPlugin(...value)
} else {
addPreset(value)
}
Expand All @@ -229,7 +227,7 @@ function unified() {
var entry = find(plugin)

if (entry) {
if (plain(entry[1]) && plain(value)) {
if (isPlainObj(entry[1]) && isPlainObj(value)) {
value = extend(true, entry[1], value)
}

Expand Down Expand Up @@ -355,7 +353,7 @@ function unified() {
function executor(resolve, reject) {
var file = vfile(doc)

pipeline.run(processor, {file: file}, done)
pipeline.run(processor, {file}, done)

function done(error) {
if (error) {
Expand Down Expand Up @@ -454,3 +452,7 @@ function assertDone(name, asyncName, complete) {
)
}
}

function vfile(doc) {
return doc instanceof VFile ? doc : new VFile(doc)
}
39 changes: 18 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
"Vse Mozhet Byt <vsemozhetbyt@gmail.com>",
"Richard Littauer <richard.littauer@gmail.com>"
],
"sideEffects": false,
"type": "module",
"types": "types/ts3.4/index.d.ts",
"typesVersions": {
">=4.0": {
Expand All @@ -47,39 +49,34 @@
"files": [
"types/ts3.4/index.d.ts",
"types/ts4.0/index.d.ts",
"index.js",
"lib"
"index.d.ts",
"index.js"
],
"dependencies": {
"bail": "^1.0.0",
"bail": "^2.0.0",
"extend": "^3.0.0",
"is-buffer": "^2.0.0",
"is-plain-obj": "^2.0.0",
"trough": "^1.0.0",
"vfile": "^4.0.0"
"is-plain-obj": "^4.0.0",
"trough": "^2.0.0",
"vfile": "^5.0.0"
},
"devDependencies": {
"browserify": "^17.0.0",
"c8": "^7.0.0",
"dtslint": "^4.0.0",
"prettier": "^2.0.0",
"remark-cli": "^9.0.0",
"remark-preset-wooorm": "^8.0.0",
"tape": "^5.0.0",
"tinyify": "^3.0.0",
"xo": "^0.38.0"
"xo": "^0.39.0"
},
"scripts": {
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"build-bundle": "browserify index.js -s unified -o unified.js",
"build-mangle": "browserify index.js -s unified -p tinyify -o unified.min.js",
"build": "npm run build-bundle && npm run build-mangle",
"test-api": "node test",
"test-coverage": "c8 --check-coverage --lines 100 --functions 100 --branches 100 --reporter lcov tape test",
"test-types": "npm run test-types-3.4 && npm run test-types-4.0",
"test-types-3.4": "dtslint types/ts3.4",
"test-types-4.0": "dtslint types/ts4.0",
"test": "npm run format && npm run build && npm run test-coverage && npm run test-types"
"test-api": "node --conditions development test/index.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node --conditions development test/index.js",
"xxx-test-types": "npm run test-types-3.4 && npm run test-types-4.0",
"xxx-test-types-3.4": "dtslint types/ts3.4",
"xxx-test-types-4.0": "dtslint types/ts4.0",
"test": "npm run format && npm run test-coverage"
},
"prettier": {
"tabWidth": 2,
Expand All @@ -91,8 +88,9 @@
},
"xo": {
"prettier": true,
"esnext": false,
"rules": {
"no-var": "off",
"prefer-arrow-callback": "off",
"guard-for-in": "off",
"no-unreachable-loop": "off",
"unicorn/prefer-number-properties": "off",
Expand All @@ -101,8 +99,7 @@
"unicorn/prefer-type-error": "off"
},
"ignores": [
"types",
"unified.js"
"types/"
]
},
"remarkConfig": {
Expand Down
16 changes: 11 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ Support this effort and give back by sponsoring on [OpenCollective][collective]!

## Install

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

[npm][]:

```sh
Expand Down Expand Up @@ -317,6 +320,9 @@ tree and discards the origin tree.

## API

This package exports the following identifiers: `unified`.
There is no default export.

### `processor()`

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

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

###### Note

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

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

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

###### Note

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

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

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

[vfile-contents]: https://github.com/vfile/vfile#vfilecontents
[vfile-value]: https://github.com/vfile/vfile#vfilevalue

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

Expand Down
10 changes: 4 additions & 6 deletions test/async-function.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
'use strict'

var test = require('tape')
var vfile = require('vfile')
var unified = require('..')
import test from 'tape'
import {VFile} from 'vfile'
import {unified} from '../index.js'

test('async function transformer () {}', function (t) {
var givenFile = vfile('alpha')
var givenFile = new VFile('alpha')
var givenNode = {type: 'bravo'}
var modifiedNode = {type: 'charlie'}

Expand Down
6 changes: 2 additions & 4 deletions test/core.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict'

var test = require('tape')
var unified = require('..')
import test from 'tape'
import {unified} from '../index.js'

test('unified()', function (t) {
var count
Expand Down
6 changes: 2 additions & 4 deletions test/data.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict'

var test = require('tape')
var unified = require('..')
import test from 'tape'
import {unified} from '../index.js'

test('data(key[, value])', function (t) {
var processor = unified()
Expand Down
12 changes: 5 additions & 7 deletions test/freeze.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
'use strict'

var test = require('tape')
var simple = require('./util/simple.js')
var unified = require('..')
import test from 'tape'
import {SimpleCompiler, SimpleParser} from './util/simple.js'
import {unified} from '../index.js'

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

function config() {
this.Parser = simple.Parser
this.Compiler = simple.Compiler
this.Parser = SimpleParser
this.Compiler = SimpleCompiler
}

t.doesNotThrow(function () {
Expand Down
32 changes: 9 additions & 23 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,11 @@
'use strict'

/* eslint-disable import/no-unassigned-import */
require('./core.js')
require('./freeze.js')
require('./data.js')
require('./use.js')
require('./parse.js')
require('./run.js')
require('./stringify.js')
require('./process.js')

var asyncfunctions = false

try {
eval('typeof async function() {}') // eslint-disable-line no-eval
asyncfunctions = true
} catch (_) {}

console.log('asyncfunctions:', asyncfunctions)
if (asyncfunctions) {
require('./async-function.js')
}

import './core.js'
import './freeze.js'
import './data.js'
import './use.js'
import './parse.js'
import './run.js'
import './stringify.js'
import './process.js'
import './async-function.js'
/* eslint-enable import/no-unassigned-import */
6 changes: 2 additions & 4 deletions test/parse.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict'

var test = require('tape')
var unified = require('..')
import test from 'tape'
import {unified} from '../index.js'

test('parse(file)', function (t) {
var processor = unified()
Expand Down
Loading

0 comments on commit dc46bc5

Please sign in to comment.