generated from bywhitebird/starter-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from bywhitebird/59-create-the-storybook-plugi…
…n-for-kaz 59 create the storybook plugin for kaz
- Loading branch information
Showing
23 changed files
with
6,112 additions
and
1,107 deletions.
There are no files selected for viewing
This file contains 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 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,33 @@ | ||
import { join, dirname } from "path"; | ||
|
||
/** | ||
* This function is used to resolve the absolute path of a package. | ||
* It is needed in projects that use Yarn PnP or are set up within a monorepo. | ||
*/ | ||
function getAbsolutePath(value) { | ||
return dirname(require.resolve(join(value, "package.json"))); | ||
} | ||
|
||
/** @type { import('@storybook/react-vite').StorybookConfig } */ | ||
const config = { | ||
stories: ["../src/**/*.mdx", "../src/**/*.stories.@(js|jsx|mjs|ts|tsx)"], | ||
addons: [ | ||
getAbsolutePath("@storybook/addon-links"), | ||
getAbsolutePath("@storybook/addon-essentials"), | ||
getAbsolutePath("@storybook/addon-onboarding"), | ||
getAbsolutePath("@storybook/addon-interactions"), | ||
], | ||
framework: { | ||
// @ts-ignore | ||
name: getAbsolutePath("@whitebird/storybook-plugin-kaz-react-vite"), | ||
options: {}, | ||
}, | ||
docs: { | ||
autodocs: "tag", | ||
}, | ||
core: { | ||
// @ts-ignore | ||
builder: getAbsolutePath('@storybook/builder-vite'), | ||
}, | ||
}; | ||
export default config; |
This file contains 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,14 @@ | ||
/** @type { import('@storybook/react').Preview } */ | ||
const preview = { | ||
parameters: { | ||
actions: { argTypesRegex: "^on[A-Z].*" }, | ||
controls: { | ||
matchers: { | ||
color: /(background|color)$/i, | ||
date: /Date$/i, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
export default preview; |
This file contains 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 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
22 changes: 22 additions & 0 deletions
22
examples/simple-kazam-project/src/stories/Button.stories.tsx
This file contains 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,22 @@ | ||
import type { StoryObj } from '@storybook/react' | ||
|
||
import Button from '../components/Button.kaz' | ||
|
||
export default { | ||
title: 'Example/Button', | ||
component: Button, | ||
parameters: { | ||
layout: 'centered', | ||
}, | ||
tags: ['autodocs'], | ||
argTypes: { | ||
callback: { action: 'clicked' }, | ||
}, | ||
} | ||
|
||
export const Primary: StoryObj = { | ||
args: { | ||
buttonText: 'Button', | ||
color: 'red', | ||
}, | ||
} |
This file contains 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 @@ | ||
declare module '*.kaz' { | ||
import type { ComponentType } from 'react' | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const component: ComponentType<any> | ||
export default component | ||
} |
This file contains 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,11 @@ | ||
{ | ||
"$schema": "https://json.schemastore.org/tsconfig", | ||
"display": "Default", | ||
"extends": "@whitebird/tsconfig/node.json", | ||
"compilerOptions": { | ||
"outDir": "dist", | ||
"jsx": "react-jsx" | ||
}, | ||
"include": ["src"], | ||
"exclude": ["node_modules", "dist"] | ||
} |
This file contains 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 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,12 @@ | ||
# @whitebird/kazam-parser-base | ||
|
||
## 0.1.0 | ||
|
||
### Minor Changes | ||
|
||
- 4fd3da1: Initial release | ||
|
||
### Patch Changes | ||
|
||
- Updated dependencies [4fd3da1] | ||
- @whitebird/kazam-transformer-base@0.1.0 |
This file contains 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 { defineBuildConfig } from 'unbuild' | ||
|
||
export default defineBuildConfig({ | ||
declaration: true, | ||
}) |
This file contains 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,36 @@ | ||
{ | ||
"name": "@whitebird/storybook-plugin-kaz-react-vite", | ||
"type": "module", | ||
"version": "0.1.0", | ||
"license": "MIT", | ||
"exports": { | ||
".": { | ||
"import": "./dist/index.mjs", | ||
"require": "./dist/index.cjs" | ||
}, | ||
"./preset": { | ||
"types": "./dist/preset.d.ts", | ||
"require": "./dist/preset.cjs" | ||
}, | ||
"./package.json": "./package.json" | ||
}, | ||
"main": "./dist/index.cjs", | ||
"types": "./dist/index.d.ts", | ||
"files": [ | ||
"dist" | ||
], | ||
"scripts": { | ||
"build": "unbuild", | ||
"lint": "eslint .", | ||
"lint:fix": "eslint . --fix" | ||
}, | ||
"dependencies": { | ||
"@storybook/react-vite": "^7.5.1", | ||
"@whitebird/kazam-parser-kaz": "workspace:*", | ||
"@whitebird/kazam-transformer-react": "workspace:*", | ||
"kazam": "workspace:*" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^18.11.9" | ||
} | ||
} |
This file contains 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 @@ | ||
module.exports = require('./dist/preset') |
This file contains 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 @@ | ||
export {} |
This file contains 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,17 @@ | ||
import type { StorybookConfig } from '@storybook/react-vite' | ||
import { viteFinal as reactViteFinal } from '@storybook/react-vite/preset' | ||
|
||
import { viteKazReactPlugin } from './vite-kaz-react-plugin' | ||
|
||
export * from '@storybook/react-vite/preset' | ||
|
||
export const viteFinal: StorybookConfig['viteFinal'] = async (config, options) => { | ||
if (reactViteFinal !== undefined) | ||
config = await reactViteFinal(config, options) | ||
|
||
const { plugins = [] } = config | ||
|
||
plugins.unshift(viteKazReactPlugin()) | ||
|
||
return config | ||
} |
78 changes: 78 additions & 0 deletions
78
packages/storybook-plugin-kaz-react-vite/src/vite-kaz-react-plugin.ts
This file contains 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,78 @@ | ||
import * as path from 'node:path' | ||
|
||
import { ParserKaz } from '@whitebird/kazam-parser-kaz' | ||
import { TransformerReact } from '@whitebird/kazam-transformer-react' | ||
import * as kazam from 'kazam' | ||
import type { Plugin } from 'vite' | ||
|
||
export const viteKazReactPlugin = (): Plugin => { | ||
const KAZ_REACT_SUFFIX = '?kaz-react' | ||
|
||
const kazComponents: string[] = [] | ||
let kazamGenerationResult: Awaited<ReturnType<typeof kazam.generate>>[0] | undefined | ||
let rootDir: string | undefined | ||
|
||
return { | ||
name: 'vite-kaz-react', | ||
|
||
enforce: 'pre', | ||
|
||
resolveId(source, importer) { | ||
if (importer?.endsWith('/index.html')) | ||
rootDir = importer.slice(0, -'index.html'.length) | ||
|
||
if (source.endsWith('.kaz')) { | ||
const componentPath = path.join(path.dirname(importer ?? ''), source) | ||
|
||
kazComponents.push(componentPath) | ||
return `${componentPath}.tsx${KAZ_REACT_SUFFIX}` | ||
} | ||
|
||
return null | ||
}, | ||
|
||
async load(id) { | ||
// As we provide only one config, kazam.generate will return only one output | ||
if (id.endsWith(KAZ_REACT_SUFFIX)) { | ||
if (rootDir === undefined) | ||
throw new Error('rootDir is undefined') | ||
|
||
kazamGenerationResult ??= await kazam.generate({ | ||
rootDir, | ||
input: kazComponents, | ||
output: rootDir, | ||
parsers: [ParserKaz], | ||
transformers: [TransformerReact], | ||
}).then(([output]) => output) | ||
|
||
if (kazamGenerationResult === undefined) | ||
throw new Error('kazamGenerationResult is undefined') | ||
|
||
const kazComponentId = id.slice(0, -`.tsx${KAZ_REACT_SUFFIX}`.length) | ||
const kazComponentFilename = path.basename(kazComponentId) | ||
|
||
const output = kazamGenerationResult.get(kazComponentFilename)?.content | ||
|
||
if (output === undefined) | ||
throw new Error(`Could not find output for ${kazComponentId}`) | ||
|
||
return output | ||
} | ||
|
||
return null | ||
}, | ||
|
||
handleHotUpdate({ file, server, modules }) { | ||
if (file.endsWith('.kaz')) { | ||
kazamGenerationResult = undefined | ||
|
||
return [ | ||
...(server.moduleGraph.getModulesByFile(`${file}.tsx`) ?? []), | ||
...modules, | ||
] | ||
} | ||
|
||
return modules | ||
}, | ||
} | ||
} |
This file contains 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,10 @@ | ||
{ | ||
"$schema": "https://json.schemastore.org/tsconfig", | ||
"display": "Default", | ||
"extends": "@whitebird/tsconfig/node.json", | ||
"compilerOptions": { | ||
"outDir": "dist" | ||
}, | ||
"include": ["src"], | ||
"exclude": ["node_modules", "dist"] | ||
} |
This file contains 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 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 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 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 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,16 @@ | ||
diff --git a/package.json b/package.json | ||
index 53cb6051f09b624d4c54bfb4a6b388531b90ac88..4fcf4772164a0a2566f1677f1afe6fac4e6a30a9 100644 | ||
--- a/package.json | ||
+++ b/package.json | ||
@@ -32,6 +32,11 @@ | ||
}, | ||
"./package.json": "./package.json" | ||
}, | ||
+ "typesVersions": { | ||
+ "*": { | ||
+ "preset": ["./dist/preset.d.ts"] | ||
+ } | ||
+ }, | ||
"main": "dist/index.js", | ||
"module": "dist/index.mjs", | ||
"types": "dist/index.d.ts", |
Oops, something went wrong.