Skip to content

Commit

Permalink
feat(solution): support storybook
Browse files Browse the repository at this point in the history
  • Loading branch information
JSerFeng committed Sep 6, 2023
1 parent 99fbf3b commit af81f97
Show file tree
Hide file tree
Showing 37 changed files with 3,086 additions and 388 deletions.
7 changes: 7 additions & 0 deletions .changeset/lovely-insects-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@modern-js/storybook': patch
'@modern-js/storybook-builder': patch
---

feat: add storybook-framework
feat: 支持 storybook-framework
2 changes: 2 additions & 0 deletions packages/builder/builder-rspack-provider/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type {

// Hook Callback Types
ModifyRspackConfigFn,
ModifyRspackConfigUtils,

// Plugin Types
BuilderPluginAPI,
Expand All @@ -17,4 +18,5 @@ export type {
Rspack,
Compiler as RspackCompiler,
MultiCompiler as RspackMultiCompiler,
RspackConfig,
} from './types';
1 change: 1 addition & 0 deletions packages/cli/core/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './createLoadedConfig';
export * from './createResolvedConfig';
export * from './createDefaultConfig';
export { loadConfig } from './loadConfig';
1 change: 0 additions & 1 deletion packages/cli/core/src/createCli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,5 @@ export const createCli = () => {
test,
runCommand,
getPrevInitOptions: () => initOptions,
getRunner: () => hooksRunner,
};
};
5 changes: 2 additions & 3 deletions packages/cli/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { createCli } from './createCli';

export * from './types';
export * from './config';
export * from './loadEnv';
export * from './loadPlugins';
export * from '@modern-js/plugin';
export { mergeConfig, initAppDir, checkIsDuplicationPlugin } from './utils';
export { loadConfig } from './config';
export { mergeConfig, initAppDir } from './utils';
export { manager, createPlugin, registerHook } from './manager';
export {
AppContext,
Expand Down
15 changes: 12 additions & 3 deletions packages/runtime/plugin-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
".": {
"types": "./dist/types/index.d.ts",
"jsnext:source": "./src/index.ts",
"node": "./dist/cjs/index.js",
"default": "./dist/esm/index.js"
},
"./types": "./types/index.d.ts",
Expand All @@ -36,16 +37,19 @@
"./loadable": {
"types": "./dist/types/exports/loadable.d.ts",
"jsnext:source": "./src/exports/loadable.ts",
"node": "./dist/cjs/exports/loadable.js",
"default": "./dist/esm/exports/loadable.js"
},
"./head": {
"types": "./dist/types/exports/head.d.ts",
"jsnext:source": "./src/exports/head.ts",
"node": "./dist/cjs/exports/head.js",
"default": "./dist/esm/exports/head.js"
},
"./styled": {
"types": "./dist/types/exports/styled.d.ts",
"jsnext:source": "./src/exports/styled.ts",
"node": "./dist/cjs/exports/styled.js",
"default": "./dist/esm/exports/styled.js"
},
"./server": {
Expand All @@ -69,34 +73,39 @@
"./ssr": {
"types": "./dist/types/ssr/index.d.ts",
"jsnext:source": "./src/ssr/index.ts",
"node": "./dist/esm/ssr/index.node.js",
"node": "./dist/cjs/ssr/index.node.js",
"worker": "./dist/esm/ssr/index.node.js",
"default": "./dist/esm/ssr/index.js"
},
"./model": {
"types": "./types/model.d.ts",
"jsnext:source": "./src/state/index.ts",
"node": "./dist/cjs/state/index.js",
"default": "./dist/esm/state/index.js"
},
"./cli": {
"types": "./dist/types/cli/index.d.ts",
"jsnext:source": "./src/cli/index.ts",
"default": "./dist/cjs/cli/index.js"
"node": "./dist/cjs/cli/index.js",
"default": "./dist/esm/cli/index.js"
},
"./router": {
"types": "./dist/types/router/index.d.ts",
"jsnext:source": "./src/router/index.ts",
"node": "./dist/cjs/router/index.js",
"default": "./dist/esm/router/index.js"
},
"./router/server": {
"types": "./dist/types/router/runtime/server.d.ts",
"jsnext:source": "./src/router/runtime/server.ts",
"node": "./dist/cjs/router/runtime/server.js",
"default": "./dist/esm/router/runtime/server.js"
},
"./loadable-bundler-plugin": {
"types": "./dist/types/ssr/cli/loadable-bundler-plugin.d.ts",
"jsnext:source": "./src/ssr/cli/loadable-bundler-plugin.ts",
"default": "./dist/cjs/ssr/cli/loadable-bundler-plugin.js"
"node": "./dist/cjs/ssr/cli/loadable-bundler-plugin.js",
"default": "./dist/esm/ssr/cli/loadable-bundler-plugin.js"
}
},
"typesVersions": {
Expand Down
2 changes: 1 addition & 1 deletion packages/storybook/builder/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
extends: ['@modern-js'],
ignorePatterns: ['compiled/', 'fixtures/**', 'tests/**', 'vitest.config.ts'],
ignorePatterns: ['compiled/', 'fixtures/**', 'tests/**', 'modern.config.ts'],
parserOptions: {
project: require.resolve('./tsconfig.json'),
},
Expand Down
57 changes: 46 additions & 11 deletions packages/storybook/builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"types": "./src/index.ts",
"scripts": {
"build": "tsc",
"dev": "tsc --watch",
"dev": "tsc --watch --incremental",
"test": "vitest run"
},
"exports": {
Expand All @@ -21,11 +21,9 @@
"default": "./dist/index.js",
"types": "./dist/index.d.ts"
},
"./preset": {
"default": "./dist/preset.js",
"jsnext:source": "./src/preset.ts",
"types": "./dist/preset.d.ts"
},
"./preset": "./dist/preset.js",
"./templates/preview.ejs": "./templates/preview.ejs",
"./templates/virtualModuleModernEntry.js.handlebars": "./templates/virtualModuleModernEntry.js.handlebars",
"./package.json": "./package.json"
},
"engines": {
Expand All @@ -36,19 +34,56 @@
"license": "MIT",
"dependencies": {
"@modern-js/builder": "workspace:*",
"@modern-js/builder-rspack-provider": "workspace:*",
"@modern-js/builder-shared": "workspace:*",
"@modern-js/builder-webpack-provider": "workspace:*",
"@modern-js/core": "workspace:*",
"@modern-js/runtime": "workspace:*",
"@modern-js/utils": "workspace:*",
"@rspack/dev-client": "0.2.12",
"@storybook/addons": "^6.3.7",
"@storybook/api": "^7.4.0",
"@storybook/components": "^7.4.0",
"@storybook/core-common": "^7.4.0",
"@storybook/csf-plugin": "^7.4.0",
"@storybook/docs-tools": "^7.4.0",
"@storybook/global": "^5.0.0",
"@storybook/manager-api": "^7.4.0",
"@storybook/mdx1-csf": "^1.0.0",
"@storybook/mdx2-csf": "^1.1.0",
"@storybook/preview": "^7.4.0",
"@storybook/react-docgen-typescript-plugin": "^1.0.1",
"@storybook/router": "^7.4.0",
"@storybook/theming": "^7.4.0",
"@types/webpack-hot-middleware": "^2.25.6",
"ast-types": "^0.14.2",
"babel-plugin-react-docgen": "^4.2.1",
"glob-promise": "^6.0.3",
"@storybook/core-common": "7.3.2",
"webpack-dev-middleware": "6.0.1",
"react-docgen": "6.0.0-alpha.3",
"remark-external-links": "^9.0.1",
"remark-slug": "^7.0.1",
"tinypool": "^0.8.0",
"webpack-dev-middleware": "6.1.1",
"webpack-hot-middleware": "^2.25.4"
},
"devDependencies": {
"@storybook/types": "^7.3.2"
"@storybook/types": "^7.4.0",
"@types/babel__core": "^7.20.1",
"@types/express": "^4.17.13",
"@scripts/build": "workspace:*",
"@modern-js/builder-rspack-provider": "workspace:*",
"@modern-js/builder-webpack-provider": "workspace:*"
},
"peerDependencies": {
"@modern-js/builder-rspack-provider": "workspace:*",
"@modern-js/builder-webpack-provider": "workspace:*",
"react": "*"
},
"peerDependenciesMeta": {
"@modern-js/builder-rspack-provider": {
"optional": true
},
"@modern-js/builder-webpack-provider": {
"optional": true
}
},
"publishConfig": {
"registry": "https://registry.npmjs.org/",
Expand Down
56 changes: 56 additions & 0 deletions packages/storybook/builder/src/addons/components/modern.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import React from 'react';
import { createApp } from '@modern-js/runtime';
import type { Plugin, RouterConfig } from '@modern-js/runtime';
import router from '@modern-js/runtime/router';
import state from '@modern-js/runtime/model';
import type { StoryFn as StoryFunction } from '@storybook/addons';
import type { IConfig } from '../type';

export const WrapProviders = (
storyFn: StoryFunction<JSX.Element>,
config: IConfig,
) => {
const App = createApp({
plugins: resolvePlugins(config.modernConfigRuntime),
})(storyFn);

return <App />;
};

const allowedRuntimeAPI = {
router: 'router',
state: 'state',
};
const allowedRuntimeAPIValues = Object.values(allowedRuntimeAPI);

export const resolvePlugins = (runtime: IConfig['modernConfigRuntime']) => {
const plugins: Plugin[] = [];

if (!runtime) {
return plugins;
}

Object.keys(runtime).forEach(api => {
if (allowedRuntimeAPIValues.includes(api)) {
if (api === allowedRuntimeAPI.state) {
if (typeof runtime.state === 'boolean') {
if (runtime.state) {
plugins.push(state({}));
}
} else if (typeof runtime.state === 'object') {
plugins.push(state(runtime.state));
}
} else if (api === allowedRuntimeAPI.router) {
// TODO: React Router v6 is not supported yet
plugins.push(
router({
...{ serverBase: ['/'] },
...(runtime.router as RouterConfig),
}),
);
}
}
});

return plugins;
};
1 change: 1 addition & 0 deletions packages/storybook/builder/src/addons/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const ADDON_ID = 'storybook/modern-runtime';
8 changes: 8 additions & 0 deletions packages/storybook/builder/src/addons/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
declare const module: any;

if (module?.hot?.decline) {
module.hot.decline();
}

// make it work with --isolatedModules
export default {};
7 changes: 7 additions & 0 deletions packages/storybook/builder/src/addons/preset/preview.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// import { withPluginRuntime } from '../withPluginRuntime';

// FIXME: @modern-js/runtime now is ESM only, but this package
// currently focus on CommonJS, so it cannot import @modern-js/runtime
// directly.
// export const decorators = [withPluginRuntime];
export const decorators = [];
4 changes: 4 additions & 0 deletions packages/storybook/builder/src/addons/type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface IConfig {
modernConfigRuntime: any;
modernConfigDesignToken: any;
}
20 changes: 20 additions & 0 deletions packages/storybook/builder/src/addons/withPluginRuntime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { type StoryFn as StoryFunction, useParameter } from '@storybook/addons';
import { WrapProviders } from './components/modern';
import type { IConfig } from './type';

export const withPluginRuntime = (
storyFn: StoryFunction<JSX.Element>,
// context: StoryContext
) => {
const modernConfigRuntime = useParameter<IConfig['modernConfigRuntime']>(
'modernConfigRuntime',
);
const modernConfigDesignToken = useParameter<
IConfig['modernConfigDesignToken']
>('modernConfigDesignToken');

return WrapProviders(storyFn, {
modernConfigRuntime: modernConfigRuntime || {},
modernConfigDesignToken,
});
};
Loading

0 comments on commit af81f97

Please sign in to comment.