From 82fc267e8acca22a7c9cc16bc38632da703bbcee Mon Sep 17 00:00:00 2001 From: Dimitri POSTOLOV Date: Wed, 2 Oct 2024 20:45:11 +0200 Subject: [PATCH] remove `Critical dependency: the request of a dependency is an expression` warnings (#3330) * patch `@typescript/vfs` to remove `Critical dependency: the request of a dependency is an expression` warnings * fix * Update .changeset/dry-seas-pull.md * aa * prettier --- .changeset/dry-seas-pull.md | 5 ++++ packages/nextra/src/server/index.ts | 35 ++++++++++++++++------------ packages/nextra/src/server/loader.ts | 8 +++++++ 3 files changed, 33 insertions(+), 15 deletions(-) create mode 100644 .changeset/dry-seas-pull.md diff --git a/.changeset/dry-seas-pull.md b/.changeset/dry-seas-pull.md new file mode 100644 index 0000000000..d08425b9ed --- /dev/null +++ b/.changeset/dry-seas-pull.md @@ -0,0 +1,5 @@ +--- +'nextra': patch +--- + +remove `Critical dependency: the request of a dependency is an expression` warnings diff --git a/packages/nextra/src/server/index.ts b/packages/nextra/src/server/index.ts index e7005dded5..a29c814474 100644 --- a/packages/nextra/src/server/index.ts +++ b/packages/nextra/src/server/index.ts @@ -20,6 +20,8 @@ const DEFAULT_EXTENSIONS = ['js', 'jsx', 'ts', 'tsx'] const AGNOSTIC_PAGE_MAP_PATH = `.next${sep}static${sep}chunks${sep}nextra-page-map` +const RE_SEP = sep === '/' ? '/' : '\\\\' + const nextra: Nextra = nextraConfig => { const { error } = nextraConfigSchema.safeParse(nextraConfig) if (error) { @@ -152,6 +154,14 @@ const nextra: Nextra = nextraConfig => { }) } + const defaultLoaderOptions = [ + options.defaultLoaders.babel, + { + loader: 'nextra/loader', + options: loaderOptions + } + ] + rules.push( { // Match Markdown imports from non-pages. These imports have an @@ -162,13 +172,7 @@ const nextra: Nextra = nextraConfig => { issuer: request => (!!request && !request.includes(AGNOSTIC_PAGE_MAP_PATH)) || request === null, - use: [ - options.defaultLoaders.babel, - { - loader: 'nextra/loader', - options: loaderOptions - } - ] + use: defaultLoaderOptions }, { // Match pages (imports without an issuer request). @@ -198,15 +202,16 @@ const nextra: Nextra = nextraConfig => { }, { // Use platform separator because /pages\/_app\./ will not work on windows - test: new RegExp(`pages${sep === '/' ? '/' : '\\\\'}_app\\.`), + test: new RegExp(`pages${RE_SEP}_app\\.`), issuer: request => !request, - use: [ - options.defaultLoaders.babel, - { - loader: 'nextra/loader', - options: loaderOptions - } - ] + use: defaultLoaderOptions + }, + { + test: new RegExp( + `@typescript${RE_SEP}vfs${RE_SEP}dist${RE_SEP}vfs\\.` + ), + issuer: request => !!request, + use: defaultLoaderOptions } ) diff --git a/packages/nextra/src/server/loader.ts b/packages/nextra/src/server/loader.ts index e6ee41b14e..e3d61d4cd3 100644 --- a/packages/nextra/src/server/loader.ts +++ b/packages/nextra/src/server/loader.ts @@ -83,6 +83,14 @@ export async function loader( const currentPath = slash(mdxPath) + if (currentPath.includes('@typescript/vfs/dist/vfs.')) { + // Fixes https://github.com/microsoft/TypeScript-Website/pull/3022 + // Fixes https://github.com/shuding/nextra/issues/3322#issuecomment-2384046618 + return source + .replace(/String\.fromCharCode\(112, ?97, ?116, ?104\)/, '"path"') + .replace(/String\.fromCharCode\(102, ?115\)/, '"fs"') + } + if (currentPath.includes('/pages/api/')) { logger.warn( `Ignoring ${currentPath} because it is located in the "pages/api" folder.`