Less-loader - Webpack - W3cubDocs
文章推薦指數: 80 %
A Less loader for webpack. Compiles Less to CSS. Requirements. This module requires a minimum of Node v6.9.0 and Webpack v4.0.0. Getting Started. less-loader [![npm][npm]][npm-url][![node][node]][node-url][![deps][deps]][deps-url][![tests][tests]][tests-url][![chat][chat]][chat-url]ALessloaderforwebpack.CompilesLesstoCSS.ThismodulerequiresaminimumofNodev6.9.0andWebpackv4.0.0.Tobegin,you'llneedtoinstallless-loader:$npminstallless-loader--save-dev Thenmodifyyourwebpack.config.js://webpack.config.js module.exports={ ... module:{ rules:[{ test:/\.less$/, loader:'less-loader'//compilesLesstoCSS }] } }; Andrunwebpackviayourpreferredmethod.Theless-loaderrequireslessaspeerDependency.Thusyouareabletocontroltheversionsaccurately.Chaintheless-loaderwiththecss-loaderandthestyle-loadertoimmediatelyapplyallstylestotheDOM.//webpack.config.js module.exports={ ... module:{ rules:[{ test:/\.less$/, use:[{ loader:'style-loader'//createsstylenodesfromJSstrings },{ loader:'css-loader'//translatesCSSintoCommonJS },{ loader:'less-loader'//compilesLesstoCSS }] }] } }; YoucanpassanyLessspecificoptionstotheless-loadervialoaderoptions.SeetheLessdocumentationforallavailableoptionsindash-case.Sincewe'repassingtheseoptionstoLessprogrammatically,youneedtopassthemincamelCasehere://webpack.config.js module.exports={ ... module:{ rules:[{ test:/\.less$/, use:[{ loader:'style-loader' },{ loader:'css-loader' },{ loader:'less-loader',options:{ strictMath:true, noIeCompat:true } }] }] } }; Unfortunately,Lessdoesn'tmapalloptions1-by-1tocamelCase.Whenindoubt,checktheirexecutableandsearchforthedash-caseoption.Usually,it'srecommendedtoextractthestylesheetsintoadedicatedfileinproductionusingtheMiniCssExtractPlugin.ThiswayyourstylesarenotdependentonJavaScript.Startingwithless-loader4,youcannowchoosebetweenLess'builtinresolverandwebpack'sresolver.Bydefault,webpack'sresolverisused.webpackprovidesanadvancedmechanismtoresolvefiles.Theless-loaderappliesaLesspluginthatpassesallqueriestothewebpackresolver.ThusyoucanimportyourLessmodulesfromnode_modules.Justprependthemwitha~whichtellswebpacktolookupthemodules.@import'~bootstrap/less/bootstrap'; It'simportanttoonlyprependitwith~,because~/resolvestothehome-directory.webpackneedstodistinguishbetweenbootstrapand~bootstrap,becauseCSSandLessfileshavenospecialsyntaxforimportingrelativefiles.Writing@import"file"isthesameas@import"./file";Usingwebpack'sresolver,youcanimportanyfiletype.YoujustneedaloaderthatexportsvalidLesscode.Often,youwillalsowanttosettheissuerconditiontoensurethatthisruleisonlyappliedonimportsoriginatingfromLessfiles://webpack.config.js module.exports={ ... module:{ rules:[{ test:/\.js$/, issuer:/\.less$/, use:[{ loader:'js-to-less-loader' }] }] } }; Ifyouspecifythepathsoption,theless-loaderwillnotusewebpack'sresolver.Modules,thatcan'tberesolvedinthelocalfolder,willbesearchedinthegivenpaths.ThisisLess'defaultbehavior.pathsshouldbeanarraywithabsolutepaths://webpack.config.js module.exports={ ... module:{ rules:[{ test:/\.less$/, use:[{ loader:'style-loader' },{ loader:'css-loader' },{ loader:'less-loader',options:{ paths:[ path.resolve(__dirname,'node_modules') ] } }] }] } }; Inthiscase,allwebpackfeatureslikeimportingnon-Lessfilesoraliasingwon'tworkofcourse.Inordertouseplugins,simplysetthepluginsoptionlikethis://webpack.config.js constCleanCSSPlugin=require('less-plugin-clean-css'); module.exports={ ... { loader:'less-loader',options:{ plugins:[ newCleanCSSPlugin({advanced:true}) ] } }] ... }; BundlingCSSwithwebpackhassomeniceadvantageslikereferencingimagesandfontswithhashedurlsorhotmodulereplacementindevelopment.Inproduction,ontheotherhand,it'snotagoodideatoapplyyourstylesheetsdependingonJSexecution.RenderingmaybedelayedorevenaFOUCmightbevisible.Thusit'softenstillbettertohavethemasseparatefilesinyourfinalproductionbuild.Therearetwopossibilitiestoextractastylesheetfromthebundle: extract-loader(simpler,butspecializedonthecss-loader'soutput) MiniCssExtractPlugin(morecomplex,butworksinalluse-cases)ToenableCSSsourcemaps,you'llneedtopassthesourceMapoptiontotheless-loaderandthecss-loader.Yourwebpack.config.jsshouldlooklikethis:module.exports={ ... module:{ rules:[{ test:/\.less$/, use:[{ loader:'style-loader' },{ loader:'css-loader',options:{ sourceMap:true } },{ loader:'less-loader',options:{ sourceMap:true } }] }] } }; AlsocheckoutthesourceMapsexample.IfyouwanttoedittheoriginalLessfilesinsideChrome,there'sagoodblogpost.TheblogpostisaboutSassbutitalsoworksforLess.ThereisaknownproblemwithLessandCSSmodulesregardingrelativefilepathsinurl(...)statements.Seethisissueforanexplanation.Pleasetakeamomenttoreadourcontributingguidelinesifyouhaven'tyetdoneso. ©JSFoundationandothercontributorsLicensedundertheCreativeCommonsAttributionLicense4.0. https://webpack.js.org/loaders/less-loader
延伸文章資訊
- 1P11.6-webpack配置less-loader - 华为云社区
这篇再介绍下使用less-loader插件安装配置,通过这篇案例以后我们就可以复制它的步骤,举一反三安装更多的插件。 2.创建less项目. 2.1.创建项目. 创建项目 ...
- 2less-loader - npm
webpack provides an advanced mechanism to resolve files. less-loader applies a Less plugin that p...
- 3less-loader | webpack 中文文档 - 印记中文
webpack 将Less 编译为CSS 的loader。 快速开始. 首先,你需要先安装 less 和 less-loader : $ npm install less less-loader...
- 4Webpack 之less-loader 详解- SegmentFault 思否
将打包的less文件分离出来在官方文档看到这样一个例子: {代码...} 但是我... ... 安装. npm install --save-dev less-loader less ...
- 5less-loader - webpack
webpack provides an advanced mechanism to resolve files. The less-loader applies a Less plugin th...