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

React storybook: Can't resolve .ts files #2259

Closed
3 tasks done
leota opened this issue Jan 3, 2020 · 4 comments · Fixed by #2270
Closed
3 tasks done

React storybook: Can't resolve .ts files #2259

leota opened this issue Jan 3, 2020 · 4 comments · Fixed by #2270
Labels
outdated scope: react Issues related to React support for Nx type: bug

Comments

@leota
Copy link
Contributor

leota commented Jan 3, 2020

Prerequisites

  • I am running the latest version
  • I checked the documentation (nx.dev) and found no answer
  • I checked to make sure that this issue has not already been filed

Expected Behavior

Being able to import from .ts into .stories.tsx.

Current Behavior

It throws following error while compiling:

ERROR in ./libs/ui/src/lib/components/controls/button.stories.tsx
Module not found: Error: Can't resolve '../../themes' in '/Users/{USERNAME}/Documents/workspace/platform/apps/libs/ui/src/lib/components/controls'
 @ ./libs/ui/src/lib/components/controls/button.stories.tsx 4:0-45 10:29-42
 @ ./libs/ui/src/lib sync \.stories\.tsx?$
 @ ./libs/ui/.storybook/config.js
 @ multi ./node_modules/@storybook/core/dist/server/common/polyfills.js ./node_modules/@storybook/core/dist/server/preview/globals.js ./libs/ui/.storybook/config.js (webpack)-hot-middleware/client.js?reload=true&quiet=true

Steps to Reproduce

This is my project structure:

- ROOT
    - .storybook
    - apps
    -libs
        -ui
            -.storybook
            -src
                -lib
                    -components
                        -controls
                            button.tsx
                            button.stories.tsx
                            index.ts
                    -themes
                        index.ts
                        provider.tsx   
                        my-theme.ts    

This is button.stories.tsx

import React from 'react';
import { action } from '@storybook/addon-actions';
import { UIButton } from './button';
import { ThemeProvider, MyTheme } from '../../themes';

export default { title: 'Button' };

export const withText = () => (
  <ThemeProvider theme={MyTheme}>
    <UIButton onClick={action('clicked')}>Button</UIButton>
  </ThemeProvider>
);

export const withEmoji = () => (
  <UIButton>
    <span role="img" aria-label="so cool">
      😀 😎 👍 💯
    </span>
  </UIButton>
);

This is provider.tsx:

import styled from 'styled-components';
import { Grommet } from 'grommet';

export const ThemeProvider = styled(Grommet)``;

Context

NX report:

 @nrwl/angular : Not Found
  @nrwl/cli : 8.9.0
  @nrwl/cypress : 8.9.0
  @nrwl/eslint-plugin-nx : 8.9.0
  @nrwl/express : 8.9.0
  @nrwl/jest : 8.9.0
  @nrwl/linter : 8.9.0
  @nrwl/nest : Not Found
  @nrwl/next : Not Found
  @nrwl/node : 8.9.0
  @nrwl/react : 8.9.0
  @nrwl/schematics : Not Found
  @nrwl/tao : 8.9.0
  @nrwl/web : 8.9.0
  @nrwl/workspace : 8.9.0
  typescript : 3.4.5
@FrozenPandaz FrozenPandaz added scope: react Issues related to React support for Nx type: bug labels Jan 3, 2020
@leota
Copy link
Contributor Author

leota commented Jan 6, 2020

I fixed it pushing the.ts extension in libs/ui/.storybook/webpack config.js, which now looks like this:

const rootWebpackConfig = require('../../../.storybook/webpack.config');
// Export a function. Accept the base config as the only param.
module.exports = async ({ config, mode }) => {
  config = await rootWebpackConfig({ config, mode });

  config.resolve.extensions.push('.tsx');
  config.resolve.extensions.push('.ts');
  config.module.rules.push({
    test: /\.(ts|tsx)$/,
    loader: require.resolve('babel-loader'),
    options: {
      presets: [
        '@babel/preset-env',
        '@babel/preset-react',
        '@babel/preset-typescript'
      ]
    }
  });
  return config;
};

I'm going to open a PR

@leota leota changed the title React storybook: Can't resolve relative paths React storybook: Can't resolve .ts files Jan 6, 2020
leota added a commit to t-and-f/nx that referenced this issue Jan 6, 2020
…nfiguration template

can import from .ts files in .stories.tsx

ISSUES CLOSED: nrwl#2259
jaysoo pushed a commit that referenced this issue Jan 6, 2020
…nfiguration template (#2270)

can import from .ts files in .stories.tsx

ISSUES CLOSED: #2259
@jaysoo
Copy link
Member

jaysoo commented Jan 6, 2020

@leota Thanks for the fix! It is merged.

@leota
Copy link
Contributor Author

leota commented Jan 7, 2020

@jaysoo great, thank you!

@github-actions
Copy link

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated scope: react Issues related to React support for Nx type: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants