refactor(mdx-loader): remove useless usage of mdx loader this.query (#10422)

This commit is contained in:
Sébastien Lorber 2024-08-19 19:17:33 +02:00 committed by GitHub
parent 6652ed1443
commit bb499926a0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 22 additions and 31 deletions

View file

@ -140,12 +140,11 @@ export async function mdxLoader(
const compilerName = getWebpackLoaderCompilerName(this); const compilerName = getWebpackLoaderCompilerName(this);
const callback = this.async(); const callback = this.async();
const filePath = this.resourcePath; const filePath = this.resourcePath;
const reqOptions: Options = this.getOptions(); const options: Options = this.getOptions();
const {query} = this;
ensureMarkdownConfig(reqOptions); ensureMarkdownConfig(options);
const {frontMatter} = await reqOptions.markdownConfig.parseFrontMatter({ const {frontMatter} = await options.markdownConfig.parseFrontMatter({
filePath, filePath,
fileContent, fileContent,
defaultParseFrontMatter: DEFAULT_PARSE_FRONT_MATTER, defaultParseFrontMatter: DEFAULT_PARSE_FRONT_MATTER,
@ -155,16 +154,15 @@ export async function mdxLoader(
const preprocessedContent = preprocessor({ const preprocessedContent = preprocessor({
fileContent, fileContent,
filePath, filePath,
admonitions: reqOptions.admonitions, admonitions: options.admonitions,
markdownConfig: reqOptions.markdownConfig, markdownConfig: options.markdownConfig,
}); });
const hasFrontMatter = Object.keys(frontMatter).length > 0; const hasFrontMatter = Object.keys(frontMatter).length > 0;
const processor = await createProcessorCached({ const processor = await createProcessorCached({
filePath, filePath,
reqOptions, options,
query,
mdxFrontMatter, mdxFrontMatter,
}); });
@ -203,14 +201,14 @@ export async function mdxLoader(
// MDX partials are MDX files starting with _ or in a folder starting with _ // MDX partials are MDX files starting with _ or in a folder starting with _
// Partial are not expected to have associated metadata files or front matter // Partial are not expected to have associated metadata files or front matter
const isMDXPartial = reqOptions.isMDXPartial?.(filePath); const isMDXPartial = options.isMDXPartial?.(filePath);
if (isMDXPartial && hasFrontMatter) { if (isMDXPartial && hasFrontMatter) {
const errorMessage = `Docusaurus MDX partial files should not contain front matter. const errorMessage = `Docusaurus MDX partial files should not contain front matter.
Those partial files use the _ prefix as a convention by default, but this is configurable. Those partial files use the _ prefix as a convention by default, but this is configurable.
File at ${filePath} contains front matter that will be ignored: File at ${filePath} contains front matter that will be ignored:
${JSON.stringify(frontMatter, null, 2)}`; ${JSON.stringify(frontMatter, null, 2)}`;
if (!reqOptions.isMDXPartialFrontMatterWarningDisabled) { if (!options.isMDXPartialFrontMatterWarningDisabled) {
const shouldError = process.env.NODE_ENV === 'test' || process.env.CI; const shouldError = process.env.NODE_ENV === 'test' || process.env.CI;
if (shouldError) { if (shouldError) {
return callback(new Error(errorMessage)); return callback(new Error(errorMessage));
@ -222,11 +220,8 @@ ${JSON.stringify(frontMatter, null, 2)}`;
function getMetadataPath(): string | undefined { function getMetadataPath(): string | undefined {
if (!isMDXPartial) { if (!isMDXPartial) {
// Read metadata for this MDX and export it. // Read metadata for this MDX and export it.
if ( if (options.metadataPath && typeof options.metadataPath === 'function') {
reqOptions.metadataPath && return options.metadataPath(filePath);
typeof reqOptions.metadataPath === 'function'
) {
return reqOptions.metadataPath(filePath);
} }
} }
return undefined; return undefined;
@ -246,8 +241,8 @@ ${JSON.stringify(frontMatter, null, 2)}`;
: undefined; : undefined;
const assets = const assets =
reqOptions.createAssets && metadata options.createAssets && metadata
? reqOptions.createAssets({frontMatter, metadata}) ? options.createAssets({frontMatter, metadata})
: undefined; : undefined;
// TODO use remark plugins to insert extra exports instead of string concat? // TODO use remark plugins to insert extra exports instead of string concat?

View file

@ -229,31 +229,29 @@ type ProcessorsCacheEntry = {
const ProcessorsCache = new Map<string | Options, ProcessorsCacheEntry>(); const ProcessorsCache = new Map<string | Options, ProcessorsCacheEntry>();
async function createProcessorsCacheEntry({ async function createProcessorsCacheEntry({
query, options,
reqOptions,
}: { }: {
query: string | Options; options: Options;
reqOptions: Options;
}): Promise<ProcessorsCacheEntry> { }): Promise<ProcessorsCacheEntry> {
const {createProcessorSync} = await createProcessorFactory(); const {createProcessorSync} = await createProcessorFactory();
const compilers = ProcessorsCache.get(query); const compilers = ProcessorsCache.get(options);
if (compilers) { if (compilers) {
return compilers; return compilers;
} }
const compilerCacheEntry: ProcessorsCacheEntry = { const compilerCacheEntry: ProcessorsCacheEntry = {
mdProcessor: createProcessorSync({ mdProcessor: createProcessorSync({
options: reqOptions, options,
format: 'md', format: 'md',
}), }),
mdxProcessor: createProcessorSync({ mdxProcessor: createProcessorSync({
options: reqOptions, options,
format: 'mdx', format: 'mdx',
}), }),
}; };
ProcessorsCache.set(query, compilerCacheEntry); ProcessorsCache.set(options, compilerCacheEntry);
return compilerCacheEntry; return compilerCacheEntry;
} }
@ -261,20 +259,18 @@ async function createProcessorsCacheEntry({
export async function createProcessorCached({ export async function createProcessorCached({
filePath, filePath,
mdxFrontMatter, mdxFrontMatter,
query, options,
reqOptions,
}: { }: {
filePath: string; filePath: string;
mdxFrontMatter: MDXFrontMatter; mdxFrontMatter: MDXFrontMatter;
query: string | Options; options: Options;
reqOptions: Options;
}): Promise<SimpleProcessor> { }): Promise<SimpleProcessor> {
const compilers = await createProcessorsCacheEntry({query, reqOptions}); const compilers = await createProcessorsCacheEntry({options});
const format = getFormat({ const format = getFormat({
filePath, filePath,
frontMatterFormat: mdxFrontMatter.format, frontMatterFormat: mdxFrontMatter.format,
markdownConfigFormat: reqOptions.markdownConfig.format, markdownConfigFormat: options.markdownConfig.format,
}); });
return format === 'md' ? compilers.mdProcessor : compilers.mdxProcessor; return format === 'md' ? compilers.mdProcessor : compilers.mdxProcessor;