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

Add ladle plugin #728

Merged
merged 16 commits into from
Jul 22, 2024
Merged

Add ladle plugin #728

merged 16 commits into from
Jul 22, 2024

Conversation

justb3a
Copy link
Contributor

@justb3a justb3a commented Jul 12, 2024

Ladle is a lightweight Storybook alternative. Its configuration is stored in .ladle/config.mjs, along with some other files. It uses Vite, a custom Vite config is sometimes necessary in a customizable location via viteConfig.

Related issue: #268

Copy link

netlify bot commented Jul 12, 2024

Deploy Preview for knip canceled.

Name Link
🔨 Latest commit 9ca4e9a
🔍 Latest deploy log https://app.netlify.com/sites/knip/deploys/669e8f9021796b0008a8be98

Copy link
Collaborator

@webpro webpro left a comment

Choose a reason for hiding this comment

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

Thanks @justb3a! A Ladle plugin is a great addition to Knip.

My main point of feedback is that the fixtures are a bit minimal. Ideally there would be more configuration with stories and components to verify the right files are indeed used as entry files (and they'll probably use dependencies that are then no longer unused).


const resolveEntryPaths: ResolveEntryPaths<LadleConfig> = async localConfig => {
const localStories = typeof localConfig.stories === 'string' ? [localConfig.stories] : localConfig.stories;
const localViteConfig = localConfig.viteConfig ? [localConfig.viteConfig] : [];
Copy link
Collaborator

Choose a reason for hiding this comment

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

The Vite config should be handled by the Vite plugin, so I think we can just ignore it here? Btw it would need be a config file, not an entry file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For ladle it might be necessary to have a dedicated / different viteConfig file. So in my case I have ~/.vite.config.ts and for ladle ~/.ladle/vite.config.ts. Only if you don't use the default one, this parameter is set and then should be respected. Otherwise, I get it listed as "unused file".

Copy link
Collaborator

Choose a reason for hiding this comment

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

OK in that case I think it's interesting to look into using the resolveConfig function of the Vitest plugin (which is basically the same as the Vite plugin).

Simplified inside resolveConfig: const viteDeps = resolveConfig(await load(localConfig.viteConfig))

Does that make sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed 👍 I wasn't aware that it is allowed to use other plugins.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

… I cannot get rid of the message that the file is unused, stuck here.

Unused files (1)
.ladle/vite.config.ts

I also tried things like

const resolveConfig: ResolveConfig<LadleConfig> = async (localConfig, options) =>
  resolveVitestConfig(localConfig.viteConfig ? await load(join(process.cwd(), localConfig.viteConfig)) : [], options);

Maybe someone with more insights can help here? 😇

Copy link
Collaborator

Choose a reason for hiding this comment

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

Perhaps confusingly, loading the file does not "use" it (because it's not part of the dependency graph). For this you could add it as an entry file (contrary to what I said earlier, sorry, but that was for another reason).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I re-added it additionally as an entry file 👍

packages/knip/src/plugins/ladle/index.ts Outdated Show resolved Hide resolved
packages/knip/src/plugins/ladle/index.ts Outdated Show resolved Hide resolved
Copy link

pkg-pr-new bot commented Jul 13, 2024

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

commit: 9ca4e9a

knip

npm i https://pkg.pr.new/knip@728


templates

@justb3a
Copy link
Contributor Author

justb3a commented Jul 15, 2024

@webpro thanks for the feedback. I added some commits on top and extended the fixtures. If you want to have something else regarding fixtures/tests pls give me some hints, I checked some other plugins and couldn't find something helpful. I did not add node_modules folder because this one would be quite big even with the limited set of package json entries.

@webpro
Copy link
Collaborator

webpro commented Jul 15, 2024

I added some commits on top and extended the fixtures. If you want to have something else regarding fixtures/tests pls give me some hints, I checked some other plugins and couldn't find something helpful.

Most plugins should have fixtures for the config and entry files they're referencing in the implementation.

I did not add node_modules folder because this one would be quite big even with the limited set of package json entries.

Great, that would be too much indeed. Sometimes a basic definConfig might come in useful, such as the one for Vite (https://github.com/webpro-nl/knip/blob/main/packages/knip/fixtures/plugins/vite/node_modules/vite/index.js).

};

const resolveConfig: ResolveConfig<LadleConfig> = async (localConfig, options) =>
localConfig.viteConfig ? resolveVitestConfig(await load(localConfig.viteConfig), options) : [];
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please expand it a bit and use something like join(options.configFileDir, localConfig.viteConfig) so that it is resolved relative the Ladle config file dir.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh never mind, now I read https://ladle.dev/docs/config/#viteconfig and see that the path should be absolute anyway (wasn't clear from the fixture).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure, just the example uses an absolute path, I could not find something in the documentation stating that is has to be absolute.

I think it would be great to support both, so I now check if the path is already absolute and if not, I join it with options.cwd. (I did not check before what's inside options, so helpful 🤣 ).

At least on my machine with our config it now works like a charm :)

@justb3a
Copy link
Contributor Author

justb3a commented Jul 16, 2024

@webpro do you have any idea why the test is failing in CI?

test/plugins/ladle.test.ts:
  dyld[2233]: missing symbol called
  error: script "test" was terminated by signal SIGABRT (Abort)
  /Users/runner/work/_temp/e765fe65-ff66-46a8-84ea-064701d7180d.sh: line 1:  2232 Abort trap: 6           bun run test
  Error: Process completed with exit code 134.

locally it's working

❯ bun test test/plugins/ladle.test.ts
bun test v1.1.18 (5a0b9352)

test/plugins/ladle.test.ts:
✓ Find dependencies with the ladle plugin [135.67ms]

 1 pass
 0 fail
Ran 1 tests across 1 files. [473.00ms]

@webpro
Copy link
Collaborator

webpro commented Jul 16, 2024

Yeah, crazy. I'll take a look tonight.

@webpro
Copy link
Collaborator

webpro commented Jul 17, 2024

The issue seems to be with Bun 1.1.19, 1.1.20 handles it fine.

@webpro
Copy link
Collaborator

webpro commented Jul 17, 2024

But bun 1.1.20 has another issue, so I'm afraid we'll have to wait for a fix. Still no idea what eventually causes the issue in this PR 🤷‍♂️

@webpro webpro merged commit ebd79d4 into webpro-nl:main Jul 22, 2024
17 checks passed
@alecmev
Copy link

alecmev commented Jul 23, 2024

This is great, thanks! 👍

@webpro
Copy link
Collaborator

webpro commented Jul 23, 2024

Thanks to @justb3a! There was an issue with the release script, apparently the release notes were too long, but this PR is part of v5.27.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants