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

Fix svelte-stories-loader in Windows #133

Merged
merged 1 commit into from
Aug 31, 2023
Merged
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
7 changes: 2 additions & 5 deletions src/parser/svelte-stories-loader.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import dedent from 'dedent';
import { extractStories } from './extract-stories.js';
import { fileURLToPath } from 'url';
import { readFileSync } from 'fs';

const parser = fileURLToPath(new URL('./collect-stories.js', import.meta.url))
.replace(/\\/g, "\\\\"); // For Windows paths;
.replace(/\\/g, '/'); // For Windows paths;
Comment on lines 5 to +6
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe call path.posix instead


// From https://github.com/sveltejs/svelte/blob/8db3e8d0297e052556f0b6dde310ef6e197b8d18/src/compiler/compile/utils/get_name_from_filename.ts
// Copied because it is not exported from the compiler
Expand Down Expand Up @@ -57,9 +56,7 @@ function transformSvelteStories(code: string) {

const codeWithoutDefaultExport = code.replace('export default ', '//export default');

// throws dedent expression is not callable.
// @ts-ignore
return dedent`${codeWithoutDefaultExport}
return `${codeWithoutDefaultExport}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why remove dedent? if we remove it I think you'll need to remove the comments on the lines above this as well as the import

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a regression with dedent from Storybook v7. It doesn't handle correctly quoted backslash. I have added an example in the referenced issue.

Note that the vite loader doesn't use dedent either.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will

source = dedent`${component.substr(start, end - start)}`;
fail on windows as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably. However here where are not generating invalid javacsript. at worst the source will have invalid breaking line, but we really need dedent here.

In fact, this issue is not related to windows and is caused by this commit: 02ce02a

Copy link
Contributor

@benmccann benmccann Aug 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it would help to upgrade this project from dedent 1.2 to the latest version (1.5.1 if I recall correctly)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're ok with this, let's create a new issue to improve the rendering of the source snippet.

I'd like to close this PR, it's the last issue for upgrading my stack to Svelte 4 & Storybook 7.4 ^^

const { default: parser } = require('${parser}');
const __storiesMetaData = parser(${componentName}, ${JSON.stringify(storiesDef)});
export default __storiesMetaData.meta;
Expand Down