File size: 4,240 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
const { SourceMapGenerator } = require('source-map')
const path = require('path')
const { createHash } = require('crypto')
const markdownExtensions = [
'md',
'markdown',
'mdown',
'mkdn',
'mkd',
'mdwn',
'mkdown',
'ron',
]
const mdx = ['.mdx']
const md = markdownExtensions.map((/** @type {string} */ d) => '.' + d)
const own = {}.hasOwnProperty
const marker = {}
const cache = new WeakMap()
/*
* From next.config.js's mdxRs option, construct an actual option object that mdxRs compiler accepts.
*/
function coereceMdxTransformOptions(options = {}) {
const { mdxType, ...restOptions } = options
let parse = undefined
switch (mdxType) {
case 'gfm':
parse = {
constructs: {
gfmAutolinkLiteral: true,
gfmFootnoteDefinition: true,
gfmLabelStartFootnote: true,
gfmStrikethrough: true,
gfmTable: true,
gfmTaskListItem: true,
},
}
break
case 'commonMark':
default:
parse = { gfmStrikethroughSingleTilde: true, mathTextSingleDollar: true }
break
}
return {
...restOptions,
parse,
}
}
/**
* A webpack loader for mdx-rs. This is largely based on existing @mdx-js/loader,
* replaces internal compilation logic to use mdx-rs instead.
*/
function loader(value, bindings, callback) {
const defaults = this.sourceMap ? { SourceMapGenerator } : {}
const options = this.getOptions()
const config = { ...defaults, ...options }
const hash = getOptionsHash(options)
const compiler = this._compiler || marker
let map = cache.get(compiler)
if (!map) {
map = new Map()
cache.set(compiler, map)
}
let process = map.get(hash)
if (!process) {
process = createFormatAwareProcessors(
bindings,
coereceMdxTransformOptions(config)
).compile
map.set(hash, process)
}
process({ value, path: this.resourcePath }).then(
(code) => {
// TODO: no sourcemap
callback(null, code, null)
},
(error) => {
const fpath = path.relative(this.context, this.resourcePath)
error.message = `${fpath}:${error.name}: ${error.message}`
callback(error)
}
)
}
function getOptionsHash(options) {
const hash = createHash('sha256')
let key
for (key in options) {
if (own.call(options, key)) {
const value = options[key]
if (value !== undefined) {
const valueString = JSON.stringify(value)
hash.update(key + valueString)
}
}
}
return hash.digest('hex').slice(0, 16)
}
function createFormatAwareProcessors(bindings, compileOptions = {}) {
const mdExtensions = compileOptions.mdExtensions || md
const mdxExtensions = compileOptions.mdxExtensions || mdx
let cachedMarkdown
let cachedMdx
return {
extnames:
compileOptions.format === 'md'
? mdExtensions
: compileOptions.format === 'mdx'
? mdxExtensions
: mdExtensions.concat(mdxExtensions),
compile,
}
function compile({ value, path: p }) {
const format =
compileOptions.format === 'md' || compileOptions.format === 'mdx'
? compileOptions.format
: path.extname(p) &&
(compileOptions.mdExtensions || md).includes(path.extname(p))
? 'md'
: 'mdx'
const options = {
parse: compileOptions.parse,
development: compileOptions.development,
providerImportSource: compileOptions.providerImportSource,
jsx: compileOptions.jsx,
jsxRuntime: compileOptions.jsxRuntime,
jsxImportSource: compileOptions.jsxImportSource,
pragma: compileOptions.pragma,
pragmaFrag: compileOptions.pragmaFrag,
pragmaImportSource: compileOptions.pragmaImportSource,
filepath: p,
}
const compileMdx = (input) => bindings.mdx.compile(input, options)
const processor =
format === 'md'
? cachedMarkdown || (cachedMarkdown = compileMdx)
: cachedMdx || (cachedMdx = compileMdx)
return processor(value)
}
}
module.exports = function (code) {
const callback = this.async()
const { loadBindings } = require('next/dist/build/swc')
loadBindings().then((bindings) => {
return loader.call(this, code, bindings, callback)
})
}
|