[英]webpack: understanding source maps when working with CSS
Introduction 介绍
I have already setup bundling for my Javascript files with webpack
in my project. 我已经在项目中使用
webpack
为Javascript文件设置了捆绑。 Now I am in the process of adding CSS files to the webpack
configuration. 现在,我正在将CSS文件添加到
webpack
配置中。 So far, I have been including the CSS files manually in the HTML header by adding <link>
elements for every CSS file I depend on (eg bootstrap, my own css, etc.). 到目前为止,我已经通过在每个我依赖的CSS文件(例如,引导程序,我自己的css等)中添加
<link>
元素,来在HTML标头中手动添加CSS文件。 Obviously this is not very elegant and using webpack
would be much better, so I would like to replace the link elements and bundle them via webpack
. 显然这不是很好,使用
webpack
会更好,所以我想替换link元素并通过webpack
捆绑它们。
This should be easy, everything is pretty much documented in the webpack documentation. 这应该很容易,所有内容都已在webpack文档中进行了记录。 After reading the documentation and experimenting a bit with
webpack
I have arrived at the configuration below which already works. 阅读文档并尝试使用
webpack
我得出了下面的配置,该配置已经可以使用。
Problem 问题
The problem with my current setup is that I would like to have proper source map support and that does not seem to work. 当前设置的问题是我希望获得适当的源映射支持,但似乎无法正常工作。 By proper, I mean that I expect that when I run a development build with
webpack
and I inspect some element in Chrome DevTools, that I will see from which file and which line in the file a certain CSS class originated and that I can click on the CSS rules and the browser jumps to that file. 正确的说,我希望当我使用
webpack
运行开发版本并检查Chrome DevTools中的某些元素时,我会看到某个CSS类从哪个文件和文件的哪一行开始,并且我可以单击CSS规则,浏览器跳至该文件。
I do not want to have inline styles in the head element, because then the browser will show something like .foobar { <style>..</style>
, rather then .foobar { app.css:154
. 我不想在head元素中使用内联样式,因为这样浏览器将显示类似
.foobar { <style>..</style>
,而不是.foobar { app.css:154
。
With my current setup I have all CSS files combined (but not minified) into one app.css
file. 在当前设置下,我将所有CSS文件组合(但未
app.css
)到一个app.css
文件中。 This means that if I inspect a bootstrap class such as .btn
then it appears as .btn { app.css:3003
. 这意味着,如果我检查引导类,例如
.btn
则它显示为.btn { app.css:3003
。 However, what I want to achieve is that the browser shows it as .btn { bootstrap.css:3003
. 但是,我要实现的是浏览器将其显示为
.btn { bootstrap.css:3003
。
So now I am trying to understand how webpack
and the different plugins such as css-loader
and min-css-extract-plugin
apply CSS source maps, and how I can configure them to achieve a proper debugging experience. 因此,现在我试图了解
webpack
和不同的插件(例如css-loader
和min-css-extract-plugin
如何应用CSS源映射,以及如何配置它们以实现适当的调试体验。
I am not sure how relevant this is, but when I navigate in DevTools under Sources to webpack://./bootstrap/dist/css/bootstrap.css
I see that it only contains a single line: // extracted by mini-css-extract-plugin
. 我不确定这有什么相关性,但是当我在DevTools中在Sources下导航到
webpack://./bootstrap/dist/css/bootstrap.css
我看到它只包含一行: // extracted by mini-css-extract-plugin
。
Webpack Setup Webpack设置
index.js: index.js:
window.jQuery = require('jquery/dist/jquery');
require('bootstrap/dist/css/bootstrap.css');
require('bootstrap/dist/js/bootstrap');
/* other dependencies */
webpack.config.js: webpack.config.js:
const devMode = process.env.NODE_ENV !== 'production';
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
module: {
rules: [
{ /* Javascript rules excluded */ },
{
test: /\.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader
},
{
loader: 'css-loader',
options: {
sourceMap: true
}
}
]
},
{
test: /\.(png|svg|jpg|gif)$/,
use: [
'file-loader'
]
},
{
test: /\.(woff|woff2|eot|ttf|otf)$/,
use: [
'file-loader'
]
}
plugins: [
new UglifyJSPlugin (),
new HtmlWebpackPlugin({
template: 'app/index.tpl.html'
}),
new MiniCssExtractPlugin({ filename: devMode ?
'[name].css' :
'[name].[hash].css'
})
],
Conclusion 结论
It seems I just passed the rubber duck test . 看来我刚通过橡皮鸭测试 。 While I was writing this I arrived at a solution.
在写这篇文章的时候,我想到了一个解决方案。 I will still publish the question, maybe it can help others.
我仍然会发布问题,也许它可以帮助其他人。
The problem was that I was also using the mini-css-extract-plugin
for development and not just for production. 问题是我还在使用
mini-css-extract-plugin
进行开发,而不仅仅是用于生产。 I thought that I needed to do that, because when at first I was using the style-loaded
I would get styles included in the header and the browser would show me all styles as .foobar { <style>..</style>
. 我认为我需要这样做,因为当我最初使用
style-loaded
我会在标头中包含样式,浏览器会将所有样式显示为.foobar { <style>..</style>
。
However, the actual problem seemed to be, that I was not using devtools
. 但是,实际的问题似乎是我没有使用
devtools
。 So the solution was to add devtool: devMode ? 'cheap-module-eval-source-map' : 'source-map',
因此解决方案是添加
devtool: devMode ? 'cheap-module-eval-source-map' : 'source-map',
devtool: devMode ? 'cheap-module-eval-source-map' : 'source-map',
to the webpack
configuration to conditionally use the style-loader
plugin during development builds and mini-css-extract-plugin
during production builds. devtool: devMode ? 'cheap-module-eval-source-map' : 'source-map',
到webpack
配置,以在开发构建期间有条件地使用style-loader
插件,在生产构建期间有条件地使用mini-css-extract-plugin
。
webpack.config.js webpack.config.js
{
test: /\.css$/,
use: [
{
- loader: MiniCssExtractPlugin.loader,
+ loader: devMode ? 'style-loader' : MiniCssExtractPlugin.loader,
},
{
loader: 'css-loader',
options: {
sourceMap: true
}
}
]
},
/* ... */
+ devtool: devMode ? 'cheap-module-eval-source-map' : 'source-map',
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.