Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use pathToFileUrl to make esm import()s work with absolute windows paths #64386

Merged
merged 7 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/build/swc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
1 change: 1 addition & 0 deletions packages/next/src/lib/find-config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
21 changes: 19 additions & 2 deletions packages/next/src/lib/find-config.ts
Original file line number Diff line number Diff line change
@@ -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]>
Expand Down Expand Up @@ -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)
}
Expand Down
Loading