From 136112f75801516471eaed96216c106137afafd4 Mon Sep 17 00:00:00 2001 From: Hiroki Osame Date: Thu, 1 Jul 2021 22:49:42 -0400 Subject: [PATCH 1/2] feat: add pathinfo --- src/index.js | 10 ++++++++ test/pathinfo.test.js | 58 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 test/pathinfo.test.js 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..6df6052d --- /dev/null +++ b/test/pathinfo.test.js @@ -0,0 +1,58 @@ +/* eslint-env browser */ +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"); + }); +}); From eea829303d457b905b12d8f6c82726d7daeb45a2 Mon Sep 17 00:00:00 2001 From: Hiroki Osame Date: Thu, 1 Jul 2021 23:05:43 -0400 Subject: [PATCH 2/2] chore: remove browser eslint env from pathinfo test --- test/pathinfo.test.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/pathinfo.test.js b/test/pathinfo.test.js index 6df6052d..07d9ac72 100644 --- a/test/pathinfo.test.js +++ b/test/pathinfo.test.js @@ -1,4 +1,3 @@ -/* eslint-env browser */ import path from "path"; import MiniCssExtractPlugin from "../src/cjs";