mirror of
https://github.com/facebook/docusaurus.git
synced 2025-07-13 23:07:58 +02:00
refactor(mdx-loader): refactor mdx-loader, expose loader creation utils (#10450)
This commit is contained in:
parent
db6c2af160
commit
d5885c0c5d
13 changed files with 494 additions and 413 deletions
48
packages/docusaurus-mdx-loader/src/createMDXLoader.ts
Normal file
48
packages/docusaurus-mdx-loader/src/createMDXLoader.ts
Normal file
|
@ -0,0 +1,48 @@
|
|||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
import {createProcessors} from './processor';
|
||||
import type {Options} from './loader';
|
||||
import type {RuleSetRule, RuleSetUseItem} from 'webpack';
|
||||
|
||||
async function enhancedOptions(options: Options): Promise<Options> {
|
||||
// Because Jest doesn't like ESM / createProcessors()
|
||||
if (process.env.N0DE_ENV === 'test' || process.env.JEST_WORKER_ID) {
|
||||
return options;
|
||||
}
|
||||
|
||||
// We create the processor earlier here, to avoid the lazy processor creating
|
||||
// Lazy creation messes-up with Rsdoctor ability to measure mdx-loader perf
|
||||
const newOptions: Options = options.processors
|
||||
? options
|
||||
: {...options, processors: await createProcessors({options})};
|
||||
|
||||
return newOptions;
|
||||
}
|
||||
|
||||
export async function createMDXLoaderItem(
|
||||
options: Options,
|
||||
): Promise<RuleSetUseItem> {
|
||||
return {
|
||||
loader: require.resolve('@docusaurus/mdx-loader'),
|
||||
options: await enhancedOptions(options),
|
||||
};
|
||||
}
|
||||
|
||||
export async function createMDXLoaderRule({
|
||||
include,
|
||||
options,
|
||||
}: {
|
||||
include: RuleSetRule['include'];
|
||||
options: Options;
|
||||
}): Promise<RuleSetRule> {
|
||||
return {
|
||||
test: /\.mdx?$/i,
|
||||
include,
|
||||
use: [await createMDXLoaderItem(options)],
|
||||
};
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue