Skip to content

Commit

Permalink
Fix source-loader-plugin CSF3 compatibility (#165)
Browse files Browse the repository at this point in the history
Closes #164
  • Loading branch information
joshwooding authored Nov 24, 2021
1 parent c2e9c3c commit dfb72df
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions packages/storybook-builder-vite/source-loader-plugin.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
const inject =
require('@storybook/source-loader/dist/cjs/abstract-syntax-tree/inject-decorator').default;
const sourceLoaderTransform =
require('@storybook/source-loader').default;

module.exports.sourceLoaderPlugin = function () {
return {
name: 'storybook-vite-source-loader-plugin',
enforce: 'pre',
transform(src, id) {
async transform(src, id) {
if (id.match(/\.stories\.[jt]sx?$/)) {
const { source, sourceJson, addsMap } = inject(src, id);
const preamble = `
/* eslint-disable */
// @ts-nocheck
// @ts-ignore
var __STORY__ = ${sourceJson};
// @ts-ignore
var __LOCATIONS_MAP__ = ${JSON.stringify(addsMap)};
`;
// We need to mock 'this' when calling transform from @storybook/source-loader
const mockClassLoader = { emitWarning: (message) => console.warn(message), resourcePath: id };
const code = await sourceLoaderTransform.call(mockClassLoader, src);

return {
code: `${preamble}\n${source}`,
code,
map: { mappings: '' },
};
}
Expand Down

0 comments on commit dfb72df

Please sign in to comment.