1
1
/**
2
- * @typedef {import('@mdx-js/mdx').ProcessorOptions } ProcessorOptions
2
+ * @typedef {import('@mdx-js/mdx').CompileOptions } CompileOptions
3
3
* @typedef {import('esbuild').Message } Message
4
4
* @typedef {import('esbuild').OnLoadArgs } OnLoadArgs
5
5
* @typedef {import('esbuild').OnLoadResult } OnLoadResult
10
10
*/
11
11
12
12
/**
13
- * @typedef EsbuildOptions
14
- * Extra options.
15
- * @property {boolean | null | undefined } [allowDangerousRemoteMdx=false]
16
- * Whether to allow importing from `http:` and `https:` URLs (`boolean`,
17
- * default: `false`).
18
- *
19
- * When passing `allowDangerousRemoteMdx`, MD(X) *and* JS files can be
20
- * imported from `http:` and `https:` urls.
21
- *
22
13
* @typedef {Omit<OnLoadArgs, 'pluginData'> & LoadDataFields } LoadData
23
14
* Data passed to `onload`.
24
15
*
27
18
* @property {PluginData | null | undefined } [pluginData]
28
19
* Plugin data.
29
20
*
30
- * @typedef {EsbuildOptions & ProcessorOptions } Options
21
+ * @typedef {CompileOptions } Options
31
22
* Configuration.
32
23
*
33
- * Options are the same as `compile` from `@mdx-js/mdx` with the addition
34
- * of `allowDangerousRemoteMdx`.
35
- *
36
- * ###### Notes
37
- *
38
- * > ⚠️ **Security**: `allowDangerousRemoteMdx` (intentionally) enabled remote
39
- * > code execution.
40
- * > Make sure you trust your code!
41
- * > See [§ Security][security] for more
42
- * > info.
43
- *
44
- * > 💡 **Experiment**: `allowDangerousRemoteMdx` is an experimental feature
45
- * > that might not work well and might change in minor releases.
24
+ * Options are the same as `compile` from `@mdx-js/mdx`.
46
25
*
47
26
* @typedef PluginData
48
27
* Extra data passed.
62
41
import assert from 'node:assert'
63
42
import fs from 'node:fs/promises'
64
43
import path from 'node:path'
65
- import process from 'node:process'
66
44
import { createFormatAwareProcessors } from '@mdx-js/mdx/internal-create-format-aware-processors'
67
45
import { extnamesToRegex } from '@mdx-js/mdx/internal-extnames-to-regex'
68
- import { fetch } from 'undici'
69
46
import { VFile } from 'vfile'
70
47
import { VFileMessage } from 'vfile-message'
71
48
72
49
const eol = / \r \n | \r | \n | \u2028 | \u2029 / g
73
50
74
- /** @type {Map<string, string> } */
75
- const cache = new Map ( )
76
51
const name = '@mdx-js/esbuild'
77
- const p = process
78
- const remoteNamespace = name + '-remote'
79
52
80
53
/**
81
54
* Create an esbuild plugin to compile MDX to JS.
@@ -92,8 +65,7 @@ const remoteNamespace = name + '-remote'
92
65
* Plugin.
93
66
*/
94
67
export function esbuild ( options ) {
95
- const { allowDangerousRemoteMdx, ...rest } = options || { }
96
- const { extnames, process} = createFormatAwareProcessors ( rest )
68
+ const { extnames, process} = createFormatAwareProcessors ( options || { } )
97
69
98
70
return { name, setup}
99
71
@@ -104,85 +76,7 @@ export function esbuild(options) {
104
76
* Nothing.
105
77
*/
106
78
function setup ( build ) {
107
- const filter = extnamesToRegex ( extnames )
108
- const filterHttp = new RegExp ( '^https?:\\/{2}.+' + filter . source )
109
- const http = / ^ h t t p s ? : \/ { 2 } /
110
- const filterHttpOrRelative = / ^ ( h t t p s ? : \/ { 2 } | .{ 1 , 2 } \/ ) .* /
111
-
112
- if ( allowDangerousRemoteMdx ) {
113
- // Intercept import paths starting with "http:" and "https:" so
114
- // esbuild doesn't attempt to map them to a file system location.
115
- // Tag them with the "http-url" namespace to associate them with
116
- // this plugin.
117
- build . onResolve (
118
- { filter : filterHttp , namespace : 'file' } ,
119
- resolveRemoteInLocal
120
- )
121
-
122
- build . onResolve (
123
- { filter : filterHttpOrRelative , namespace : remoteNamespace } ,
124
- resolveInRemote
125
- )
126
- }
127
-
128
- build . onLoad ( { filter : / .* / , namespace : remoteNamespace } , onloadremote )
129
- build . onLoad ( { filter} , onload )
130
-
131
- /** @param {OnResolveArgs } args */
132
- function resolveRemoteInLocal ( args ) {
133
- return { namespace : remoteNamespace , path : args . path }
134
- }
135
-
136
- // Intercept all import paths inside downloaded files and resolve them against
137
- // the original URL. All of these
138
- // files will be in the "http-url" namespace. Make sure to keep
139
- // the newly resolved URL in the "http-url" namespace so imports
140
- // inside it will also be resolved as URLs recursively.
141
- /** @param {OnResolveArgs } args */
142
- function resolveInRemote ( args ) {
143
- return {
144
- namespace : remoteNamespace ,
145
- path : String ( new URL ( args . path , args . importer ) )
146
- }
147
- }
148
-
149
- /**
150
- * @param {OnLoadArgs } data
151
- * Data.
152
- * @returns {Promise<OnLoadResult> }
153
- * Result.
154
- */
155
- async function onloadremote ( data ) {
156
- const href = data . path
157
- console . log ( '%s: downloading `%s`' , remoteNamespace , href )
158
-
159
- /** @type {string } */
160
- let contents
161
-
162
- const cachedContents = cache . get ( href )
163
- if ( cachedContents ) {
164
- contents = cachedContents
165
- } else {
166
- const response = await fetch ( href )
167
- contents = await response . text ( )
168
- cache . set ( href , contents )
169
- }
170
-
171
- if ( filter . test ( href ) ) {
172
- // Clean search and hash from URL.
173
- const url = new URL ( href )
174
- url . hash = ''
175
- url . search = ''
176
- return onload ( {
177
- namespace : 'file' ,
178
- path : url . href ,
179
- pluginData : { contents} ,
180
- suffix : ''
181
- } )
182
- }
183
-
184
- return { contents, loader : 'js' , resolveDir : p . cwd ( ) }
185
- }
79
+ build . onLoad ( { filter : extnamesToRegex ( extnames ) } , onload )
186
80
187
81
/**
188
82
* @param {LoadData } data
@@ -240,9 +134,7 @@ export function esbuild(options) {
240
134
return {
241
135
contents : value || '' ,
242
136
errors,
243
- resolveDir : http . test ( file . path )
244
- ? p . cwd ( )
245
- : path . resolve ( file . cwd , file . dirname ) ,
137
+ resolveDir : path . resolve ( file . cwd , file . dirname ) ,
246
138
warnings
247
139
}
248
140
}
1 commit comments
vercel[bot] commentedon Oct 23, 2023
Successfully deployed to the following URLs:
mdx – ./
mdxjs.com
mdx-mdx.vercel.app
v2.mdxjs.com
mdx-git-main-mdx.vercel.app