Skip to content

Commit 31b678c

Browse files
refactor: code (#784)
1 parent a37713f commit 31b678c

File tree

8 files changed

+71
-74
lines changed

8 files changed

+71
-74
lines changed

src/index.js

+17-17
Original file line numberDiff line numberDiff line change
@@ -984,35 +984,36 @@ class MiniCssExtractPlugin {
984984
compiler.webpack.sources;
985985
const source = new ConcatSource();
986986
const externalsSource = new ConcatSource();
987-
const includePathinfo = compilation.outputOptions.pathinfo;
988987

989-
for (const m of usedModules) {
990-
let content = m.content.toString();
988+
for (const module of usedModules) {
989+
let content = module.content.toString();
991990

992-
if (includePathinfo) {
991+
const readableIdentifier = module.readableIdentifier(requestShortener);
992+
993+
if (compilation.outputOptions.pathinfo) {
993994
// From https://github.com/webpack/webpack/blob/29eff8a74ecc2f87517b627dee451c2af9ed3f3f/lib/ModuleInfoHeaderPlugin.js#L191-L194
994-
const req = m.readableIdentifier(requestShortener);
995-
const reqStr = req.replace(/\*\//g, "*_/");
995+
const reqStr = readableIdentifier.replace(/\*\//g, "*_/");
996996
const reqStrStar = "*".repeat(reqStr.length);
997997
const headerStr = `/*!****${reqStrStar}****!*\\\n !*** ${reqStr} ***!\n \\****${reqStrStar}****/\n`;
998+
998999
content = headerStr + content;
9991000
}
10001001

10011002
if (/^@import url/.test(content)) {
10021003
// HACK for IE
10031004
// http://stackoverflow.com/a/14676665/1458162
1004-
if (m.media) {
1005+
if (module.media) {
10051006
// insert media into the @import
10061007
// this is rar
10071008
// TODO improve this and parse the CSS to support multiple medias
1008-
content = content.replace(/;|\s*$/, m.media);
1009+
content = content.replace(/;|\s*$/, module.media);
10091010
}
10101011

10111012
externalsSource.add(content);
10121013
externalsSource.add("\n");
10131014
} else {
1014-
if (m.media) {
1015-
source.add(`@media ${m.media} {\n`);
1015+
if (module.media) {
1016+
source.add(`@media ${module.media} {\n`);
10161017
}
10171018

10181019
const { path: filename } = compilation.getPathWithInfo(
@@ -1024,22 +1025,21 @@ class MiniCssExtractPlugin {
10241025

10251026
content = content.replace(new RegExp(AUTO_PUBLIC_PATH, "g"), undoPath);
10261027

1027-
if (m.sourceMap) {
1028+
if (module.sourceMap) {
10281029
source.add(
10291030
new SourceMapSource(
10301031
content,
1031-
m.readableIdentifier(requestShortener),
1032-
m.sourceMap.toString()
1032+
readableIdentifier,
1033+
module.sourceMap.toString()
10331034
)
10341035
);
10351036
} else {
1036-
source.add(
1037-
new RawSource(content, m.readableIdentifier(requestShortener))
1038-
);
1037+
source.add(new RawSource(content, readableIdentifier));
10391038
}
1039+
10401040
source.add("\n");
10411041

1042-
if (m.media) {
1042+
if (module.media) {
10431043
source.add("}\n");
10441044
}
10451045
}

test/cases/pathinfo/expected/main.css

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*!********************************************************************!*\
2+
!*** css ../../../node_modules/css-loader/dist/cjs.js!./style.css ***!
3+
\********************************************************************/
4+
body {
5+
background: red;
6+
}
7+
8+
/*!********************************************************************!*\
9+
!*** css ../../../node_modules/css-loader/dist/cjs.js!./other.css ***!
10+
\********************************************************************/
11+
body {
12+
background: blue;
13+
}
14+
15+
/*!********************************************************************!*\
16+
!*** css ../../../node_modules/css-loader/dist/cjs.js!./extra.css ***!
17+
\********************************************************************/
18+
body {
19+
background: yellow;
20+
}
21+

test/cases/pathinfo/extra.css

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
body {
2+
background: yellow;
3+
}

test/cases/pathinfo/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import "./style.css";
2+
import "./other.css";
3+
import "./extra.css";

test/cases/pathinfo/other.css

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
body {
2+
background: blue;
3+
}

test/cases/pathinfo/style.css

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
body {
2+
background: red;
3+
}

test/cases/pathinfo/webpack.config.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import Self from "../../../src";
2+
3+
module.exports = {
4+
entry: "./index.js",
5+
output: {
6+
pathinfo: true,
7+
},
8+
module: {
9+
rules: [
10+
{
11+
test: /\.css$/,
12+
use: [Self.loader, "css-loader"],
13+
},
14+
],
15+
},
16+
plugins: [
17+
new Self({
18+
filename: "[name].css",
19+
}),
20+
],
21+
};

test/pathinfo.test.js

-57
This file was deleted.

0 commit comments

Comments
 (0)