Skip to content

Commit 136112f

Browse files
committed
feat: add pathinfo
1 parent 9da806f commit 136112f

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-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

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

0 commit comments

Comments
 (0)