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

feat(storybook-addon): use new api to get the storybook virtual static modules #3298

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions .changeset/tame-taxis-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@module-federation/storybook-addon': major
---

Use new api to get the virtual static modules
2 changes: 1 addition & 1 deletion packages/storybook-addon/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module.exports = storybookConfig;

### Rsbuild App or Rslib Module

```
```js
import { dirname, join } from 'node:path';
import type { StorybookConfig } from 'storybook-react-rsbuild';

Expand Down
2 changes: 2 additions & 0 deletions packages/storybook-addon/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ export default {
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/packages/storybook-addon',
// https://mswjs.io/docs/migrations/1.x-to-2.x#requestresponsetextencoder-is-not-defined-jest
testEnvironment: 'jest-fixed-jsdom',
};
13 changes: 7 additions & 6 deletions packages/storybook-addon/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@module-federation/storybook-addon",
"version": "3.0.11",
"version": "3.1.0",
"description": "Storybook addon to consume remote module federated apps/components",
"license": "MIT",
"repository": "https://github.com/module-federation/core/tree/main/packages/storybook-addon",
Expand Down Expand Up @@ -48,10 +48,11 @@
"@module-federation/sdk": "workspace:*"
},
"devDependencies": {
"jest-fixed-jsdom": "^0.0.9",
"@module-federation/utilities": "workspace:*",
"@rsbuild/core": "^1.0.19",
"@storybook/core-common": "7.6.20",
"@storybook/node-logger": "7.6.20",
"@storybook/core": "^8.4.6",
"webpack": "5.93.0",
"webpack-virtual-modules": "0.6.2"
},
Expand All @@ -60,10 +61,10 @@
"@module-federation/utilities": "^3.1.31",
"@nx/react": ">= 16.0.0",
"@nx/webpack": ">= 16.0.0",
"@storybook/core-common": "^6.5.16 || ^7.0.0 || ^ 8.0.0",
"@storybook/node-logger": "^6.5.16 || ^7.0.0 || ^ 8.0.0",
"@storybook/core": ">= 8.2.0",
"@storybook/node-logger": "^6.5.16 || ^7.0.0 || ^8.0.0",
"webpack": "^5.75.0",
"webpack-virtual-modules": "^0.5.0 || ^0.6.0"
"webpack-virtual-modules": "0.6.2"
},
"peerDependenciesMeta": {
"@rsbuild/core": {
Expand All @@ -78,7 +79,7 @@
"@nx/webpack": {
"optional": true
},
"@storybook/core-common": {
"@storybook/core": {
"optional": true
},
"@storybook/node-logger": {
Expand Down
22 changes: 9 additions & 13 deletions packages/storybook-addon/src/lib/storybook-addon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@ import fs from 'fs';
import { dirname, join } from 'path';
import * as process from 'process';
import VirtualModulesPlugin from 'webpack-virtual-modules';
import { container, Configuration } from 'webpack';
import { container, type Configuration } from 'webpack';
import { logger } from '@storybook/node-logger';
// NOTE: @storybook/core-common is deprecated while still available, considering importing
// from 'storybook/internal/common' or '@storybook/core'. Considering requires Storybook 8
// at least and change this in the next breaking change version.
import { normalizeStories } from '@storybook/core-common';
import { ModuleFederationPluginOptions } from '@module-federation/utilities';

import { ModuleFederationConfig } from '@nx/webpack';
import { normalizeStories } from '@storybook/core/common';
import withModuleFederation from '../utils/with-module-federation';
import { correctImportPath } from '../utils/correctImportPath';

import type { ModuleFederationPluginOptions } from '@module-federation/utilities';
fyodorovandrei marked this conversation as resolved.
Show resolved Hide resolved
import type { ModuleFederationConfig } from '@nx/webpack';

const { ModuleFederationPlugin } = container;

export type Preset = string | { name: string };
Expand Down Expand Up @@ -70,17 +67,16 @@ export const webpack = async (
);

const index = plugins.findIndex(
//@ts-ignore
(plugin) => plugin.constructor.name === 'VirtualModulesPlugin',
(plugin) => plugin?.constructor.name === 'VirtualModulesPlugin',
);

if (index !== -1) {
logger.info(`=> [MF] Detected plugin VirtualModulesPlugin`);

/* eslint-disable @typescript-eslint/no-explicit-any */
const plugin = plugins[index] as any;
const plugin = plugins[index] as VirtualModulesPlugin;

const virtualEntries = plugin._staticModules; // TODO: Exist another way to get virtual modules? Or maybe it's good idea to open a PR adding a method to get modules?
const virtualEntries: Record<string, string> =
plugin.getModuleList('static');
const virtualEntriesPaths: string[] = Object.keys(virtualEntries);

logger.info(`=> [MF] Write files from VirtualModulesPlugin`);
Expand Down
Loading