Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: source map generation with pathinfo #817

Merged
merged 2 commits into from Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/index.js
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
@@ -0,0 +1,3 @@
body {
background: yellow;
}
3 changes: 3 additions & 0 deletions test/cases/pathinfo-devtool-source-map/index.js
@@ -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
@@ -0,0 +1,3 @@
body {
background: blue;
}
3 changes: 3 additions & 0 deletions test/cases/pathinfo-devtool-source-map/style.css
@@ -0,0 +1,3 @@
body {
background: red;
}
22 changes: 22 additions & 0 deletions test/cases/pathinfo-devtool-source-map/webpack.config.js
@@ -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",
}),
],
};