Unverified Commit e5c53365 authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

Fix source mapping dev middlewares for Windows (#72139)

parent 3650f478
...@@ -54,7 +54,10 @@ export async function getSourceMapFromFile( ...@@ -54,7 +54,10 @@ export async function getSourceMapFromFile(
} }
} }
const sourceMapFilename = path.resolve(path.dirname(filename), sourceUrl) const sourceMapFilename = path.resolve(
path.dirname(filename),
decodeURIComponent(sourceUrl)
)
try { try {
const sourceMapContents = await fs.readFile(sourceMapFilename, 'utf-8') const sourceMapContents = await fs.readFile(sourceMapFilename, 'utf-8')
......
...@@ -12,10 +12,12 @@ import { ...@@ -12,10 +12,12 @@ import {
import fs, { constants as FS } from 'fs/promises' import fs, { constants as FS } from 'fs/promises'
import path from 'path' import path from 'path'
import url from 'url'
import { launchEditor } from '../internal/helpers/launchEditor' import { launchEditor } from '../internal/helpers/launchEditor'
import type { StackFrame } from 'next/dist/compiled/stacktrace-parser' import type { StackFrame } from 'next/dist/compiled/stacktrace-parser'
import type { Project, TurbopackStackFrame } from '../../../../build/swc/types' import type { Project, TurbopackStackFrame } from '../../../../build/swc/types'
import { getSourceMapFromFile } from '../internal/helpers/get-source-map-from-file' import { getSourceMapFromFile } from '../internal/helpers/get-source-map-from-file'
import { findSourceMap } from 'node:module'
const currentSourcesByFile: Map<string, Promise<string | null>> = new Map() const currentSourcesByFile: Map<string, Promise<string | null>> = new Map()
export async function batchedTraceSource( export async function batchedTraceSource(
...@@ -169,17 +171,29 @@ export function getSourceMapMiddleware(project: Project, distDir: string) { ...@@ -169,17 +171,29 @@ export function getSourceMapMiddleware(project: Project, distDir: string) {
return badRequest(res) return badRequest(res)
} }
if (filename.startsWith('webpack://next/')) { if (
filename.startsWith('webpack://') ||
filename.startsWith('webpack-internal:///')
) {
const sourceMap = findSourceMap(filename)
if (sourceMap) {
return json(res, sourceMap.payload)
}
return noContent(res) return noContent(res)
} }
try { try {
if (filename.startsWith('/_next/static')) { if (filename.startsWith('/_next/static')) {
filename = path.join( filename = path.join(distDir, filename.replace(/^\/_next\//, ''))
distDir, }
// /_next/static/chunks/%5Bproject%5D... => static/chunks/[project]...
decodeURIComponent(filename.replace(/^\/_next\//, '')) // Turbopack chunk filenames might be URL-encoded.
) filename = decodeURI(filename)
if (path.isAbsolute(filename)) {
filename = url.pathToFileURL(filename).href
} }
const sourceMapString = await project.getSourceMap(filename) const sourceMapString = await project.getSourceMap(filename)
...@@ -188,7 +202,7 @@ export function getSourceMapMiddleware(project: Project, distDir: string) { ...@@ -188,7 +202,7 @@ export function getSourceMapMiddleware(project: Project, distDir: string) {
return jsonString(res, sourceMapString) return jsonString(res, sourceMapString)
} }
if (filename.startsWith('file:') || filename.startsWith(path.sep)) { if (filename.startsWith('file:')) {
const sourceMap = await getSourceMapFromFile(filename) const sourceMap = await getSourceMapFromFile(filename)
if (sourceMap) { if (sourceMap) {
......
import { constants as FS, promises as fs } from 'fs' import { constants as FS, promises as fs } from 'fs'
import path from 'path' import path from 'path'
import url from 'url'
import { SourceMapConsumer } from 'next/dist/compiled/source-map08' import { SourceMapConsumer } from 'next/dist/compiled/source-map08'
import type { StackFrame } from 'next/dist/compiled/stacktrace-parser' import type { StackFrame } from 'next/dist/compiled/stacktrace-parser'
import { getSourceMapFromFile } from '../internal/helpers/get-source-map-from-file' import { getSourceMapFromFile } from '../internal/helpers/get-source-map-from-file'
...@@ -213,7 +214,11 @@ export async function getSource( ...@@ -213,7 +214,11 @@ export async function getSource(
filename = path.join(distDirectory, filename.replace(/^\/_next\//, '')) filename = path.join(distDirectory, filename.replace(/^\/_next\//, ''))
} }
if (filename.startsWith('file:') || filename.startsWith(path.sep)) { if (path.isAbsolute(filename)) {
filename = url.pathToFileURL(filename).href
}
if (filename.startsWith('file:')) {
const sourceMap = await getSourceMapFromFile(filename) const sourceMap = await getSourceMapFromFile(filename)
return sourceMap return sourceMap
......
...@@ -154,7 +154,6 @@ module.exports = ({ dev, turbo, bundleType, experimental }) => { ...@@ -154,7 +154,6 @@ module.exports = ({ dev, turbo, bundleType, experimental }) => {
experimental ? '-experimental' : '' experimental ? '-experimental' : ''
}.runtime.${dev ? 'dev' : 'prod'}.js`, }.runtime.${dev ? 'dev' : 'prod'}.js`,
libraryTarget: 'commonjs2', libraryTarget: 'commonjs2',
devtoolModuleFilenameTemplate: './[resource-path]',
}, },
devtool: process.env.NEXT_SERVER_EVAL_SOURCE_MAPS devtool: process.env.NEXT_SERVER_EVAL_SOURCE_MAPS
? 'eval-source-map' ? 'eval-source-map'
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment