Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: adding media, supports and layer for external import #1072

Merged
merged 2 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1368,11 +1368,32 @@ class MiniCssExtractPlugin {

// HACK for IE
// http://stackoverflow.com/a/14676665/1458162
if (module.media) {
if (
module.media ||
module.supports ||
typeof module.layer !== "undefined"
) {
let atImportExtra = "";

const needLayer = typeof module.layer !== "undefined";

if (needLayer) {
atImportExtra +=
module.layer.length > 0 ? ` layer(${module.layer})` : " layer";
}

if (module.supports) {
atImportExtra += ` supports(${module.supports})`;
}

if (module.media) {
atImportExtra += ` ${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*$/, `${module.media};`);
content = content.replace(/;|\s*$/, `${atImportExtra};`);
}

externalsSource.add(content);
Expand Down
15 changes: 15 additions & 0 deletions test/cases/at-import-external-with-media/a.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@import url(http://some/path/to/css1.css);
@import url(http://some/path/to/css2.css) screen and (max-width: 1024px);
@import url(http://some/path/to/css3.css) supports(display: grid) screen and
(max-width: 400px);
@import url(http://some/path/to/css4.css) supports(display: grid);
@import url(http://some/path/to/css5.css) layer();
@import url(http://some/path/to/css6.css) layer;
@import url(http://some/path/to/css7.css) layer(layer-name)
supports(display: grid) screen and (max-width: 1024px);
@import url(http://some/path/to/css8.css) layer(layer-name) screen and
(max-width: 1024px);

body {
background: red;
}
5 changes: 5 additions & 0 deletions test/cases/at-import-external-with-media/b.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@import url("https://some/external/css");

.b {
background: red;
}
19 changes: 19 additions & 0 deletions test/cases/at-import-external-with-media/expected/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@import url(http://some/path/to/css1.css);
@import url(http://some/path/to/css2.css) screen and (max-width: 1024px);
@import url(http://some/path/to/css3.css) supports(display: grid) screen and
(max-width: 400px);
@import url(http://some/path/to/css4.css) supports(display: grid);
@import url(http://some/path/to/css5.css) layer;
@import url(http://some/path/to/css6.css) layer;
@import url(http://some/path/to/css7.css) layer(layer-name) supports(display: grid) screen and (max-width: 1024px);
@import url(http://some/path/to/css8.css) layer(layer-name) screen and
(max-width: 1024px);
@import url(https://some/external/css);
body {
background: red;
}

.b {
background: red;
}

2 changes: 2 additions & 0 deletions test/cases/at-import-external-with-media/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import "./a.css";
import "./b.css";
18 changes: 18 additions & 0 deletions test/cases/at-import-external-with-media/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Self from "../../../src";

module.exports = {
entry: "./index.js",
module: {
rules: [
{
test: /\.css$/,
use: [Self.loader, "css-loader"],
},
],
},
plugins: [
new Self({
filename: "[name].css",
}),
],
};