Skip to content

Commit 723a8ca

Browse files
authored
refactor(mdx-loader): make loader setup synchronous (AI-assisted) (facebook#12461)
1 parent 44ad065 commit 723a8ca

11 files changed

Lines changed: 204 additions & 205 deletions

File tree

packages/docusaurus-mdx-loader/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"remark-emoji": "^5.0.2",
3636
"remark-frontmatter": "^5.0.0",
3737
"remark-gfm": "^4.0.1",
38-
"stringify-object": "^6.0.0",
38+
"stringify-object": "^7.0.0",
3939
"tslib": "^2.8.1",
4040
"unified": "^11.0.5",
4141
"unist-util-visit": "^5.1.0",
@@ -48,7 +48,6 @@
4848
"@types/escape-html": "^1.0.4",
4949
"@types/estree": "^1.0.9",
5050
"@types/mdast": "^4.0.4",
51-
"@types/stringify-object": "^3.3.1",
5251
"@types/unist": "^3.0.3",
5352
"lodash": "^4.18.1",
5453
"mdast-util-directive": "3.1.0",

packages/docusaurus-mdx-loader/src/__tests__/processor.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,32 @@ describe('MDX processor', () => {
6969
const result = await processContent('## Heading', options);
7070
expect(result.content).toMatchSnapshot();
7171
});
72+
73+
it('reuses the processor when compiling files concurrently', async () => {
74+
let pluginInitializations = 0;
75+
const asyncPlugin: Plugin<[], Root> = () => {
76+
pluginInitializations += 1;
77+
return async (_tree, file) => {
78+
await new Promise<void>((resolve) => setImmediate(resolve));
79+
file.data.asyncPluginContent = file.toString();
80+
};
81+
};
82+
const options = createOptions({
83+
markdownConfig: {format},
84+
remarkPlugins: [asyncPlugin],
85+
});
86+
87+
const results = await Promise.all([
88+
processContent('First document', options),
89+
processContent('Second document', options),
90+
]);
91+
92+
expect(pluginInitializations).toBe(1);
93+
expect(results.map((result) => result.data.asyncPluginContent)).toEqual([
94+
'First document',
95+
'Second document',
96+
]);
97+
});
7298
});
7399

74100
it.each([true, false])('supports emoji=%s', async (emoji) => {

packages/docusaurus-mdx-loader/src/createMDXLoader.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ type CreateOptions = {
1313
useCrossCompilerCache?: boolean;
1414
};
1515

16-
async function normalizeOptions(
17-
optionsInput: Options & CreateOptions,
18-
): Promise<Options> {
16+
function normalizeOptions(optionsInput: Options & CreateOptions): Options {
1917
// Skip eager processor creation in tests
2018
if (process.env.NODE_ENV === 'test' || process.env.VITEST) {
2119
return optionsInput;
@@ -26,7 +24,7 @@ async function normalizeOptions(
2624
// We create the processor earlier here, to avoid the lazy processor creating
2725
// Lazy creation messes-up with Rsdoctor ability to measure mdx-loader perf
2826
if (!options.processors) {
29-
options = {...options, processors: await createProcessors({options})};
27+
options = {...options, processors: createProcessors({options})};
3028
}
3129

3230
// Cross-compiler cache permits to compile client/server MDX only once
@@ -43,25 +41,25 @@ async function normalizeOptions(
4341
return options;
4442
}
4543

46-
export async function createMDXLoaderItem(
44+
export function createMDXLoaderItem(
4745
options: Options & CreateOptions,
48-
): Promise<RuleSetUseItem> {
46+
): RuleSetUseItem {
4947
return {
5048
loader: require.resolve('./index'),
51-
options: await normalizeOptions(options),
49+
options: normalizeOptions(options),
5250
};
5351
}
5452

55-
export async function createMDXLoaderRule({
53+
export function createMDXLoaderRule({
5654
include,
5755
options,
5856
}: {
5957
include: RuleSetRule['include'];
6058
options: Options & CreateOptions;
61-
}): Promise<RuleSetRule> {
59+
}): RuleSetRule {
6260
return {
6361
test: /\.mdx?$/i,
6462
include,
65-
use: [await createMDXLoaderItem(options)],
63+
use: [createMDXLoaderItem(options)],
6664
};
6765
}

packages/docusaurus-mdx-loader/src/loader.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,7 @@ import {
2121
import type {WebpackCompilerName} from '@docusaurus/utils';
2222
import type {Options} from './options';
2323
import type {LoaderContext} from 'webpack';
24-
25-
// TODO as of April 2023, no way to import/re-export this ESM type easily :/
26-
// This might change soon, likely after TS 5.2
27-
// See https://github.com/microsoft/TypeScript/issues/49721#issuecomment-1517839391
28-
type Pluggable = any; // TODO fix this asap
29-
30-
export type MDXPlugin = Pluggable;
24+
export type {MDXPlugin} from './processor';
3125

3226
async function loadMDX({
3327
fileContent,

0 commit comments

Comments
 (0)