Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

Commit

Permalink
Ensure Next.js core development sourcemaps use correct input file (ve…
Browse files Browse the repository at this point in the history
…rcel#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.
  • Loading branch information
timneutkens authored Aug 1, 2021
1 parent fb8ce0a commit e6bcdf6
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions packages/next/taskfile-swc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,28 @@ const path = require('path')
// eslint-disable-next-line import/no-extraneous-dependencies
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) {
// eslint-disable-next-line require-yield
task.plugin('swc', {}, function* (
file,
serverOrClient,
{ stripExtension, dev } = {}
{ stripExtension } = {}
) {
// Don't compile .d.ts
if (file.base.endsWith('.d.ts')) return

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 = {
module: {
type: 'commonjs',
Expand Down Expand Up @@ -108,7 +118,14 @@ module.exports = function (task) {
this._.files.push({
base: map,
dir: file.dir,
data: Buffer.from(output.map),
data: Buffer.from(
JSON.stringify(
updateSourceMapSources(
JSON.parse(output.map),
path.relative(distFilePath, fullFilePath)
)
)
),
})
}

Expand Down

0 comments on commit e6bcdf6

Please sign in to comment.