Skip to content

Less files importing other less files #47

Closed
@BasilPP

Description

@BasilPP

Describe the bug
When a .less file imports another .less file with a relative path, an error occurs.
When a .less file imports another .less file with an absolute path, an error occurs.

I tried logging the error less.render was giving:
FileError: './constants.module.less' wasn't found. Tried - ./constants.module.less,c:\dev\app\constants.module.less,constants.module.less in input on line 1, column 1: 1 @import "./constants.module.less";

To Reproduce
Have a .less file @import another less file, e.g. :

@import "./constants.module.less";                              // relative
@import "../../site/style-helpers/constants.module.less";	// relative
@import "site/style-helpers/constants.module.less";		// absolute

Expected behaviour
.less imports to not break.

Desktop (please complete the following information):
OS - windows

Additional context
In both the relative and absolute path import cases, I think this issue is in DtsSnapshotCreator.ts, when the method getClasses is called. This method calls less.render to render the less file to a string.

Relative import case:

  • logging the error seems to imply that the import is happening relative to the apps root directory, and not relative to the less file.
  • as an idea for a solution, less.render allows for a paths parameter, could the less file’s directory be passed to less.render to help with relative path imports?

Absolute import case:

  • I’ve setup my webpack configuration to allow for absolute paths. The plugin understandably has no way of knowing about this webpack setup.
{
    loader: "less-loader",
    options: {
        paths: [path.resolve(__dirname, "ClientApp")],
    }
}
  • Ideas for a solution:
    o pass tsconfig’s baseurl to less.render?
    o a plugin parameter to set root paths?

Unfortunately both ideas have some flaws, I'm not sure if there is a better way to achieve absolute imports?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions