Skip to content

Commit

Permalink
use pathToFileUrl to make esm import()s work with absolute windows pa…
Browse files Browse the repository at this point in the history
…ths (#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>
  • Loading branch information
3 people authored and ztanner committed Apr 12, 2024
1 parent 774563f commit 14987d9
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
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

0 comments on commit 14987d9

Please sign in to comment.