Storybook: Import Less not work.

Created on 9 Feb 2020  路  11Comments  路  Source: storybookjs/storybook

Describe the bug
Can't import less file in JavaScript.

To Reproduce
Steps to reproduce the behavior:

  1. Use Creaet-React-App create a default React template SPA
  2. cd <dir>, then Execute npx -p @storybook/cli sb init
  3. yarn add -D less less-loader
  4. modify .storybook/main.js:
  5. create 'src/stories/temp.less`, then write some style, and import it in story module:
import './temp.less';
````
```less
span{background:black;}
// for test, it makes emojis container background in the template example Button story to be black

Expected behavior
Import Less file successfully into story, then show styles defined by the less file.

Screenshots

I know the webpack doesn't load less finally by devtools

image

However, when I wrote down options.errorFlag = true in rules delibrately, it throw a loader error, which proves it has already parse the imported less file. Why? 馃

image

Code snippets

.storybook/main.js

const fs = require('fs')
const path = require('path')

module.exports = {
  stories: ['../src/**/*.stories.js'],
  addons: [
    '@storybook/preset-create-react-app',
    '@storybook/addon-actions',
    '@storybook/addon-links',
  ],
  webpackFinal: async (config, { configType }) => {
    config.module.rules.push({
      test: /\.less$/,
      use: [
        'style-loader',
        'css-loader',
        'resolve-url-loader',
        {
          loader: 'less-loader',
          options: {
            javascriptEnabled: true,
            sourceMap: true,
          }
        }]
    });
    return config;
  },
};

System:

  • System:

    • OS: Windows 10 10.0.18363

    • CPU: (8) x64 Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz

  • Binaries:

    • Node: 12.14.0 - C:\Program Filesnodejsnode.EXE

    • Yarn: 1.19.2 - C:\Program Filesnodejs\yarn.CMD

    • npm: 6.4.1 - C:\Program Filesnodejs\npm.CMD

  • Browsers:

    • Chrome: 79.0.3945.131

Additional context

  • Storybook Version:

Using the newest stable 5.3.12.

If I change the .less extension to .css, it works! Why not less?

babel / webpack inactive question / support

Most helpful comment

I've been looking for the answers for a similar problem. In @storybook/preset-ant-design it mentions configuring CRA to exclude less from the file loader.

{
  "name": "@storybook/preset-create-react-app",
  "options": {
    "craOverrides": {
      "fileLoaderExcludes": ["less"]
    }
  }
}

May help.

All 11 comments

I find that less file was handled with file-loader or url-loader. But I don't know how to fix this. 馃槩

import styles from './index.less'

console.log(style) // it's a url path

Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 30 days. Thanks!

I've been looking for the answers for a similar problem. In @storybook/preset-ant-design it mentions configuring CRA to exclude less from the file loader.

{
  "name": "@storybook/preset-create-react-app",
  "options": {
    "craOverrides": {
      "fileLoaderExcludes": ["less"]
    }
  }
}

May help.

Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 30 days. Thanks!

I have the same problem. I use index.module.lessmodule file which support in create-react-app 2.

Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 30 days. Thanks!

i have the same problem

Here's an example using craco and semantic UI

module.exports = {
  stories: ['../src/**/*.stories.js'],
  addons: [
    '@storybook/preset-create-react-app',
    '@storybook/addon-actions',
    '@storybook/addon-links',
  ],
  webpackFinal: async (webpackConfig, { configType }) => {
    // `configType` has a value of 'DEVELOPMENT' or 'PRODUCTION'
    // You can change the configuration based on that.
    // 'PRODUCTION' is used when building the static version of storybook.
    const { loadCracoConfig } = require('@craco/craco/lib/config');
    const { getCraPaths } = require('@craco/craco/lib/cra');
    const context = {env: process.env.NODE_ENV};
    const cracoConfig = loadCracoConfig(context);
    context.paths = getCraPaths(cracoConfig);
    console.log(context);
    const {overrideWebpackConfig} = require('@semantic-ui-react/craco-less');
    overrideWebpackConfig({
      context,
      webpackConfig
    });

    // Return the altered config
    return webpackConfig;
  },
};

because cra's file-loader intercept all other file, so the less file can't go into less-loader;
solution is to config the @storybook/preset-create-react-app;
image
there are some diffrent between storebook 5.3.0 and the 5.2.x ; can read https://github.com/storybookjs/presets/tree/master/packages/preset-create-react-app

Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 30 days. Thanks!

Hey there, it's me again! I am going close this issue to help our maintainers focus on the current development roadmap instead. If the issue mentioned is still a concern, please open a new ticket and mention this old one. Cheers and thanks for using Storybook!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

levithomason picture levithomason  路  3Comments

purplecones picture purplecones  路  3Comments

tirli picture tirli  路  3Comments

dnlsandiego picture dnlsandiego  路  3Comments

tomitrescak picture tomitrescak  路  3Comments