- webpack概述
- 入口起点(Entry Points)
- 输出(Output)
- 模块(Mode)
- 加载器(Loaders)
- 插件(Plugins)
- 配置(Configuration)
- 模块(Modules)
- 模块解析(Module Resolution)
- 依赖图表(Dependency Graph)
- 文件清单(Manifest)
- 构建目标(Targets)
- 模块热替换(Hot Module Replacement)
- 第二部分:配置
- 使用不同语言进行配置(Configuration Languages)
- 多种配置类型
- 入口和上下文(Entry and Context)
- 输出(Output)
- 模块(Module)
- 解析(Resolve)
- 插件(Plugins)
- 开发中 Server(DevServer)
- 开发工具(Devtool)
- 构建目标(Targets)
- Watch 和 WatchOptions
- 外部扩展(Externals)
- 性能(Performance)
- Node
- 统计(Stats)
- 其它选项(Other Options)
- 第三部分:API
- 命令行接口(CLI)
- 包含统计数据的文件(stats data)
- Node.js API
- 模块热替换(Hot Module Replacement)
- 加载器 API
- 模块方法(module methods)
- 模块变量(module variables)
- Plugin API
- compiler 钩子
- compilation 钩子
- resolver
- parser
- 第四部分:指南
- 安装
- 起步
- 管理资源
- 管理输出
- 开发
- 模块热替换
- Tree shaking
- 生产环境构建
- 代码拆分(Code Splitting)
- 懒加载(Lazy Loading)
- 缓存(Caching)
- 创建库 (Library)
- Shimming
- 渐进式网络应用程序
- TypeScript
- 迁移到新版本
- 使用环境变量
- 构建性能
- 内容安全策略
- 开发 - Vagrant
- 管理依赖
- Public Path(公共路径)
- 集成(Integrations)
- 第五部分:加载器
- babel-loader
- yaml-frontmatter-loader
- cache-loader
- coffee-loader
- coffee-redux-loader
- coverjs-loader
- css-loader
- exports-loader
- expose-loader
- extract-loader
- file-loader
- gzip-loader
- html-loader
- i18n-loader
- imports-loader
- istanbul-instrumenter-loader
- jshint-loader
- json-loader
- json5-loader
- less-loader
- bundle-loader
- multi-loader
- node-loader
- null-loader
- polymer-webpack-loader
- postcss-loader
- raw-loader
- react-proxy-loader
- restyle-loader
- sass-loader
- script-loader
- source-map-loader
- style-loader
- svg-inline-loader
- thread-loader
- transform-loader
- url-loader
- val-loader
- worker-loader
- mocha-loader
- 第六部分:插件
- AggressiveSplittingPlugin
- ZopfliWebpackPlugin
- BannerPlugin
- ClosureWebpackPlugin
- CommonsChunkPlugin
- ComponentWebpackPlugin
- CompressionWebpackPlugin
- ContextReplacementPlugin
- CopyWebpackPlugin
- DefinePlugin
- DllPlugin
- EnvironmentPlugin
- EvalSourceMapDevToolPlugin
- ExtractTextWebpackPlugin
- HashedModuleIdsPlugin
- HotModuleReplacementPlugin
- HtmlWebpackPlugin
- BabelMinifyWebpackPlugin
- IgnorePlugin
- LoaderOptionsPlugin
- MinChunkSizePlugin
- ModuleConcatenationPlugin
- NamedModulesPlugin
- NormalModuleReplacementPlugin
- NpmInstallWebpackPlugin
- PrefetchPlugin
- ProfilingPlugin
- ProvidePlugin
- SourceMapDevToolPlugin
- SplitChunksPlugin
- UglifyjsWebpackPlugin
- WatchIgnorePlugin
- I18nWebpackPlugin
less-loader
Compiles Less to CSS.
Use the /doc/webpack-loaders-css-loader or the /doc/webpack-loaders-raw-loader to turn it into a JS module and the ExtractTextPlugin to extract it into a separate file.
安装
npm install --save-dev less-loader less
The less-loader requires less as peerDependency
. Thus you are able to control the versions accurately.
示例
将 /doc/webpack-loaders-css-loader、style-loader 和 less-loader 链式调用,可以把所有样式立即应用于 DOM。
// webpack.config.js
module.exports = {
...
module: {
rules: [{
test: /\.less$/,
use: [{
loader: "style-loader" // creates style nodes from JS strings
}, {
loader: "/doc/webpack-loaders-css-loader" // translates CSS into CommonJS
}, {
loader: "less-loader" // compiles Less to CSS
}]
}]
}
};
You can pass any Less specific options to the less-loader via loader options. See the Less documentation for all available options in dash-case. Since we're passing these options to Less programmatically, you need to pass them in camelCase here:
// webpack.config.js
module.exports = {
...
module: {
rules: [{
test: /\.less$/,
use: [{
loader: "style-loader"
}, {
loader: "/doc/webpack-loaders-css-loader"
}, {
loader: "less-loader", options: {
strictMath: true,
noIeCompat: true
}
}]
}]
}
};
Unfortunately, Less doesn't map all options 1-by-1 to camelCase. When in doubt, check their executable and search for the dash-case option.
Usually, it's recommended to extract the style sheets into a dedicated file in production using the ExtractTextPlugin. This way your styles are not dependent on JavaScript:
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const extractLess = new ExtractTextPlugin({
filename: "[name].[contenthash].css",
disable: process.env.NODE_ENV === "development"
});
module.exports = {
...
module: {
rules: [{
test: /\.less$/,
use: extractLess.extract({
use: [{
loader: "/doc/webpack-loaders-css-loader"
}, {
loader: "less-loader"
}],
// use style-loader in development
fallback: "style-loader"
})
}]
},
plugins: [
extractLess
]
};
Usage
Imports
Starting with less-loader 4, you can now choose between Less' builtin resolver and webpack's resolver. By default, webpack's resolver is used.
webpack resolver
webpack 提供了一种解析文件的高级机制。less-loader 应用一个 Less 插件,并且将所有查询参数传递给 webpack resolver。所以,你可以从 node_modules
导入你的 less 模块。只要加一个 ~
前缀,告诉 webpack 去查询模块
。
@import "~bootstrap/less/bootstrap";
重要的是只使用 ~
前缀,因为 ~/
会解析为主目录。webpack 需要区分bootstrap
和〜bootstrap
,因为 CSS 和 Less 文件没有用于导入相对文件的特殊语法。@import "file"
与 @import "./file";
写法相同
Non-Less imports
使用 webpack resolver,你可以引入任何文件类型。你只需要一个导出有效的 Less 代码的 loader。通常,你还需要设置 issuer
条件,以确保此规则仅适用于 Less 文件的导入:
// webpack.config.js
module.exports = {
...
module: {
rules: [{
test: /\.js$/,
issuer: /\.less$/,
use: [{
loader: "js-to-less-loader"
}]
}]
}
};
Less resolver
If you specify the paths
option, the less-loader will not use webpack's resolver. Modules, that can't be resolved in the local folder, will be searched in the given paths
. This is Less' default behavior. paths
should be an array with absolute paths:
// webpack.config.js
module.exports = {
...
module: {
rules: [{
test: /\.less$/,
use: [{
loader: "style-loader"
}, {
loader: "/doc/webpack-loaders-css-loader"
}, {
loader: "less-loader", options: {
paths: [
path.resolve(__dirname, "node_modules")
]
}
}]
}]
}
};
In this case, all webpack features like importing non-Less files or aliasing won't work of course.
插件
为了使用插件,只需像下面这样简单设置 plugins
选项:
// webpack.config.js
const CleanCSSPlugin = require("less-plugin-clean-css");
module.exports = {
...
{
loader: "less-loader", options: {
plugins: [
new CleanCSSPlugin({ advanced: true })
]
}
}]
...
};
Extracting style sheets
Bundling CSS with webpack has some nice advantages like referencing images and fonts with hashed urls or hot module replacement in development. In production, on the other hand, it's not a good idea to apply your style sheets depending on JS execution. Rendering may be delayed or even a FOUC might be visible. Thus it's often still better to have them as separate files in your final production build.
There are two possibilities to extract a style sheet from the bundle:
- extract-loader (simpler, but specialized on the /doc/webpack-loaders-css-loader's output)
- extract-text-webpack-plugin (more complex, but works in all use-cases)
Source maps
要启用 CSS 的 source map,你需要将 sourceMap
选项传递给 less-loader 和 /doc/webpack-loaders-css-loader。所以你的 `webpack.config.js' 应该是这样:
module.exports = {
...
module: {
rules: [{
test: /\.less$/,
use: [{
loader: "style-loader"
}, {
loader: "/doc/webpack-loaders-css-loader", options: {
sourceMap: true
}
}, {
loader: "less-loader", options: {
sourceMap: true
}
}]
}]
}
};
Also checkout the sourceMaps example.
如果你要编辑 Chrome 中的原始 Less 文件,这里有一个很好的博客文章。此博客文章是关于 Sass 的,但它也适用于 Less。
CSS modules gotcha
There is a known problem with Less and CSS modules regarding relative file paths in url(...)
statements. See this issue for an explanation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论