Skip to content

Commit

Permalink
feat: support the pathinfo option (#783)
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed Jul 5, 2021
1 parent 9da806f commit a37713f
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/index.js
Expand Up @@ -984,10 +984,20 @@ class MiniCssExtractPlugin {
compiler.webpack.sources;
const source = new ConcatSource();
const externalsSource = new ConcatSource();
const includePathinfo = compilation.outputOptions.pathinfo;

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

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

if (/^@import url/.test(content)) {
// HACK for IE
// http://stackoverflow.com/a/14676665/1458162
Expand Down
57 changes: 57 additions & 0 deletions test/pathinfo.test.js
@@ -0,0 +1,57 @@
import path from "path";

import MiniCssExtractPlugin from "../src/cjs";

import { compile, getCompiler } from "./helpers/index";

describe("pathinfo option", () => {
it(`should insert pathinfo`, async () => {
const compiler = getCompiler(
"esm.js",
{},
{
mode: "none",
output: {
pathinfo: true,
path: path.resolve(__dirname, "../outputs"),
},
plugins: [
new MiniCssExtractPlugin({
filename: "[name].css",
}),
],
}
);
const stats = await compile(compiler);
const fs = stats.compilation.compiler.outputFileSystem;
const extractedCss = fs
.readFileSync(path.resolve(__dirname, "../outputs/main.css"))
.toString();

expect(extractedCss).toMatch("./simple.css");
});
it(`should not insert pathinfo`, async () => {
const compiler = getCompiler(
"esm.js",
{},
{
mode: "none",
output: {
path: path.resolve(__dirname, "../outputs"),
},
plugins: [
new MiniCssExtractPlugin({
filename: "[name].css",
}),
],
}
);
const stats = await compile(compiler);
const fs = stats.compilation.compiler.outputFileSystem;
const extractedCss = fs
.readFileSync(path.resolve(__dirname, "../outputs/main.css"))
.toString();

expect(extractedCss).not.toMatch("./simple.css");
});
});

0 comments on commit a37713f

Please sign in to comment.