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 <tim@timneutkens.nl>
Co-authored-by: Jiachi Liu <inbox@huozhi.im>
This commit is contained in:
Tobias Koppers 2024-04-12 19:58:13 +02:00 committed by GitHub
parent bf45353af1
commit fbf9e12de0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 23 additions and 4 deletions

View file

@ -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'

View file

@ -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()
}

View file

@ -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',

View file

@ -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<T> = {
[P in keyof T]?: RecursivePartial<T[P]>
@ -64,11 +65,27 @@ export async function findConfig<T>(
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)
}