Skip to content

Commit bd21c56

Browse files
committed
Refactor to improve bundle size
1 parent 4995154 commit bd21c56

File tree

3 files changed

+26
-34
lines changed

3 files changed

+26
-34
lines changed

.babelrc

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
"babel-plugin-inline-constants",
55
{
66
"modules": [
7-
"micromark/lib/character/codes",
8-
"micromark/lib/constant/constants",
9-
"micromark/lib/constant/types"
7+
"micromark/dist/character/codes",
8+
"micromark/dist/constant/constants",
9+
"micromark/dist/constant/types"
1010
]
1111
}
1212
]

lib/index.js

+21-30
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
module.exports = fromMarkdown
44

55
// These three are compiled away in the `dist/`
6-
var codes = require('micromark/lib/character/codes')
7-
var constants = require('micromark/lib/constant/constants')
8-
var types = require('micromark/lib/constant/types')
6+
var codes = require('micromark/dist/character/codes')
7+
var constants = require('micromark/dist/constant/constants')
8+
var types = require('micromark/dist/constant/types')
99

1010
var toString = require('mdast-util-to-string')
1111
var assign = require('micromark/dist/constant/assign')
@@ -147,12 +147,10 @@ function compiler(options) {
147147
function compile(events) {
148148
var stack = [{type: 'root', children: []}]
149149
var tokenStack = []
150-
var index = -1
151150
var listStack = []
152-
var length
151+
var index = -1
153152
var handler
154153
var listStart
155-
var event
156154

157155
var context = {
158156
stack: stack,
@@ -167,15 +165,13 @@ function compiler(options) {
167165
}
168166

169167
while (++index < events.length) {
170-
event = events[index]
171-
172168
// We preprocess lists to add `listItem` tokens, and to infer whether
173169
// items the list itself are spread out.
174170
if (
175-
event[1].type === types.listOrdered ||
176-
event[1].type === types.listUnordered
171+
events[index][1].type === types.listOrdered ||
172+
events[index][1].type === types.listUnordered
177173
) {
178-
if (event[0] === 'enter') {
174+
if (events[index][0] === 'enter') {
179175
listStack.push(index)
180176
} else {
181177
listStart = listStack.pop(index)
@@ -185,9 +181,8 @@ function compiler(options) {
185181
}
186182

187183
index = -1
188-
length = events.length
189184

190-
while (++index < length) {
185+
while (++index < events.length) {
191186
handler = config[events[index][0]]
192187

193188
if (own.call(handler, events[index][1].type)) {
@@ -198,7 +193,7 @@ function compiler(options) {
198193
}
199194
}
200195

201-
if (tokenStack.length > 0) {
196+
if (tokenStack.length) {
202197
throw new Error(
203198
'Cannot close document, a token (`' +
204199
tokenStack[tokenStack.length - 1].type +
@@ -214,10 +209,10 @@ function compiler(options) {
214209
// Figure out `root` position.
215210
stack[0].position = {
216211
start: point(
217-
length ? events[0][1].start : {line: 1, column: 1, offset: 0}
212+
events.length ? events[0][1].start : {line: 1, column: 1, offset: 0}
218213
),
219214
end: point(
220-
length
215+
events.length
221216
? events[events.length - 2][1].end
222217
: {line: 1, column: 1, offset: 0}
223218
)
@@ -430,8 +425,7 @@ function compiler(options) {
430425
}
431426

432427
function resume() {
433-
var value = toString(this.stack.pop())
434-
return value
428+
return toString(this.stack.pop())
435429
}
436430

437431
//
@@ -555,11 +549,10 @@ function compiler(options) {
555549
return
556550
}
557551

558-
if (getData('setextHeadingSlurpLineEnding')) {
559-
return
560-
}
561-
562-
if (config.canContainEols.indexOf(context.type) !== -1) {
552+
if (
553+
!getData('setextHeadingSlurpLineEnding') &&
554+
config.canContainEols.indexOf(context.type) > -1
555+
) {
563556
onenterdata.call(this, token)
564557
onexitdata.call(this, token)
565558
}
@@ -677,6 +670,7 @@ function compiler(options) {
677670
var data = this.sliceSerialize(token)
678671
var type = getData('characterReferenceType')
679672
var value
673+
var tail
680674

681675
if (type) {
682676
value = safeFromInt(
@@ -690,7 +684,7 @@ function compiler(options) {
690684
value = decode(data)
691685
}
692686

693-
var tail = this.stack.pop()
687+
tail = this.stack.pop()
694688
tail.value += value
695689
tail.position.end = point(token.end)
696690
}
@@ -793,10 +787,9 @@ function compiler(options) {
793787
}
794788

795789
function configure(config, extensions) {
796-
var length = extensions.length
797790
var index = -1
798791

799-
while (++index < length) {
792+
while (++index < extensions.length) {
800793
extension(config, extensions[index])
801794
}
802795

@@ -806,16 +799,14 @@ function configure(config, extensions) {
806799
function extension(config, extension) {
807800
var key
808801
var left
809-
var right
810802

811803
for (key in extension) {
812804
left = own.call(config, key) ? config[key] : (config[key] = {})
813-
right = extension[key]
814805

815806
if (key === 'canContainEols') {
816-
config[key] = [].concat(left, right)
807+
config[key] = [].concat(left, extension[key])
817808
} else {
818-
Object.assign(left, right)
809+
Object.assign(left, extension[key])
819810
}
820811
}
821812
}

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
"scripts": {
6464
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
6565
"generate-dist": "babel lib/ --out-dir dist/ --quiet --retain-lines; prettier dist/ --loglevel error --write",
66-
"generate-size": "browserify . -p tinyify -s mdast-util-from-markdown -o mdast-util-from-markdown.min.js; gzip-size mdast-util-from-markdown.min.js",
66+
"generate-size": "browserify . -p tinyify -s mdast-util-from-markdown -o mdast-util-from-markdown.min.js; gzip-size mdast-util-from-markdown.min.js --raw",
6767
"generate": "npm run generate-dist && npm run generate-size",
6868
"test-api": "node test",
6969
"test-coverage": "nyc --reporter lcov tape test/index.js",
@@ -90,6 +90,7 @@
9090
"rules": {
9191
"complexity": "off",
9292
"guard-for-in": "off",
93+
"unicorn/explicit-length-check": "off",
9394
"unicorn/no-fn-reference-in-iterator": "off",
9495
"unicorn/prefer-includes": "off",
9596
"unicorn/prefer-number-properties": "off",

0 commit comments

Comments
 (0)