question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use LESS files

See original GitHub issue

I would like to use less files in project.

UPDATE - Solution:

  • install npm dependencies: npm install --save-dev less less-loader
  • add less extension to resolve.extensions section:
resolve: {
    // ensure loader extensions match
    extensions: ['','.ts','.js','.json', '.less', '.css', '.html']
  },
  • add a loader like: { test: /.less$/, exclude: /node_modules/, loader: 'raw-loader!less-loader' }
  • and now can use like:
@Component({
  styles: [ require('./filename.less') ],
...
})

The problem:

I tried this config in webpack.config.js:

{ test: /\.less$/, loader: "raw-loader!less-loader"}

and use it as home.ts:

@Component({
  selector: 'home',
  directives: [ FORM_DIRECTIVES ],
  pipes: [],
  styles: [ require('./home.less') ],
  template: require('./home.html')
})

but when I try to npm start, I got the following error:

ERROR in ./src/app/home/home.less
Module build failed: visitors[i].run is not a function
 @ /home/ubuntu/workspace/browserclient/src/app/home/home.less (line null, column -1)
 near lines:
 @ ./src/app/home/home.ts 23:21-43

My less file is very basic (copy it from lesscss.org startpage)

@base: #f938ab;

.box-shadow(@style, @c) when (iscolor(@c)) {
  -webkit-box-shadow: @style @c;
  box-shadow:         @style @c;
}
.box-shadow(@style, @alpha: 50%) when (isnumber(@alpha)) {
  .box-shadow(@style, rgba(0, 0, 0, @alpha));
}
.box {
  color: saturate(@base, 5%);
  border-color: lighten(@base, 30%);
  div { .box-shadow(0 0 5px, 30%) }
}

Thanks

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:2
  • Comments:24 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
matthewlsawyercommented, Mar 9, 2016

I found an issue with the raw-loader loading less styles into a component. Images, references by the less source via a url will not be resolved by webpack.

For example, the component:

@Component({
    selector: 'test',
    template: require('./template.html'),
    styles: [require('./style.less')],
    directives: [...],
    pipes: [...],
    providers: [...]
})

And in style.less:

.my-background {
    background-image: url('./my-image.png');
}

Webpack config:

{test: /\.less$/, loader: 'raw-loader!less-loader'},
{test: /\.(png|jpg|gif)$/, loader: 'file-loader'}

The raw-loader will not try to resolve this url, that seems to be the responsibility of the css-loader. As far as I’m aware the css-loader doesn’t work with angular components.

4reactions
JunkyDeLuxecommented, Apr 6, 2017

Nobody had fixed this issue ? styleUrls: ['./login.component.less'],

and in webpack use: [‘raw-loader’, ‘css-loader’, ‘less-loader’]

Same error chrome “display Uncaught Error: Expected ‘styles’ to be an array of strings.”

Read more comments on GitHub >

github_iconTop Results From Across the Web

Less.js: Getting started
Less (which stands for Leaner Style Sheets) is a backwards-compatible language extension for CSS. This is the official documentation for Less, the language...
Read more >
LESS CSS - Beginner's Guide - Hongkiat
LESS comes with a less.js file which is really easy to deploy in your website. Create a stylesheet with .less extension and link...
Read more >
How to create a LESS file and how to compile it - GeeksforGeeks
Step 1: Go to your project folder, create a subfolder named CSS and then create a file named styles.less inside it.
Read more >
What is a Less file? - Stack Overflow
Less is basically a CSS-like language with additional capabilities that can be compiled to CSS using a preprocessor.
Read more >
Less CSS Pre-Processor Tutorial - YouTube
... look at all of the basics and fundamentals of the Less CSS pre-processor/pre-compiler. Less gives you powerful features to take advanta.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found