Skip to content

Commit

Permalink
refactor: rework imports in preview virtual module to fix Yarn 2 comp…
Browse files Browse the repository at this point in the history
…atibility

Using ES6 import directly in `preview.js-generated-config-entry.js` module definition is causing the following error when using Yarn 2:
```
ERROR in ./.storybook/preview.js-generated-config-entry.js
Module not found: Error: Something that got detected as your top-level application (because it doesn't seem to belong to any package) tried to access a package that is not declared in your dependencies

Required package: @storybook/client-api (via "@storybook/client-api")
Required by: /Users/dev/project/.storybook/preview.js-generated-config-entry.js
```
This is because the code of the virtual module is importing @storybook/client-api and @storybook/client-logger.
When the code is executed these packages are required in the context of SB user project and not in the one of `@storybook/core` causing Yarn 2 to throw.
To avoid this, we are now resolving theses deps directly when webpack config is built (so in `@storybook/core` context) and then using the resolved values in the virtual module.

The change is done only for PnP users to avoid issues when running SB examples available in this monorepo, if examples are extracted somewhere else the `require.resolve` behavior can be applied in a non PnP situation.
  • Loading branch information
gaetanmaisse committed Apr 6, 2020
1 parent d4027f0 commit 5f9f8a1
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/core/src/server/preview/iframe-webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,18 @@ export default ({
const match = entryFilename.match(/(.*)-generated-(config|other)-entry.js$/);
if (match) {
const configFilename = match[1];

const IS_USING_YARN_PNP = typeof process.versions.pnp !== 'undefined';
const clientApi = IS_USING_YARN_PNP
? `${require.resolve('@storybook/client-api')}`
: '@storybook/client-api';
const clientLogger = IS_USING_YARN_PNP
? `${require.resolve('@storybook/client-logger')}`
: '@storybook/client-logger';

virtualModuleMapping[entryFilename] = `
import { addDecorator, addParameters, addParameterEnhancer } from '@storybook/client-api';
import { logger } from '@storybook/client-logger';
const { addDecorator, addParameters, addParameterEnhancer } = require("${clientApi}")
const { logger } = require("${clientLogger}")
const { decorators, parameters, parameterEnhancers, globalArgs, globalArgTypes, args, argTypes } = require(${JSON.stringify(
configFilename
Expand Down

0 comments on commit 5f9f8a1

Please sign in to comment.