-
-
Notifications
You must be signed in to change notification settings - Fork 322
Add ladle plugin #728
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
Merged
Merged
Add ladle plugin #728
Changes from 7 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
733e0a7
Add ladle plugin
justb3a 8beeb70
Remove head.html from entry files since it is not supported
justb3a 66f9edb
Use same defaults as ladle does
justb3a 1bfc42d
Handle specific vite config as config file
justb3a 4025e7f
Extend ladle fixtures
justb3a c813e69
Use vitest resolve config
justb3a 67c4e87
Add custom .ladle config
justb3a cfefa56
Fix local vite config path
justb3a 0f5ae20
Fix ladle tests
justb3a 1bf1838
Organize imports
justb3a c8c051e
Minor refactor ladle plugin
webpro 452c099
Merge branch 'main' into feat/plugin-ladle
webpro 00c7af2
Try bun 1.1.20
webpro fff5023
Minor refactor
webpro 52b34c7
Try latest bun
webpro 9ca4e9a
Add `upgrade` bun command to binary resolver
webpro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 @@ | ||
| import { type GlobalProvider } from "@ladle/react"; | ||
| import React from "react"; | ||
|
|
||
| // For details see https://ladle.dev/docs/providers | ||
| export const Provider: GlobalProvider = ({ children }) => <div>{children}</div>; |
This file contains hidden or 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,6 @@ | ||
| // For details see https://ladle.dev/docs/config | ||
| /** @type {import('@ladle/react').UserConfig} */ | ||
| export default { | ||
| stories: "app/**/*.stories.{tsx,mdx}", | ||
| viteConfig: "./.ladle/vite.config.ts", | ||
| }; |
This file contains hidden or 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,7 @@ | ||
| import { defineConfig } from "vite"; | ||
|
|
||
| export default defineConfig({ | ||
| server: { | ||
| open: false, | ||
| }, | ||
| }); |
39 changes: 39 additions & 0 deletions
39
packages/knip/fixtures/plugins/ladle/app/basic.stories.tsx
This file contains hidden or 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,39 @@ | ||
| export const Hello = () => <h2>Simple Story</h2>; | ||
|
|
||
| export const Responsive = () => { | ||
| return ( | ||
| <> | ||
| <div | ||
| style={{ | ||
| width: '100', | ||
| background: '#000', | ||
| color: '#FFF', | ||
| padding: '32px 32px', | ||
| border: '1px solid black', | ||
| fontFamily: 'arial', | ||
| fontSize: 28, | ||
| }} | ||
| > | ||
| Header | ||
| </div> | ||
| <button | ||
| style={{ | ||
| padding: '16px 102px', | ||
| fontFamily: 'arial', | ||
| fontSize: 22, | ||
| margin: 32, | ||
| borderRadius: 8, | ||
| color: '#174291', | ||
| border: '2px solid #174291', | ||
| background: '#FFF', | ||
| }} | ||
| > | ||
| Ladle v4 | ||
| </button> | ||
| </> | ||
| ); | ||
| }; | ||
| Responsive.meta = { | ||
| width: 'xsmall', | ||
| }; | ||
|
|
37 changes: 37 additions & 0 deletions
37
packages/knip/fixtures/plugins/ladle/app/control.stories.tsx
This file contains hidden or 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 { Story } from "@ladle/react"; | ||
|
|
||
| export const Controls: Story<{ | ||
| label: string; | ||
| disabled: boolean; | ||
| count: number; | ||
| colors: string[]; | ||
| variant: string; | ||
| size: string; | ||
| }> = ({ count, disabled, label, colors, variant, size }) => ( | ||
| <> | ||
| <p>Count: {count}</p> | ||
| <p>Disabled: {disabled ? "yes" : "no"}</p> | ||
| <p>Label: {label}</p> | ||
| <p>Colors: {colors.join(",")}</p> | ||
| <p>Variant: {variant}</p> | ||
| <p>Size: {size}</p> | ||
| </> | ||
| ); | ||
|
|
||
| Controls.args = { | ||
| label: "Hello world", | ||
| disabled: false, | ||
| count: 2, | ||
| colors: ["Red", "Blue"], | ||
| }; | ||
| Controls.argTypes = { | ||
| variant: { | ||
| options: ["primary", "secondary"], | ||
| control: { type: "radio" }, | ||
| defaultValue: "primary", | ||
| }, | ||
| size: { | ||
| options: ["small", "medium", "big", "huuuuge"], | ||
| control: { type: "select" }, | ||
| }, | ||
| }; |
This file contains hidden or 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,19 @@ | ||
| { | ||
| "name": "@fixtures/ladle", | ||
| "type": "module", | ||
| "scripts": { | ||
| "dev": "ladle serve --port 4123", | ||
| "build": "ladle build" | ||
| }, | ||
| "dependencies": { | ||
| "@ladle/react": "*", | ||
| "react": "*", | ||
| "react-dom": "*" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/node": "*", | ||
| "@types/react": "*", | ||
| "@types/react-dom": "*", | ||
| "typescript": "*" | ||
| } | ||
| } |
This file contains hidden or 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,20 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "target": "ESNext", | ||
| "useDefineForClassFields": true, | ||
| "lib": ["DOM", "DOM.Iterable", "ESNext"], | ||
| "allowJs": false, | ||
| "skipLibCheck": false, | ||
| "esModuleInterop": false, | ||
| "allowSyntheticDefaultImports": true, | ||
| "strict": true, | ||
| "forceConsistentCasingInFileNames": true, | ||
| "module": "ESNext", | ||
| "moduleResolution": "node", | ||
| "resolveJsonModule": true, | ||
| "isolatedModules": true, | ||
| "noEmit": true, | ||
| "jsx": "react-jsx" | ||
| }, | ||
| "include": ["src"] | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,43 @@ | ||
| import type { EnablerPatterns } from '#p/types/config.js'; | ||
| import type { IsPluginEnabled, Plugin, ResolveConfig, ResolveEntryPaths } from '#p/types/plugins.js'; | ||
| import { hasDependency, load } from '#p/util/plugin.js'; | ||
| import type { LadleConfig } from './types.js'; | ||
| import { toEntryPattern } from '../../util/protocols.js'; | ||
| import { resolveConfig as resolveVitestConfig } from '../vitest/index.js'; | ||
|
|
||
| // https://ladle.dev/docs/config | ||
|
|
||
| const title = 'ladle'; | ||
|
|
||
| const enablers: EnablerPatterns = [/^@ladle\//]; | ||
|
|
||
| const isEnabled: IsPluginEnabled = ({ dependencies }) => hasDependency(dependencies, enablers); | ||
|
|
||
| const config = ['.ladle/config.{mjs,js,ts}']; | ||
|
|
||
| const stories: string[] = ['src/**/*.stories.{js,jsx,ts,tsx,mdx}']; | ||
| const restEntry: string[] = ['.ladle/components.{js,jsx,ts,tsx}']; | ||
| const entry: string[] = [...restEntry, ...stories]; | ||
|
|
||
| const project = ['.ladle/**/*.{js,jsx,ts,tsx}']; | ||
|
|
||
| const resolveEntryPaths: ResolveEntryPaths<LadleConfig> = async localConfig => { | ||
| const localStories = typeof localConfig.stories === 'string' ? [localConfig.stories] : localConfig.stories; | ||
| const patterns = [...restEntry, ...(localStories ?? stories)]; | ||
|
|
||
| return patterns.map(toEntryPattern); | ||
| }; | ||
|
|
||
| const resolveConfig: ResolveConfig<LadleConfig> = async (localConfig, options) => | ||
| localConfig.viteConfig ? resolveVitestConfig(await load(localConfig.viteConfig), options) : []; | ||
|
|
||
| export default { | ||
| title, | ||
| enablers, | ||
| isEnabled, | ||
| config, | ||
| entry, | ||
| project, | ||
| resolveEntryPaths, | ||
| resolveConfig, | ||
| } satisfies Plugin; | ||
This file contains hidden or 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,4 @@ | ||
| export type LadleConfig = { | ||
| stories?: string | string[]; | ||
| viteConfig?: string; | ||
| }; |
This file contains hidden or 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,31 @@ | ||
| import { test } from 'bun:test'; | ||
| import assert from 'node:assert/strict'; | ||
| import { main } from '../../src/index.js'; | ||
| import { resolve } from '../../src/util/path.js'; | ||
| import baseArguments from '../helpers/baseArguments.js'; | ||
| import baseCounters from '../helpers/baseCounters.js'; | ||
|
|
||
| const cwd = resolve('fixtures/plugins/ladle'); | ||
|
|
||
| test('Find dependencies with the ladle plugin', async () => { | ||
| const { issues, counters } = await main({ | ||
| ...baseArguments, | ||
| cwd, | ||
| }); | ||
|
|
||
| assert(issues.dependencies['package.json']['@ladle/react']); | ||
| assert(issues.dependencies['package.json']['react']); | ||
| assert(issues.dependencies['package.json']['react-dom']); | ||
| assert(issues.devDependencies['package.json']['@types/react']); | ||
| assert(issues.devDependencies['package.json']['@types/react-dom']); | ||
| assert(issues.binaries['package.json']['ladle']); | ||
|
|
||
| assert.deepEqual(counters, { | ||
| ...baseCounters, | ||
| binaries: 1, | ||
| dependencies: 3, | ||
| devDependencies: 2, | ||
| processed: 0, | ||
| total: 0, | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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 :)