-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27171 from storybookjs/valentin/fix-webpack5-sour…
…cemaps Builder: Fixes sourcemaps for Webpack5 and Vite builder
- Loading branch information
Showing
12 changed files
with
140 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const STORIES_REGEX = /(?<!node_modules.*)\.(story|stories)\.[tj]sx?$/; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import type { EnrichCsfOptions } from '@storybook/csf-tools'; | ||
import { enrichCsf, formatCsf, loadCsf } from '@storybook/csf-tools'; | ||
import type { RollupPlugin } from 'unplugin'; | ||
import fs from 'fs/promises'; | ||
import { STORIES_REGEX } from './constants'; | ||
|
||
const logger = console; | ||
|
||
export function rollupBasedPlugin(options: EnrichCsfOptions): Partial<RollupPlugin<any>> { | ||
return { | ||
name: 'plugin-csf', | ||
async transform(code, id) { | ||
if (!STORIES_REGEX.test(id)) { | ||
return; | ||
} | ||
|
||
const sourceCode = await fs.readFile(id, 'utf-8'); | ||
try { | ||
const makeTitle = (userTitle: string) => userTitle || 'default'; | ||
const csf = loadCsf(code, { makeTitle }).parse(); | ||
const csfSource = loadCsf(sourceCode, { | ||
makeTitle, | ||
}).parse(); | ||
enrichCsf(csf, csfSource, options); | ||
const inputSourceMap = this.getCombinedSourcemap(); | ||
return formatCsf(csf, { sourceMaps: true, inputSourceMap }, code); | ||
} catch (err: any) { | ||
// This can be called on legacy storiesOf files, so just ignore | ||
// those errors. But warn about other errors. | ||
if (!err.message?.startsWith('CSF:')) { | ||
logger.warn(err.message); | ||
} | ||
return code; | ||
} | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import fs from 'fs/promises'; | ||
import type { EnrichCsfOptions } from '@storybook/csf-tools'; | ||
import { loadCsf, formatCsf, enrichCsf } from '@storybook/csf-tools'; | ||
|
||
interface LoaderContext { | ||
async: () => (err: Error | null, result?: string, map?: any) => void; | ||
getOptions: () => EnrichCsfOptions; | ||
resourcePath: string; | ||
} | ||
|
||
async function loader(this: LoaderContext, content: string, map: any) { | ||
const callback = this.async(); | ||
const options = this.getOptions(); | ||
const id = this.resourcePath; | ||
|
||
const sourceCode = await fs.readFile(id, 'utf-8'); | ||
|
||
try { | ||
const makeTitle = (userTitle: string) => userTitle || 'default'; | ||
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); | ||
|
||
if (typeof formattedCsf === 'string') { | ||
return callback(null, formattedCsf, map); | ||
} | ||
|
||
callback(null, formattedCsf.code, formattedCsf.map); | ||
} catch (err: any) { | ||
// This can be called on legacy storiesOf files, so just ignore | ||
// those errors. But warn about other errors. | ||
if (!err.message?.startsWith('CSF:')) { | ||
console.warn(err.message); | ||
} | ||
callback(null, content, map); | ||
} | ||
} | ||
|
||
export default loader; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters