Skip to content

Commit a37713f

Browse files
feat: support the pathinfo option (#783)
1 parent 9da806f commit a37713f

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

src/index.js

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

988989
for (const m of usedModules) {
989990
let content = m.content.toString();
990991

992+
if (includePathinfo) {
993+
// 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, "*_/");
996+
const reqStrStar = "*".repeat(reqStr.length);
997+
const headerStr = `/*!****${reqStrStar}****!*\\\n !*** ${reqStr} ***!\n \\****${reqStrStar}****/\n`;
998+
content = headerStr + content;
999+
}
1000+
9911001
if (/^@import url/.test(content)) {
9921002
// HACK for IE
9931003
// http://stackoverflow.com/a/14676665/1458162

test/pathinfo.test.js

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import path from "path";
2+
3+
import MiniCssExtractPlugin from "../src/cjs";
4+
5+
import { compile, getCompiler } from "./helpers/index";
6+
7+
describe("pathinfo option", () => {
8+
it(`should insert pathinfo`, async () => {
9+
const compiler = getCompiler(
10+
"esm.js",
11+
{},
12+
{
13+
mode: "none",
14+
output: {
15+
pathinfo: true,
16+
path: path.resolve(__dirname, "../outputs"),
17+
},
18+
plugins: [
19+
new MiniCssExtractPlugin({
20+
filename: "[name].css",
21+
}),
22+
],
23+
}
24+
);
25+
const stats = await compile(compiler);
26+
const fs = stats.compilation.compiler.outputFileSystem;
27+
const extractedCss = fs
28+
.readFileSync(path.resolve(__dirname, "../outputs/main.css"))
29+
.toString();
30+
31+
expect(extractedCss).toMatch("./simple.css");
32+
});
33+
it(`should not insert pathinfo`, async () => {
34+
const compiler = getCompiler(
35+
"esm.js",
36+
{},
37+
{
38+
mode: "none",
39+
output: {
40+
path: path.resolve(__dirname, "../outputs"),
41+
},
42+
plugins: [
43+
new MiniCssExtractPlugin({
44+
filename: "[name].css",
45+
}),
46+
],
47+
}
48+
);
49+
const stats = await compile(compiler);
50+
const fs = stats.compilation.compiler.outputFileSystem;
51+
const extractedCss = fs
52+
.readFileSync(path.resolve(__dirname, "../outputs/main.css"))
53+
.toString();
54+
55+
expect(extractedCss).not.toMatch("./simple.css");
56+
});
57+
});

0 commit comments

Comments
 (0)