diff --git a/src/index.js b/src/index.js index d5263559..70dabae2 100644 --- a/src/index.js +++ b/src/index.js @@ -984,35 +984,36 @@ 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(); + for (const module of usedModules) { + let content = module.content.toString(); - if (includePathinfo) { + const readableIdentifier = module.readableIdentifier(requestShortener); + + if (compilation.outputOptions.pathinfo) { // From https://github.com/webpack/webpack/blob/29eff8a74ecc2f87517b627dee451c2af9ed3f3f/lib/ModuleInfoHeaderPlugin.js#L191-L194 - const req = m.readableIdentifier(requestShortener); - const reqStr = req.replace(/\*\//g, "*_/"); + const reqStr = readableIdentifier.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 - if (m.media) { + if (module.media) { // insert media into the @import // this is rar // TODO improve this and parse the CSS to support multiple medias - content = content.replace(/;|\s*$/, m.media); + content = content.replace(/;|\s*$/, module.media); } externalsSource.add(content); externalsSource.add("\n"); } else { - if (m.media) { - source.add(`@media ${m.media} {\n`); + if (module.media) { + source.add(`@media ${module.media} {\n`); } const { path: filename } = compilation.getPathWithInfo( @@ -1024,22 +1025,21 @@ class MiniCssExtractPlugin { content = content.replace(new RegExp(AUTO_PUBLIC_PATH, "g"), undoPath); - if (m.sourceMap) { + if (module.sourceMap) { source.add( new SourceMapSource( content, - m.readableIdentifier(requestShortener), - m.sourceMap.toString() + readableIdentifier, + module.sourceMap.toString() ) ); } else { - source.add( - new RawSource(content, m.readableIdentifier(requestShortener)) - ); + source.add(new RawSource(content, readableIdentifier)); } + source.add("\n"); - if (m.media) { + if (module.media) { source.add("}\n"); } } diff --git a/test/cases/pathinfo/expected/main.css b/test/cases/pathinfo/expected/main.css new file mode 100644 index 00000000..b1fbc3ee --- /dev/null +++ b/test/cases/pathinfo/expected/main.css @@ -0,0 +1,21 @@ +/*!********************************************************************!*\ + !*** css ../../../node_modules/css-loader/dist/cjs.js!./style.css ***! + \********************************************************************/ +body { + background: red; +} + +/*!********************************************************************!*\ + !*** css ../../../node_modules/css-loader/dist/cjs.js!./other.css ***! + \********************************************************************/ +body { + background: blue; +} + +/*!********************************************************************!*\ + !*** css ../../../node_modules/css-loader/dist/cjs.js!./extra.css ***! + \********************************************************************/ +body { + background: yellow; +} + diff --git a/test/cases/pathinfo/extra.css b/test/cases/pathinfo/extra.css new file mode 100644 index 00000000..19f99652 --- /dev/null +++ b/test/cases/pathinfo/extra.css @@ -0,0 +1,3 @@ +body { + background: yellow; +} diff --git a/test/cases/pathinfo/index.js b/test/cases/pathinfo/index.js new file mode 100644 index 00000000..938a4b6e --- /dev/null +++ b/test/cases/pathinfo/index.js @@ -0,0 +1,3 @@ +import "./style.css"; +import "./other.css"; +import "./extra.css"; diff --git a/test/cases/pathinfo/other.css b/test/cases/pathinfo/other.css new file mode 100644 index 00000000..ae844dcf --- /dev/null +++ b/test/cases/pathinfo/other.css @@ -0,0 +1,3 @@ +body { + background: blue; +} diff --git a/test/cases/pathinfo/style.css b/test/cases/pathinfo/style.css new file mode 100644 index 00000000..67ce83e4 --- /dev/null +++ b/test/cases/pathinfo/style.css @@ -0,0 +1,3 @@ +body { + background: red; +} diff --git a/test/cases/pathinfo/webpack.config.js b/test/cases/pathinfo/webpack.config.js new file mode 100644 index 00000000..77000b85 --- /dev/null +++ b/test/cases/pathinfo/webpack.config.js @@ -0,0 +1,21 @@ +import Self from "../../../src"; + +module.exports = { + entry: "./index.js", + output: { + pathinfo: true, + }, + module: { + rules: [ + { + test: /\.css$/, + use: [Self.loader, "css-loader"], + }, + ], + }, + plugins: [ + new Self({ + filename: "[name].css", + }), + ], +}; diff --git a/test/pathinfo.test.js b/test/pathinfo.test.js deleted file mode 100644 index 07d9ac72..00000000 --- a/test/pathinfo.test.js +++ /dev/null @@ -1,57 +0,0 @@ -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"); - }); -});