Unverified Commit f9ebc777 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Ensure Next.js core development sourcemaps use correct input file (#27623)

Noticed while debugging that the error output lines did not make sense and found that `sourceFileName` is not supported in SWC yet. This is a temporary fix until @kdy1 adds the option.
parent c0bb1bc8
...@@ -6,18 +6,28 @@ const path = require('path') ...@@ -6,18 +6,28 @@ const path = require('path')
// eslint-disable-next-line import/no-extraneous-dependencies // eslint-disable-next-line import/no-extraneous-dependencies
const transform = require('@swc/core').transform const transform = require('@swc/core').transform
// SWC doesn't allow setting `sourceFileName` yet so we have to manually edit the sourcemap to the correct input file path
function updateSourceMapSources(map, sourceFileName) {
map.sources = [sourceFileName]
return map
}
module.exports = function (task) { module.exports = function (task) {
// eslint-disable-next-line require-yield // eslint-disable-next-line require-yield
task.plugin('swc', {}, function* ( task.plugin('swc', {}, function* (
file, file,
serverOrClient, serverOrClient,
{ stripExtension, dev } = {} { stripExtension } = {}
) { ) {
// Don't compile .d.ts // Don't compile .d.ts
if (file.base.endsWith('.d.ts')) return if (file.base.endsWith('.d.ts')) return
const isClient = serverOrClient === 'client' const isClient = serverOrClient === 'client'
const filePath = path.join(file.dir, file.base)
const fullFilePath = path.join(__dirname, filePath)
const distFilePath = path.dirname(path.join(__dirname, 'dist', filePath))
const swcClientOptions = { const swcClientOptions = {
module: { module: {
type: 'commonjs', type: 'commonjs',
...@@ -108,7 +118,14 @@ module.exports = function (task) { ...@@ -108,7 +118,14 @@ module.exports = function (task) {
this._.files.push({ this._.files.push({
base: map, base: map,
dir: file.dir, dir: file.dir,
data: Buffer.from(output.map), data: Buffer.from(
JSON.stringify(
updateSourceMapSources(
JSON.parse(output.map),
path.relative(distFilePath, fullFilePath)
)
)
),
}) })
} }
......
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