diff --git a/src/index.js b/src/index.js index 284604c5..d5263559 100644 --- a/src/index.js +++ b/src/index.js @@ -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 diff --git a/test/pathinfo.test.js b/test/pathinfo.test.js new file mode 100644 index 00000000..07d9ac72 --- /dev/null +++ b/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"); + }); +});