Skip to content

Commit

Permalink
fix: source map generation with pathinfo (#817)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored Aug 31, 2021
1 parent c6c18a6 commit f813b4c
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -991,16 +991,22 @@ class MiniCssExtractPlugin {
const readableIdentifier = module.readableIdentifier(requestShortener);
const startsWithAtRuleImport = /^@import url/.test(content);

let header;

if (compilation.outputOptions.pathinfo) {
// From https://github.com/webpack/webpack/blob/29eff8a74ecc2f87517b627dee451c2af9ed3f3f/lib/ModuleInfoHeaderPlugin.js#L191-L194
const reqStr = readableIdentifier.replace(/\*\//g, "*_/");
const reqStrStar = "*".repeat(reqStr.length);
const headerStr = `/*!****${reqStrStar}****!*\\\n !*** ${reqStr} ***!\n \\****${reqStrStar}****/\n`;

content = headerStr + content;
header = new RawSource(headerStr);
}

if (startsWithAtRuleImport) {
if (typeof header !== "undefined") {
externalsSource.add(header);
}

// HACK for IE
// http://stackoverflow.com/a/14676665/1458162
if (module.media) {
Expand All @@ -1013,6 +1019,10 @@ class MiniCssExtractPlugin {
externalsSource.add(content);
externalsSource.add("\n");
} else {
if (typeof header !== "undefined") {
source.add(header);
}

if (module.media) {
source.add(`@media ${module.media} {\n`);
}
Expand Down
23 changes: 23 additions & 0 deletions test/cases/pathinfo-devtool-source-map/expected/main.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions test/cases/pathinfo-devtool-source-map/extra.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background: yellow;
}
3 changes: 3 additions & 0 deletions test/cases/pathinfo-devtool-source-map/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import "./style.css";
import "./other.css";
import "./extra.css";
3 changes: 3 additions & 0 deletions test/cases/pathinfo-devtool-source-map/other.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background: blue;
}
3 changes: 3 additions & 0 deletions test/cases/pathinfo-devtool-source-map/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background: red;
}
22 changes: 22 additions & 0 deletions test/cases/pathinfo-devtool-source-map/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Self from "../../../src";

module.exports = {
entry: "./index.js",
devtool: "source-map",
output: {
pathinfo: true,
},
module: {
rules: [
{
test: /\.css$/,
use: [Self.loader, "css-loader"],
},
],
},
plugins: [
new Self({
filename: "[name].css",
}),
],
};

0 comments on commit f813b4c

Please sign in to comment.