I'm using Webpack 4
with the plugins MiniCssExtractPlugin
and OptimizeCSSAssetsPlugin
to extract SCSS
files and minify the main.css
file in production. I have source mapping enabled in my config file like so:
const path = require('path');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
module.exports = {
devtool: "source-map",
entry: {
style: './src/scss/main.scss'
},
output: {
path: path.resolve(__dirname, "dist"),
filename: '[name].bundle.js'
},
module: {
rules: [
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
{ loader: 'css-loader', options: { url: false, sourceMap: true } }
]
},
{
test: /\.scss$/,
use: [
'style-loader',
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
url: false, sourceMap: true
}
},
{
loader: 'sass-loader',
options: {
sourceMap: true
}
}
]
}
]
},
plugins: [
new MiniCssExtractPlugin({
sourceMap: true,
filename: "main.css"
}),
new UglifyJsPlugin({
sourceMap: true,
test: /\.js($|\?)/i,
}),
new OptimizeCSSAssetsPlugin({
cssProcessorOptions: {
map: {
inline: true
}
}
}) ,
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('production')
})
]
}
In the console, it does give the line where a CSS rules originates from, but in the bundled main.css
file, rather than invididual scss files ie _layout.scss
, _buttons.scss
.
Question
What configuration do I add in order to display where the CSS rule originates from, rather then where it is located in the bundled main.css
file?
As i can see on your sass-loader
configuration, you're already getting the source maps for yous scss
files (to verify that you need to have a file with the same name as you css, but with a .map
on its name), so the only thing you will need is to peroperly configure your browser to load and use the source maps. This example uses Google Chrome:
/*# sourceMappingURL=style.css.map */
reference That should work. Let me know. Otherwise i will fine tune the quick tutorial.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.