Skip to content

Commit c961af8

Browse files
committedOct 22, 2023
Remove useDynamicImport option, now default
1 parent 1863914 commit c961af8

File tree

5 files changed

+30
-169
lines changed

5 files changed

+30
-169
lines changed
 

Diff for: ‎docs/_asset/editor.jsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,7 @@ function Playground() {
199199
: 'program',
200200
recmaPlugins,
201201
rehypePlugins,
202-
remarkPlugins,
203-
useDynamicImport: true
202+
remarkPlugins
204203
})
205204

206205
if (show === 'result') {

Diff for: ‎packages/mdx/lib/core.js

+2-10
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,6 @@
127127
* @property {boolean | null | undefined} [tableCellAlignToStyle=true]
128128
* Turn obsolete `align` props on `td` and `th` into CSS `style` props
129129
* (default: `true`).
130-
* @property {boolean | null | undefined} [useDynamicImport=false]
131-
* whether to compile to dynamic import expressions when `outputFormat` is
132-
* `'function-body'` (default: `false`);
133-
* so, it will turn import statements (`import {x} from 'y'`) into dynamic
134-
* import expressions (`const {x} = await import('y')`);
135-
* import statements only work at the top level of modules but import
136-
* expressions are available inside function bodies;
137-
* you should probably set `baseUrl` too.
138130
*/
139131

140132
import {unreachable} from 'devlop'
@@ -196,11 +188,11 @@ export function createProcessor(options) {
196188
}
197189

198190
if (
199-
!warned &&
200191
(settings.jsxRuntime === 'classic' ||
201192
settings.pragma ||
202193
settings.pragmaFrag ||
203-
settings.pragmaImportSource)
194+
settings.pragmaImportSource) &&
195+
!warned
204196
) {
205197
warned = true
206198
console.warn(

Diff for: ‎packages/mdx/lib/plugin/recma-document.js

-14
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ import {specifiersToDeclarations} from '../util/estree-util-specifiers-to-declar
4848
export function recmaDocument(options) {
4949
const baseUrl_ = options.baseUrl || undefined
5050
const baseUrl = typeof baseUrl_ === 'object' ? baseUrl_.href : baseUrl_
51-
const useDynamicImport = options.useDynamicImport || undefined
5251
const outputFormat = options.outputFormat || 'program'
5352
const pragma =
5453
options.pragma === undefined ? 'React.createElement' : options.pragma
@@ -419,19 +418,6 @@ export function recmaDocument(options) {
419418
// Source optional:
420419
(node.type === 'ExportNamedDeclaration' && node.source)
421420
) {
422-
if (!useDynamicImport) {
423-
file.fail(
424-
'Unexpected `import` or `export … from` in `evaluate` (outputting a function body) by default: please set `useDynamicImport: true` (and probably specify a `baseUrl`)',
425-
{
426-
// Results of this function end up in `tree` again.
427-
ancestors: [tree, node],
428-
place: positionFromEstree(node),
429-
ruleId: 'invalid-esm-statement',
430-
source: 'recma-document'
431-
}
432-
)
433-
}
434-
435421
// We always have a source, but types say they can be missing.
436422
assert(node.source, 'expected `node.source` to be defined')
437423

Diff for: ‎packages/mdx/readme.md

-47
Original file line numberDiff line numberDiff line change
@@ -764,8 +764,6 @@ Configuration for `createProcessor` (TypeScript type).
764764
The `'function-body'` format will get the runtime (and optionally provider) from
765765
`arguments[0]`, rewrite export statements, and use a return statement to yield
766766
what was exported.
767-
Normally, this output format will throw on `import` (and `exportfrom`)
768-
statements, but you can support them by setting `useDynamicImport`.
769767
770768
</details>
771769
* `mdExtensions` (`Array<string>`, default: `['.md', '.markdown', '.mdown',
@@ -957,49 +955,6 @@ Configuration for `createProcessor` (TypeScript type).
957955
for AST nodes generated by this project, this option configures it
958956
* `tableCellAlignToStyle` (`boolean`, default: `true`)
959957
turn obsolete `align` props on `td` and `th` into CSS `style` props
960-
* `useDynamicImport` (`boolean`, default: `false`)
961-
whether to compile to dynamic import expressions when `outputFormat` is
962-
`'function-body'`;
963-
so, it will turn import statements (`import {x} from 'y'`) into dynamic
964-
import expressions (`const {x} = await import('y')`);
965-
import statements only work at the top level of modules but import
966-
expressions are available inside function bodies;
967-
you should probably set `baseUrl` too.
968-
969-
<details><summary>Expand example</summary>
970-
971-
Say we have a couple modules:
972-
973-
```tsx
974-
// meta.js:
975-
export const title = 'World'
976-
977-
// numbers.js:
978-
export const no = 3.14
979-
980-
// example.js:
981-
import {compile} from '@mdx-js/mdx'
982-
983-
const code = `import {name} from './meta.js'
984-
export {no} from './numbers.js'
985-
986-
# hi {name}!`
987-
988-
console.log(String(await compile(code, {outputFormat: 'function-body', useDynamicImport: true})))
989-
```
990-
991-
now running `node example.js` yields:
992-
993-
```tsx
994-
const {Fragment: _Fragment, jsx: _jsx, jsxs: _jsxs} = arguments[0]
995-
const {name} = await import('./meta.js')
996-
const {no} = await import('./numbers.js')
997-
function _createMdxContent(props) { /* … */ }
998-
function MDXContent(props = {}) { /* … */ }
999-
return {no, default: MDXContent}
1000-
```
1001-
1002-
</details>
1003958

1004959
### `RunOptions`
1005960

@@ -1248,8 +1203,6 @@ abide by its terms.
12481203

12491204
[baseurl]: #optionsbaseurl
12501205

1251-
[usedynamicimport]: #optionsusedynamicimport
1252-
12531206
[unified]: https://github.com/unifiedjs/unified
12541207

12551208
[processor]: https://github.com/unifiedjs/unified#processor

Diff for: ‎packages/mdx/test/evaluate.js

+27-96
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,11 @@ test('@mdx-js/mdx: evaluate', async function (t) {
9898
})
9999

100100
await t.test(
101-
'should support an `import` of a relative url w/ `useDynamicImport`',
101+
'should support an `import` of a relative url w/ `baseUrl`',
102102
async function () {
103103
const mod = await evaluate(
104104
'import {number} from "./context/data.js"\n\n{number}',
105-
{baseUrl: import.meta.url, useDynamicImport: true, ...runtime}
105+
{baseUrl: import.meta.url, ...runtime}
106106
)
107107

108108
assert.equal(
@@ -113,13 +113,13 @@ test('@mdx-js/mdx: evaluate', async function (t) {
113113
)
114114

115115
await t.test(
116-
'should support an `import` of a full url w/ `useDynamicImport`',
116+
'should support an `import` of a full url w/ `baseUrl`',
117117
async function () {
118118
const mod = await evaluate(
119119
'import {number} from "' +
120120
new URL('context/data.js', import.meta.url) +
121121
'"\n\n{number}',
122-
{baseUrl: import.meta.url, useDynamicImport: true, ...runtime}
122+
{baseUrl: import.meta.url, ...runtime}
123123
)
124124

125125
assert.equal(
@@ -130,41 +130,33 @@ test('@mdx-js/mdx: evaluate', async function (t) {
130130
)
131131

132132
await t.test(
133-
'should support an `import` w/o specifiers w/ `useDynamicImport`',
133+
'should support an `import` w/o specifiers w/o `baseUrl`',
134134
async function () {
135135
assert.match(
136-
String(
137-
await compile('import "a"', {
138-
outputFormat: 'function-body',
139-
useDynamicImport: true
140-
})
141-
),
136+
String(await compile('import "a"', {outputFormat: 'function-body'})),
142137
/\nawait import\("a"\);?\n/
143138
)
144139
}
145140
)
146141

147142
await t.test(
148-
'should support an `import` w/ 0 specifiers w/ `useDynamicImport`',
143+
'should support an `import` w/ 0 specifiers w/o `baseUrl`',
149144
async function () {
150145
assert.match(
151146
String(
152-
await compile('import {} from "a"', {
153-
outputFormat: 'function-body',
154-
useDynamicImport: true
155-
})
147+
await compile('import {} from "a"', {outputFormat: 'function-body'})
156148
),
157149
/\nawait import\("a"\);?\n/
158150
)
159151
}
160152
)
161153

162154
await t.test(
163-
'should support a namespace import w/ `useDynamicImport`',
155+
'should support a namespace import w/ `baseUrl`',
164156
async function () {
165157
const mod = await evaluate(
166158
'import * as x from "./context/components.js"\n\n<x.Pill>Hi!</x.Pill>',
167-
{baseUrl: import.meta.url, useDynamicImport: true, ...runtime}
159+
{baseUrl: import.meta.url, ...runtime}
168160
)
169161

170162
assert.equal(
@@ -175,11 +167,11 @@ test('@mdx-js/mdx: evaluate', async function (t) {
175167
)
176168

177169
await t.test(
178-
'should support a namespace import and a bare specifier w/ `useDynamicImport`',
170+
'should support a namespace import and a bare specifier w/ `baseUrl`',
179171
async function () {
180172
const mod = await evaluate(
181173
'import Div, * as x from "./context/components.js"\n\n<x.Pill>a</x.Pill> and <Div>b</Div>',
182-
{baseUrl: import.meta.url, useDynamicImport: true, ...runtime}
174+
{baseUrl: import.meta.url, ...runtime}
183175
)
184176

185177
assert.equal(
@@ -247,81 +239,56 @@ test('@mdx-js/mdx: evaluate', async function (t) {
247239
)
248240
})
249241

250-
await t.test('should throw on an `export * from`', async function () {
251-
try {
252-
await evaluate('export {a} from "b"', runtime)
253-
assert.fail()
254-
} catch (error) {
255-
assert.match(
256-
String(error),
257-
/Unexpected `import` or `export from` in `evaluate` \(outputting a function body\) by default/
258-
)
259-
}
260-
})
261-
262242
await t.test(
263-
'should support an `export from` w/ `useDynamicImport`',
243+
'should support an `export from` w/ `baseUrl`',
264244
async function () {
265245
const mod = await evaluate('export {number} from "./context/data.js"', {
266246
baseUrl: import.meta.url,
267-
useDynamicImport: true,
268247
...runtime
269248
})
270249

271250
assert.equal(mod.number, 3.14)
272251
}
273252
)
274253

275-
await t.test(
276-
'should support an `export` w/ `useDynamicImport`',
277-
async function () {
278-
const mod = await evaluate(
279-
'import {number} from "./context/data.js"\nexport {number}',
280-
{baseUrl: import.meta.url, useDynamicImport: true, ...runtime}
281-
)
254+
await t.test('should support an `export` w/ `baseUrl`', async function () {
255+
const mod = await evaluate(
256+
'import {number} from "./context/data.js"\nexport {number}',
257+
{baseUrl: import.meta.url, ...runtime}
258+
)
282259

283-
assert.equal(mod.number, 3.14)
284-
}
285-
)
260+
assert.equal(mod.number, 3.14)
261+
})
286262

287263
await t.test(
288-
'should support an `export as from` w/ `useDynamicImport`',
264+
'should support an `export as from` w/ `baseUrl`',
289265
async function () {
290266
const mod = await evaluate(
291267
'export {number as data} from "./context/data.js"',
292-
{
293-
baseUrl: import.meta.url,
294-
useDynamicImport: true,
295-
...runtime
296-
}
268+
{baseUrl: import.meta.url, ...runtime}
297269
)
298270

299271
assert.equal(mod.data, 3.14)
300272
}
301273
)
302274

303275
await t.test(
304-
'should support an `export default as from` w/ `useDynamicImport`',
276+
'should support an `export default as from` w/ `baseUrl`',
305277
async function () {
306278
const mod = await evaluate(
307279
'export {default as data} from "./context/data.js"',
308-
{
309-
baseUrl: import.meta.url,
310-
useDynamicImport: true,
311-
...runtime
312-
}
280+
{baseUrl: import.meta.url, ...runtime}
313281
)
314282

315283
assert.equal(mod.data, 6.28)
316284
}
317285
)
318286

319287
await t.test(
320-
'should support an `export all from` w/ `useDynamicImport`',
288+
'should support an `export all from` w/ `baseUrl`',
321289
async function () {
322290
const mod = await evaluate('export * from "./context/data.js"', {
323291
baseUrl: import.meta.url,
324-
useDynamicImport: true,
325292
...runtime
326293
})
327294

@@ -333,11 +300,11 @@ test('@mdx-js/mdx: evaluate', async function (t) {
333300
)
334301

335302
await t.test(
336-
'should support an `export all from`, but prefer explicit exports, w/ `useDynamicImport`',
303+
'should support an `export * from`, but prefer explicit exports, w/ `baseUrl`',
337304
async function () {
338305
const mod = await evaluate(
339306
'export {default as number} from "./context/data.js"\nexport * from "./context/data.js"',
340-
{baseUrl: import.meta.url, useDynamicImport: true, ...runtime}
307+
{baseUrl: import.meta.url, ...runtime}
341308
)
342309

343310
// I’m not sure if this makes sense, but it is how Node works.
@@ -372,42 +339,6 @@ test('@mdx-js/mdx: evaluate', async function (t) {
372339
}
373340
)
374341

375-
await t.test('should throw on an export all from', async function () {
376-
try {
377-
await evaluate('export * from "a"', runtime)
378-
assert.fail()
379-
} catch (error) {
380-
assert.match(
381-
String(error),
382-
/Unexpected `import` or `export from` in `evaluate` \(outputting a function body\) by default/
383-
)
384-
}
385-
})
386-
387-
await t.test('should throw on an import', async function () {
388-
try {
389-
await evaluate('import {a} from "b"', runtime)
390-
assert.fail()
391-
} catch (error) {
392-
assert.match(
393-
String(error),
394-
/Unexpected `import` or `export from` in `evaluate` \(outputting a function body\) by default/
395-
)
396-
}
397-
})
398-
399-
await t.test('should throw on an import default', async function () {
400-
try {
401-
await evaluate('import a from "b"', runtime)
402-
assert.fail()
403-
} catch (error) {
404-
assert.match(
405-
String(error),
406-
/Unexpected `import` or `export from` in `evaluate` \(outputting a function body\) by default:/
407-
)
408-
}
409-
})
410-
411342
await t.test('should support a given components', async function () {
412343
const mod = await evaluate('<X/>', runtime)
413344

0 commit comments

Comments
 (0)
Please sign in to comment.