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:
parent
bf45353af1
commit
fbf9e12de0
4 changed files with 23 additions and 4 deletions
|
@ -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'
|
||||
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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',
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue