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

[Angular] Storybook doesn't work with non-relative imports #2718

Closed
user753 opened this issue Jan 11, 2018 · 12 comments
Closed

[Angular] Storybook doesn't work with non-relative imports #2718

user753 opened this issue Jan 11, 2018 · 12 comments

Comments

@user753
Copy link

user753 commented Jan 11, 2018

Non-relative imports like this

import {TestComponent} from "app/test/test.component"

doesn't work in stories file

stories/index.stories.ts
Module not found: Error: Can't resolve 'app/test/test.component' in 'stories'

I can fix it by using relative path

import {TestComponent} from "../src/app/test/test.component"

but if test.component has non-relative import it would have the same error

storybook/angular 3.4.0-alpha.1

@user753 user753 changed the title [Angular] Storybook doesn't work with baseUrl in tsconfig [Angular] Storybook doesn't work with non-relative imports Jan 15, 2018
@johnslay
Copy link

johnslay commented Jan 30, 2018

Same thing is happening with create-react-app.

Edit:

Resolved the CRA issue by adding NODE_PATH=src in my package.json

 "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject",
    "storybook": "NODE_PATH=src start-storybook -p 9009 -s public",
    "build-storybook": "build-storybook -s public"
  }

Although it would be nice if it read from my .env file.

@igor-dv
Copy link
Member

igor-dv commented Jan 30, 2018

By "non-relative imports", you mean paths mapping in tsconfig.json?

@tigredonorte
Copy link

tigredonorte commented Jan 30, 2018

I have the same problem: "Module not found: Error: Can't resolve '@gorila/core/utils'"

in tsconfig.json:
{ ... "paths": { "@env/*": ["src/environments/*"], } ... }

@igor-dv
Copy link
Member

igor-dv commented Jan 30, 2018

A possible workaround could be this - #2616 (comment)
I've seen some other solutions, for example adding plugin - https://github.com/s-panferov/awesome-typescript-loader#advanced-path-resolution-in-typescript-20

I think we will need to add something like this by default to storybook.

@morninng
Copy link

I have set following code in webpack.config.js

const path = require('path');
const genDefaultConfig = require('@storybook/angular/dist/server/config/defaults/webpack.config.js');
module.exports = (baseConfig, env) => {
  const config = genDefaultConfig(baseConfig, env);
  const lib_path = path.join( __dirname ,  "../src/lib" );
  config.resolve.alias['@lib-path'] = lib_path;
  return config;
};

this setting has solved the issue.

#3258
Please see this in detail

@DmitryEfimenko
Copy link

The workaround by @morninng almost worked with few adjustments:
I'm using @storybook/angular of version 4.0.0-alpha.10. Because of that, the following line of code didn't work:

const genDefaultConfig = require('@storybook/angular/dist/server/config/defaults/webpack.config.js');

This file does not exist.
I had to patch baseConfig object and adjust baseConfig.entry.styles. For some reason the path there was not right.

@igor-dv igor-dv removed the inactive label Jun 28, 2018
@storybookjs storybookjs deleted a comment from stale bot Jun 28, 2018
@storybookjs storybookjs deleted a comment from stale bot Jun 28, 2018
@storybookjs storybookjs deleted a comment from stale bot Jun 28, 2018
@igor-dv
Copy link
Member

igor-dv commented Jun 28, 2018

@DmitryEfimenko , can you please share your result for sake of clarity

@DmitryEfimenko
Copy link

DmitryEfimenko commented Jun 28, 2018

sure, below is the final webpack.config.js that worked for me. It's more of a generalized solution, which looks for tsconfig.json file, parses its options and applies them to webpack config:

const path = require('path');
var fs = require("fs");
const findUp = require('find-up');

module.exports = (baseConfig) => {
  const tsFile = findUp.sync('tsconfig.json');
  const tsConfig = JSON.parse(fs.readFileSync(tsFile));
  const root = path.join(path.dirname(tsFile), tsConfig.compilerOptions.baseUrl);
  if (!baseConfig.resolve.alias) {
    baseConfig.resolve.alias = {};
  }
  for (const prop in tsConfig.compilerOptions.paths) {
    // expects something like: "@pwa/*": ["projects/pwa/src/app/*"]
    const val = tsConfig.compilerOptions.paths[prop][0];
    const alias = prop.substring(0, prop.length - 2);
    const resolvePath = val.substring(0, val.length - 2);
    baseConfig.resolve.alias[alias] = path.join(root, resolvePath);
  }
  
  baseConfig.entry.styles[0] = baseConfig.entry.styles[0].replace('\\projects\\pwa\\projects\\pwa', '\\projects\\pwa');
  return baseConfig;
};

Notice the shenanigans with baseConfig.entry.styles[0]. This is due to the issue where original value looks something like this: C:\Source\PM-for-random-tests\client-ng6\projects\pwa\projects\pwa\src\css\styles.scss. notice the double projects\pwa. I have no idea why that's the case

@DmitryEfimenko
Copy link

By the way, I think I understand why the issue with styles came around. It's probably worth opening a new issue specifically for that. Anyhow, the command I run to start storybook is:

start-storybook -p 9001 -c projects/pwa/.storybook

As you can see, my project is in projects/pwa, but storybook thinks it's in the current working directory.
This issue will be more relevant for ng6 projects which support monorepo style libraries.

@sparqueur
Copy link

Thanks @DmitryEfimenko

Here is a storybook 5.1 version equivalent (except for the 'style' part) :

const path = require('path');
var fs = require("fs");
const findUp = require('find-up');

module.exports = ({ config, mode }) => {
  const tsFile = findUp.sync('tsconfig.json');
  const tsConfig = JSON.parse(fs.readFileSync(tsFile));
  const root = path.join(path.dirname(tsFile), tsConfig.compilerOptions.baseUrl);
  if (!config.resolve.alias) {
    config.resolve.alias = {};
  }
  for (const prop in tsConfig.compilerOptions.paths) {
    // expects something like: "@pwa/*": ["projects/pwa/src/app/*"]
    const val = tsConfig.compilerOptions.paths[prop][0];
    const alias = prop.substring(0, prop.length - 2);
    const resolvePath = val.substring(0, val.length - 2);
    config.resolve.alias[alias] = path.join(root, resolvePath);
  }
  return config;
}

@sparqueur
Copy link

MMM, why do I still have to do this ?
According to @igor-dv 's merge request it should be native, no ?

@ghost
Copy link

ghost commented Oct 18, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants