GithubHelp home page GithubHelp logo

yhtml5 / less-loader Goto Github PK

View Code? Open in Web Editor NEW

This project forked from webpack-contrib/less-loader

1.0 2.0 0.0 413 KB

less loader module for webpack

License: MIT License

JavaScript 4.52% CSS 95.38% HTML 0.09%

less-loader's Introduction

npm node deps tests coverage chat

LESS Loader

Install

npm install --save-dev less-loader less

Usage

Use less-loader in tandem with css-loader & style-loader to add LESS support for webpack.

Use the loader either via your webpack config, CLI or inline.

Via webpack config (recommended)

webpack.config.js

module.exports = {
  module: {
    rules: [
      {
        test: /\.less$/,
        use: [
          'style-loader',
          { loader: 'css-loader', options: { importLoaders: 1 } },
          'less-loader'
        ]
      }
    ]
  }
}

In your application

import css from 'file.less';

CLI

webpack --module-bind 'less=style-loader!css-loader!less-loader'

In your application

import css from 'file.less';

Inline

In your application

import css from 'style-loader!css-loader!less-loader!./file.less';

Options

You can pass any LESS specific options to less-loader via loader options or query parameters.

See the LESS documentation for all available options. LESS translates dash-case to camelCase. Certain options which take values (e.g. lessc --modify-var="a=b") are better handled with the JSON Syntax

{
  test: /\.less$/,
  use: [
    'style-loader',
    { loader: 'css-loader', options: { importLoaders: 1 } },
    { loader: 'less-loader', options: { strictMath: true, noIeCompat: true } }
	]
}

Plugins

In order to use plugins, simply set the options.lessPlugins-option on your webpack.config.js.

const CleanCSSPlugin = require('less-plugin-clean-css');

{
  test: /\.less$/,
  use: [
    'style-loader',
    { loader: 'css-loader', options: { importLoaders: 1 } },
    {
      loader: 'less-loader',
      options: { LessPlugins: new CleanCSSPlugin({ advanced: true }) }
    }
}

Imports

webpack provides an advanced mechanism to resolve files. The less-loader stubs less' fileLoader and passes all queries to the webpack resolving engine. Thus you can import your less-modules from node_modules. Just prepend them with a ~ which tells webpack to look-up the modules

@import "~bootstrap/less/bootstrap";

It's important to only prepend it with ~, because ~/ resolves to the home-directory. webpack needs to distinguish between bootstrap and ~bootstrap because css- and less-files have no special syntax for importing relative files. Writing @import "file" is the same as @import "./file";

Also please note that for CSS modules, relative file paths do not work as expected. Please see this issue for the explanation.

Sourcemaps

Because of browser limitations, sourcemaps are only available in conjunction with the extract-text-webpack-plugin. Use that plugin to extract the CSS code from the generated JS bundle into a separate file (which improves performance because JS and CSS are loaded in parallel).

webpack.config.js

const ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
  // must be 'source-map' or 'inline-source-map'
  devtool: 'source-map',
  module: {
    rules: [
      {
        test: /\.less$/,
        use: ExtractTextPlugin.extract({
          fallbackLoader: 'style-loader',
          loader: [
            // activate source maps via loader query
            {
              loader: 'css-loader',
              options: { sourceMap: true, importLoaders: 1 }
            },
            {
              loader: 'less-loader',
              options: { sourceMap: true }
            }
          ]
        })
      }
    ]
  },
  plugins: [
    // extract CSS into separate file
    new ExtractTextPlugin('app.bundle.css')
  ]
}

If you want to view the original LESS files inside Chrome and even edit it, there's a good blog post. Checkout test/sourceMap for a running example. Make sure to serve the content with an HTTP server.

Contributing

Don't hesitate to create a pull request. Every contribution is appreciated. In development you can start the tests by calling npm test.

The tests are basically just comparing the generated css with a reference css-file located under test/css. You can easily generate a reference css-file by calling node test/helpers/generateCss.js <less-file-without-less-extension>. It passes the less-file to less and writes the output to the test/css-folder.

Maintainer


Johannes Ewald

less-loader's People

Contributors

dthielman avatar fengmk2 avatar gunta avatar haspatrickc avatar hqidea avatar jhnns avatar jpvanhal avatar kevinzwhuang avatar mfellner avatar michael-ciniawsky avatar mtscout6 avatar next0 avatar sokra avatar sophiebits avatar spacek33z avatar thelarkinn avatar wolever avatar yhtml5 avatar ztamizzen avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.