Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webpack: Fix sourceMap generation in csf-tools #28585

Merged
merged 1 commit into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading