Skip to content

Commit

Permalink
Merge pull request #28585 from storybookjs/valentin/fix-source-maps-f…
Browse files Browse the repository at this point in the history
…or-webpack

Webpack: Fix sourceMap generation in csf-tools
  • Loading branch information
valentinpalkovic authored Jul 12, 2024
2 parents 7f89a8d + af48f0d commit 239ce53
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
10 changes: 2 additions & 8 deletions code/core/src/csf-tools/CsfFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { readFile, writeFile } from 'node:fs/promises';
import { dedent } from 'ts-dedent';

import * as t from '@babel/types';
import bg from '@babel/generator';
import bg, { type GeneratorOptions } from '@babel/generator';
import bt from '@babel/traverse';

import * as recast from 'recast';
Expand Down Expand Up @@ -599,15 +599,9 @@ export const loadCsf = (code: string, options: CsfOptions) => {
return new CsfFile(ast, options);
};

interface FormatOptions {
sourceMaps?: boolean;
preserveStyle?: boolean;
inputSourceMap?: any;
}

export const formatCsf = (
csf: CsfFile,
options: FormatOptions = { sourceMaps: false },
options: GeneratorOptions & { inputSourceMap?: string } = { sourceMaps: false },
code?: string
) => {
const result = generate(csf._ast, options, code);
Expand Down
6 changes: 5 additions & 1 deletion code/lib/csf-plugin/src/webpack-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ async function loader(this: LoaderContext, content: string, map: any) {
const csf = loadCsf(content, { makeTitle }).parse();
const csfSource = loadCsf(sourceCode, { makeTitle }).parse();
enrichCsf(csf, csfSource, options);
const formattedCsf = formatCsf(csf, { sourceMaps: true, inputSourceMap: map }, content);
const formattedCsf = formatCsf(
csf,
{ sourceMaps: true, inputSourceMap: map, sourceFileName: id },
content
);

if (typeof formattedCsf === 'string') {
return callback(null, formattedCsf, map);
Expand Down

0 comments on commit 239ce53

Please sign in to comment.