Skip to content

Add support for import at-rule layer functionality #4340

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/less/src/less/tree/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Expression.prototype = Object.assign(new Node(), {
},

eval(context) {
const noSpacing = this.noSpacing;
let returnValue;
const mathOn = context.isMathOn();
const inParenthesis = this.parens;
Expand Down Expand Up @@ -50,6 +51,7 @@ Expression.prototype = Object.assign(new Node(), {
&& (!(returnValue instanceof Dimension))) {
returnValue = new Paren(returnValue);
}
returnValue.noSpacing = returnValue.noSpacing || noSpacing;
return returnValue;
},

Expand Down
56 changes: 55 additions & 1 deletion packages/less/src/less/tree/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Ruleset from './ruleset';
import Anonymous from './anonymous';
import * as utils from '../utils';
import LessError from '../less-error';
import Expression from './expression';

//
// CSS @import node
Expand Down Expand Up @@ -157,6 +158,20 @@ Import.prototype = Object.assign(new Node(), {
return [];
}
}
if (this.features) {
let featureValue = this.features.value;
if (Array.isArray(featureValue) && featureValue.length === 1) {
const expr = featureValue[0];
if (expr.type === 'Expression' && Array.isArray(expr.value) && expr.value.length >= 2) {
featureValue = expr.value;
const isLayer = featureValue[0].type === 'Keyword' && featureValue[0].value === 'layer'
&& featureValue[1].type === 'Paren';
if (isLayer) {
this.css = false;
}
}
}
}
if (this.options.inline) {
const contents = new Anonymous(this.root, 0,
{
Expand All @@ -165,18 +180,57 @@ Import.prototype = Object.assign(new Node(), {
}, true, true);

return this.features ? new Media([contents], this.features.value) : [contents];
} else if (this.css) {
} else if (this.css || this.layerCss) {
const newImport = new Import(this.evalPath(context), features, this.options, this._index);
if (this.layerCss) {
newImport.css = this.layerCss;
newImport.path._fileInfo = this._fileInfo;
}
if (!newImport.css && this.error) {
throw this.error;
}
return newImport;
} else if (this.root) {
if (this.features) {
let featureValue = this.features.value;
if (Array.isArray(featureValue) && featureValue.length === 1) {
const expr = featureValue[0];
if (expr.type === 'Expression' && Array.isArray(expr.value) && expr.value.length >= 2) {
featureValue = expr.value;
const isLayer = featureValue[0].type === 'Keyword' && featureValue[0].value === 'layer'
&& featureValue[1].type === 'Paren';
if (isLayer) {
this.layerCss = true;
featureValue[0] = new Expression(featureValue.slice(0, 2));
featureValue.splice(1, 1);
featureValue[0].noSpacing = true;
return this;
}
}
}
}
ruleset = new Ruleset(null, utils.copyArray(this.root.rules));
ruleset.evalImports(context);

return this.features ? new Media(ruleset.rules, this.features.value) : ruleset.rules;
} else {
if (this.features) {
let featureValue = this.features.value;
if (Array.isArray(featureValue) && featureValue.length >= 1) {
featureValue = featureValue[0].value;
if (Array.isArray(featureValue) && featureValue.length >= 2) {
const isLayer = featureValue[0].type === 'Keyword' && featureValue[0].value === 'layer'
&& featureValue[1].type === 'Paren';
if (isLayer) {
this.css = true;
featureValue[0] = new Expression(featureValue.slice(0, 2));
featureValue.splice(1, 1);
featureValue[0].noSpacing = true;
return this;
}
}
}
}
return [];
}
}
Expand Down
4 changes: 4 additions & 0 deletions packages/test-data/css/_main/layer.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
@import url("/import/layer-import-2.css") layer(foo);
@import url("/import/layer-import-3.css") layer(responsive) supports(display: flex) screen and (max-width: 768px);
@import url("/import/layer-import-4.css") layer(print) print;
@import url("/import/layer-import-5.css") layer(features) supports(display: grid);
@layer {
.main::before {
color: #f00;
Expand Down
5 changes: 5 additions & 0 deletions packages/test-data/less/_main/import/layer-import-2.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.sub-rule {
ul {
color: white;
}
}
Empty file.
5 changes: 5 additions & 0 deletions packages/test-data/less/_main/import/layer-import-4.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.sub-rule {
ul {
color: white;
}
}
5 changes: 5 additions & 0 deletions packages/test-data/less/_main/import/layer-import-5.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.sub-rule {
ul {
color: white;
}
}
5 changes: 5 additions & 0 deletions packages/test-data/less/_main/layer.less
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
@import "./import/layer-import.less";
}

@import url("/import/layer-import-2.css") layer(foo);
@import url("/import/layer-import-3.css") layer(responsive) supports(display: flex) screen and (max-width: 768px);
@import url("/import/layer-import-4.css") layer(print) print;
@import url("/import/layer-import-5.css") layer(features) supports(display: grid);

@layer-name: primevue;

@layer @layer-name {
Expand Down