mirror of
https://github.com/facebook/docusaurus.git
synced 2025-07-12 22:38:04 +02:00
feat(core, mdx-loader): deduplicate MDX compilation - siteConfig.future.experimental_faster.mdxCrossCompilerCache
(#10479)
This commit is contained in:
parent
897ebbe3ca
commit
5bab0b5432
18 changed files with 333 additions and 151 deletions
|
@ -6,30 +6,49 @@
|
|||
*/
|
||||
|
||||
import {createProcessors} from './processor';
|
||||
import type {Options} from './loader';
|
||||
import type {Options} from './options';
|
||||
import type {RuleSetRule, RuleSetUseItem} from 'webpack';
|
||||
|
||||
async function enhancedOptions(options: Options): Promise<Options> {
|
||||
type CreateOptions = {
|
||||
useCrossCompilerCache?: boolean;
|
||||
};
|
||||
|
||||
async function normalizeOptions(
|
||||
optionsInput: Options & CreateOptions,
|
||||
): Promise<Options> {
|
||||
// Because Jest doesn't like ESM / createProcessors()
|
||||
if (process.env.N0DE_ENV === 'test' || process.env.JEST_WORKER_ID) {
|
||||
return options;
|
||||
return optionsInput;
|
||||
}
|
||||
|
||||
let options = optionsInput;
|
||||
|
||||
// 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})};
|
||||
if (!options.processors) {
|
||||
options = {...options, processors: await createProcessors({options})};
|
||||
}
|
||||
|
||||
return newOptions;
|
||||
// Cross-compiler cache permits to compile client/server MDX only once
|
||||
// We don't want to cache in dev mode (docusaurus start)
|
||||
// We only have multiple compilers in production mode (docusaurus build)
|
||||
// TODO wrong but good enough for now (example: "docusaurus build --dev")
|
||||
if (options.useCrossCompilerCache && process.env.NODE_ENV === 'production') {
|
||||
options = {
|
||||
...options,
|
||||
crossCompilerCache: new Map(),
|
||||
};
|
||||
}
|
||||
|
||||
return options;
|
||||
}
|
||||
|
||||
export async function createMDXLoaderItem(
|
||||
options: Options,
|
||||
options: Options & CreateOptions,
|
||||
): Promise<RuleSetUseItem> {
|
||||
return {
|
||||
loader: require.resolve('@docusaurus/mdx-loader'),
|
||||
options: await enhancedOptions(options),
|
||||
options: await normalizeOptions(options),
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -38,7 +57,7 @@ export async function createMDXLoaderRule({
|
|||
options,
|
||||
}: {
|
||||
include: RuleSetRule['include'];
|
||||
options: Options;
|
||||
options: Options & CreateOptions;
|
||||
}): Promise<RuleSetRule> {
|
||||
return {
|
||||
test: /\.mdx?$/i,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue