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

[Bug] svelte-stories-loader doesn't preprocess stories when using Webpack #168

Open
vadirn opened this issue Jan 29, 2024 · 0 comments
Open
Labels
bug Something isn't working

Comments

@vadirn
Copy link

vadirn commented Jan 29, 2024

Describe the bug

When using webpack5.

const source = readFileSync(resource).toString();
just reads the file, extractStories expects preprocessed source. So if story uses typescript, storybook fails.

Steps to reproduce the behavior

Just try to use lang="ts" and a type in any stories.svelte file, when using svelte-webpack5.

Expected behavior

*.stories.svelte should also be preprocessed.

Additional context

I've tried to patch the loader to make it work, but it also requires modifying storybook's webpack config. I'm not sure what's the proper way to pass svelte-webpack5 options to the loader config by default here:

export function webpack(config) {

And here is what I ended up modifying:

async function transformSvelteStories(code) {
    const options = this.getOptions();
    // @ts-ignore eslint-disable-next-line no-underscore-dangle
    const { resource } = this._module;
    const componentName = getNameFromFilename(resource);
    const source = readFileSync(resource).toString();
    // PREPROCESS -> 
    const { code: processedSource } = await svelte.preprocess(source, options.preprocess, { filename: options.filename});
    const storiesDef = extractStories(processedSource);
    const { stories } = storiesDef;
    const storyDef = Object.entries(stories)
        .filter(([, def]) => !def.template)
        .map(([id]) => `export const ${id} = __storiesMetaData.stories[${JSON.stringify(id)}]`)
        .join('\n');
    const metaExported = code.includes('export { meta }');
    const codeWithoutDefaultExport = code.replace('export default ', '//export default').replace('export { meta };', '// export { meta };');
    return `${codeWithoutDefaultExport}
    const { default: parser } = require('${parser}');
    const __storiesMetaData = parser(${componentName}, ${JSON.stringify(storiesDef)}${metaExported ? ', meta' : ''});
    export default __storiesMetaData.meta;
    ${storyDef};
  `;
}
@vadirn vadirn added the bug Something isn't working label Jan 29, 2024
@JReinhold JReinhold changed the title [Bug] svelte-stories-loader doesn't preprocess stories [Bug] svelte-stories-loader doesn't preprocess stories when using Webpack May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant