Webpack Compiling less/scss files

Source: Internet
Author: User

1, install the plug-in

Handling Less:

NPM Install Less-loader--save-dev

Handling SASS:

NPM Install Sass-loader--save-dev

2. Project directory:

Layer.less is:

. Layer {    width: 600px;     height: 200px;     background-color: Green;     > div{        width: 400px;         Height: 200px;         background-color: red;    }     . Flex{        display: flex;    } }

Layer.js is:

Import './layer.less '; // import TPL from './layer.html '; function layer () {    return  {        name:' layer ',        tpl:tpl    Default layer;

3, webpack.config.js configuration file (to compile less as an example)

varHtmlwebpackplugin = require (' Html-webpack-plugin ')); Module.exports={entry:'./src/app.js ', Output: {path: __dirname+ '/dist ', FileName:' Js/[name].js '}, module: {loaders: [{test:/\.js$/,                //The following directory does not handleExclude:/node_modules/,                //only the following directories are processedInclude:/src/, Loader:"Babel-loader",                //The configured target Runtime Environment (environment) automatically enables the required Babel plug-insquery: {presets: [' Latest ']                }            },            //CSS handles this piece{test:/\.css$/, use: [' Style-loader ', {loader:' Css-loader ', options: {//Support @important introduction of CSSImportloaders:1}}, {loader:' Postcss-loader ', Options: {plugins:function() {                                return [                                    //be sure to write in front of require ("autoprefixer"), otherwise require ("Autoprefixer") is invalidRequire (' Postcss-import ') (), Require ("Autoprefixer")({                                        "Browsers": ["Android >= 4.1", "IOS >= 7.0", "IE >= 8"]                                    })                                ]                            } }                    }                ]            },             //lessdeal with this piece {test:/\.less$/, use: [' Style-loader ', {loader:' Css-loader ', options: {//Support @important introduction of CSSImportloaders:1}}, {loader:' Postcss-loader ', Options: {plugins:function() {                                return [                                    //be sure to write in front of require ("autoprefixer"), otherwise require ("Autoprefixer") is invalidRequire (' Postcss-import ') (), Require ("Autoprefixer")({                                        "Browsers": ["Android >= 4.1", "IOS >= 7.0", "IE >= 8"]                                    })                                ]                            } }                    },                    ' Less-loader ']}]}, plugins: [Newhtmlwebpackplugin ({Template:' Index.html ', FileName:' Index.html '        })    ]}

Note: Postcss-loader after Css-loader, before Less-loader.

4. Perform compilation & View Effects

NPM Run Webpack

Webpack Compiling less/scss files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.