Skip to content

Commit

Permalink
Refactor to improve bundle size
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Dec 27, 2020
1 parent 347bc6f commit 8f135d0
Showing 1 changed file with 15 additions and 32 deletions.
47 changes: 15 additions & 32 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,13 @@ function pipelineRun(p, ctx, next) {

function pipelineStringify(p, ctx) {
var result = p.stringify(ctx.tree, ctx.file)
var file = ctx.file

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

Expand All @@ -55,8 +54,8 @@ function unified() {
var attachers = []
var transformers = trough()
var namespace = {}
var frozen = false
var freezeIndex = -1
var frozen

// Data management.
processor.data = data
Expand All @@ -82,10 +81,9 @@ function unified() {
// Create a new processor based on the processor in the current scope.
function processor() {
var destination = unified()
var length = attachers.length
var index = -1

while (++index < length) {
while (++index < attachers.length) {
destination.use.apply(null, attachers[index])
}

Expand All @@ -103,8 +101,6 @@ function unified() {
// In essence, always invoke this when exporting a processor.
function freeze() {
var values
var plugin
var options
var transformer

if (frozen) {
Expand All @@ -113,19 +109,16 @@ function unified() {

while (++freezeIndex < attachers.length) {
values = attachers[freezeIndex]
plugin = values[0]
options = values[1]
transformer = null

if (options === false) {
if (values[1] === false) {
continue
}

if (options === true) {
if (values[1] === true) {
values[1] = undefined
}

transformer = plugin.apply(processor, values.slice(1))
transformer = values[0].apply(processor, values.slice(1))

if (typeof transformer === 'function') {
transformers.use(transformer)
Expand All @@ -145,9 +138,7 @@ function unified() {
// Set `key`.
if (arguments.length === 2) {
assertUnfrozen('data', frozen)

namespace[key] = value

return processor
}

Expand Down Expand Up @@ -221,16 +212,12 @@ function unified() {
}

function addList(plugins) {
var length
var index
var index = -1

if (plugins === null || plugins === undefined) {
// Empty.
} else if (typeof plugins === 'object' && 'length' in plugins) {
length = plugins.length
index = -1

while (++index < length) {
while (++index < plugins.length) {
add(plugins[index])
}
} else {
Expand All @@ -254,15 +241,11 @@ function unified() {
}

function find(plugin) {
var length = attachers.length
var index = -1
var entry

while (++index < length) {
entry = attachers[index]

if (entry[0] === plugin) {
return entry
while (++index < attachers.length) {
if (attachers[index][0] === plugin) {
return attachers[index]
}
}
}
Expand Down Expand Up @@ -320,8 +303,8 @@ function unified() {
// Run transforms on a unist node representation of a file (in string or
// vfile representation), sync.
function runSync(node, file) {
var complete = false
var result
var complete

run(node, file, done)

Expand All @@ -331,8 +314,8 @@ function unified() {

function done(err, tree) {
complete = true
bail(err)
result = tree
bail(err)
}
}

Expand Down Expand Up @@ -388,8 +371,8 @@ function unified() {

// Process the given document (in string or vfile representation), sync.
function processSync(doc) {
var complete = false
var file
var complete

freeze()
assertParser('processSync', processor.Parser)
Expand Down

0 comments on commit 8f135d0

Please sign in to comment.