Skip to content

Commit

Permalink
Fix sourcemaps across loaders and builders
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinpalkovic committed May 23, 2024
1 parent ac6c1e5 commit 780ef97
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ export default async function loader(
);
}

const generatedMap = magicString.generateMap({ hires: true });

return callback(null, magicString.toString(), generatedMap, meta);
return callback(null, magicString.toString(), map, meta);
} catch (err) {
return callback(null, source, map, meta);
}
Expand Down
1 change: 0 additions & 1 deletion code/frameworks/nextjs/src/swc/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export const configureSWCLoader = async (
test: /\.((c|m)?(j|t)sx?)$/,
include: [getProjectRoot()],
exclude: [/(node_modules)/, ...Object.keys(virtualModules)],
enforce: 'post',
use: {
// we use our own patch because we need to remove tracing from the original code
// which is not possible otherwise
Expand Down
16 changes: 5 additions & 11 deletions code/frameworks/nextjs/src/swc/next-swc-loader-patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ async function loaderTransform(this: any, parentTrace: any, source?: string, inp
const programmaticOptions = {
...swcOptions,
filename,
inputSourceMap: inputSourceMap ? JSON.stringify(inputSourceMap) : undefined,
inputSourceMap:
inputSourceMap && typeof inputSourceMap === 'object'
? JSON.stringify(inputSourceMap)
: undefined,

// Set the default sourcemap behavior based on Webpack's mapping flag,
sourceMaps: this.sourceMap,
Expand Down Expand Up @@ -166,20 +169,11 @@ export function pitch(this: any) {
}, callback);
}

function sanitizeSourceMap(rawSourceMap: any): any {
const { sourcesContent, ...sourceMap } = rawSourceMap ?? {};

// JSON parse/stringify trick required for swc to accept the SourceMap
return JSON.parse(JSON.stringify(sourceMap));
}

export default function swcLoader(this: any, inputSource: string, inputSourceMap: any) {
const loaderSpan = mockCurrentTraceSpan.traceChild('next-swc-loader');
const callback = this.async();
loaderSpan
.traceAsyncFn(() =>
loaderTransform.call(this, loaderSpan, inputSource, sanitizeSourceMap(inputSourceMap))
)
.traceAsyncFn(() => loaderTransform.call(this, loaderSpan, inputSource, inputSourceMap))
.then(
([transformedSource, outputSourceMap]: any) => {
callback(null, transformedSource, outputSourceMap || inputSourceMap);
Expand Down
12 changes: 8 additions & 4 deletions code/lib/csf-tools/src/CsfFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -583,15 +583,19 @@ export const loadCsf = (code: string, options: CsfOptions) => {
interface FormatOptions {
sourceMaps?: boolean;
preserveStyle?: boolean;
inputSourceMap?: any;
}

export const formatCsf = (csf: CsfFile, options: FormatOptions = { sourceMaps: false }) => {
const result = generate.default(csf._ast, options);
export const formatCsf = (
csf: CsfFile,
options: FormatOptions = { sourceMaps: false },
code?: string
) => {
const result = generate.default(csf._ast, options, code);
if (options.sourceMaps) {
return result;
}
const { code } = result;
return code;
return result.code;
};

/**
Expand Down

0 comments on commit 780ef97

Please sign in to comment.