From 14987d96674275c7b1004a4ceb33947499d7247c Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Fri, 12 Apr 2024 19:58:13 +0200 Subject: [PATCH] use pathToFileUrl to make esm import()s work with absolute windows paths (#64386) ### What? Fixes #64371 Fixes #63359 Closes PACK-2946 --------- Co-authored-by: Tim Neutkens Co-authored-by: Jiachi Liu --- azure-pipelines.yml | 3 ++- packages/next/src/build/swc/index.ts | 2 +- packages/next/src/lib/find-config.test.ts | 1 + packages/next/src/lib/find-config.ts | 21 +++++++++++++++++++-- 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/azure-pipelines.yml b/azure-pipelines.yml index a4a63e68955c0..14c69e0bbc3ab 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -69,8 +69,9 @@ stages: - script: npx playwright@1.35.1 install chromium condition: eq(variables['isDocsOnly'], 'No') + # Test critical app router and CNA tests to cover basic usage cases with windows - script: | - node run-tests.js -c 1 test/production/pages-dir/production/test/index.test.ts test/integration/css-client-nav/test/index.test.js test/integration/rewrites-has-condition/test/index.test.js + node run-tests.js -c 1 test/production/pages-dir/production/test/index.test.ts test/integration/css-client-nav/test/index.test.js test/integration/rewrites-has-condition/test/index.test.js test/integration/create-next-app/examples.test.ts test/integration/create-next-app/index.test.ts test/integration/create-next-app/package-manager/pnpm.test.ts condition: eq(variables['isDocsOnly'], 'No') displayName: 'Run tests' diff --git a/packages/next/src/build/swc/index.ts b/packages/next/src/build/swc/index.ts index 5219c4797b9a3..86aef60666c6c 100644 --- a/packages/next/src/build/swc/index.ts +++ b/packages/next/src/build/swc/index.ts @@ -1220,7 +1220,7 @@ async function loadWasm(importPath = '') { // the import path must be exact when not in node_modules pkgPath = path.join(importPath, pkg, 'wasm.js') } - let bindings = await import(pkgPath) + let bindings = await import(pathToFileURL(pkgPath).toString()) if (pkg === '@next/swc-wasm-web') { bindings = await bindings.default() } diff --git a/packages/next/src/lib/find-config.test.ts b/packages/next/src/lib/find-config.test.ts index dd00d43216266..ed1cee47c6d78 100644 --- a/packages/next/src/lib/find-config.test.ts +++ b/packages/next/src/lib/find-config.test.ts @@ -3,6 +3,7 @@ import { join } from 'node:path' import { tmpdir } from 'node:os' import { findConfig } from './find-config' +// Jest does not support `import('file://something')` (file: imports) yet. describe('findConfig()', () => { const exampleConfig = { basePath: '/docs', diff --git a/packages/next/src/lib/find-config.ts b/packages/next/src/lib/find-config.ts index 58b2f6abc3e7a..4bcce7b6ee771 100644 --- a/packages/next/src/lib/find-config.ts +++ b/packages/next/src/lib/find-config.ts @@ -1,6 +1,7 @@ import findUp from 'next/dist/compiled/find-up' import { readFile } from 'fs/promises' import JSON5 from 'next/dist/compiled/json5' +import { pathToFileURL } from 'url' type RecursivePartial = { [P in keyof T]?: RecursivePartial @@ -64,11 +65,27 @@ export async function findConfig( const filePath = await findConfigPath(directory, key) + const esmImport = (path: string) => { + // Skip mapping to absolute url with pathToFileURL on windows if it's jest + // https://github.com/nodejs/node/issues/31710#issuecomment-587345749 + if (process.platform === 'win32' && !process.env.JEST_WORKER_ID) { + // on windows import("C:\\path\\to\\file") is not valid, so we need to + // use file:// URLs + return import(pathToFileURL(path).toString()) + } else { + return import(path) + } + } + if (filePath) { if (filePath.endsWith('.js')) { - return isESM ? (await import(filePath)).default : require(filePath) + if (isESM) { + return (await esmImport(filePath)).default + } else { + return require(filePath) + } } else if (filePath.endsWith('.mjs')) { - return (await import(filePath)).default + return (await esmImport(filePath)).default } else if (filePath.endsWith('.cjs')) { return require(filePath) }