Skip to content

Commit 72c469d

Browse files
cscottlukeapage
authored andcommitted
Implement syntax in gh less#1185 for @import options. Implement multiple & less.
First step in implementing syntax for @import options, proposed in less#1185 (comment) (steps (1) and (2)). I've implemented the 'multiple' and 'less' options. One could trivially add 'once' and 'css' options as well, if there was need. Proposed "silent" and "inline" options are deferred for future work. I left the existing "@import-multiple" and "@import-once" syntax in place, although the proposal is for this to be deprecated once the new option syntax is in place.
1 parent 05fc86f commit 72c469d

File tree

5 files changed

+59
-8
lines changed

5 files changed

+59
-8
lines changed

lib/less/import-visitor.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
var env = new tree.evalEnv(this.env, this.env.frames.slice(0));
4242
this._importer.push(importNode.getPath(), importNode.currentFileInfo, function (e, root, imported) {
4343
if (e && !e.filename) { e.index = importNode.index; e.filename = importNode.currentFileInfo.filename; }
44-
if (imported && importNode.once) { importNode.skip = imported; }
44+
if (imported && !importNode.options.multiple) { importNode.skip = imported; }
4545

4646
var subFinish = function() {
4747
importVisitor.importCount--;

lib/less/parser.js

+31-2
Original file line numberDiff line numberDiff line change
@@ -1207,18 +1207,47 @@ less.Parser = function Parser(env) {
12071207

12081208
var dir = $(/^@import(?:-(once|multiple))?\s+/);
12091209

1210+
var options = (dir ? $(this.importOptions) : null) || {};
1211+
12101212
if (dir && (path = $(this.entities.quoted) || $(this.entities.url))) {
12111213
features = $(this.mediaFeatures);
12121214
if ($(';')) {
12131215
features = features && new(tree.Value)(features);
1214-
var importOnce = dir[1] !== 'multiple';
1215-
return new(tree.Import)(path, features, importOnce, index, env.currentFileInfo);
1216+
if (dir[1] === 'multiple') { options.multiple = true; }
1217+
return new(tree.Import)(path, features, options, index, env.currentFileInfo);
12161218
}
12171219
}
12181220

12191221
restore();
12201222
},
12211223

1224+
importOptions: function() {
1225+
var o, options = {};
1226+
1227+
// single option, no parens
1228+
if (o = $(this.importOption)) {
1229+
options[o] = true;
1230+
return options;
1231+
}
1232+
// list of options, surrounded by parens
1233+
if (! $('(')) { return null; }
1234+
do {
1235+
if (o = $(this.importOption)) {
1236+
options[o] = true;
1237+
if (! $(',')) { break }
1238+
}
1239+
} while (o);
1240+
expect(')');
1241+
return options;
1242+
},
1243+
1244+
importOption: function() {
1245+
var opt = $(/^(less|multiple)/);
1246+
if (opt) {
1247+
return opt[1];
1248+
}
1249+
},
1250+
12221251
mediaFeature: function () {
12231252
var e, p, nodes = [];
12241253

lib/less/tree/import.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
// `import,push`, we also pass it a callback, which it'll call once
1212
// the file has been fetched, and parsed.
1313
//
14-
tree.Import = function (path, features, once, index, currentFileInfo) {
14+
tree.Import = function (path, features, options, index, currentFileInfo) {
1515
var that = this;
1616

17-
this.once = once;
17+
this.options = options;
1818
this.index = index;
1919
this.path = path;
2020
this.features = features;
@@ -24,6 +24,7 @@ tree.Import = function (path, features, once, index, currentFileInfo) {
2424
if (pathValue) {
2525
this.css = /css([\?;].*)?$/.test(pathValue);
2626
}
27+
if (this.options.less) { this.css = false; }
2728
};
2829

2930
//
@@ -61,7 +62,7 @@ tree.Import.prototype = {
6162
return null;
6263
},
6364
evalForImport: function (env) {
64-
return new(tree.Import)(this.path.eval(env), this.features, this.once, this.index, this.currentFileInfo);
65+
return new(tree.Import)(this.path.eval(env), this.features, this.options, this.index, this.currentFileInfo);
6566
},
6667
evalPath: function (env) {
6768
var path = this.path.eval(env);
@@ -81,7 +82,7 @@ tree.Import.prototype = {
8182
if (this.skip) { return []; }
8283

8384
if (this.css) {
84-
var newImport = new(tree.Import)(this.evalPath(env), features, this.once, this.index);
85+
var newImport = new(tree.Import)(this.evalPath(env), features, this.options, this.index);
8586
if (!newImport.css && this.error) {
8687
throw this.error;
8788
}

test/css/import.css

+15
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,18 @@
2121
height: 10px;
2222
color: #ff0000;
2323
}
24+
@media screen and (max-width: 601px) {
25+
#css {
26+
color: yellow;
27+
}
28+
}
29+
@media screen and (max-width: 602px) {
30+
body {
31+
width: 100%;
32+
}
33+
}
34+
@media screen and (max-width: 603px) {
35+
#css {
36+
color: yellow;
37+
}
38+
}

test/less/import.less

+7-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,10 @@
1212
}
1313
@import "import/import-test-e" screen and (max-width: 600px);
1414

15-
@import url(http://github.com/%3Cspan%20class=%22pl-s%22%3E%3Cspan%20class=%22pl-pds%22%3E"%3C/span%3Eimport/import-test-a.less%3Cspan%20class=%22pl-pds%22%3E"%3C/span%3E%3C/span%3E);
15+
@import url(http://github.com/%3Cspan%20class=%22pl-s%22%3E%3Cspan%20class=%22pl-pds%22%3E"%3C/span%3Eimport/import-test-a.less%3Cspan%20class=%22pl-pds%22%3E"%3C/span%3E%3C/span%3E);
16+
17+
@import less "import/import-test-d.css" screen and (max-width: 601px);
18+
19+
@import multiple "import/import-test-e" screen and (max-width: 602px);
20+
21+
@import (less, multiple) url(http://github.com/%3Cspan%20class=%22pl-s%22%3E%3Cspan%20class=%22pl-pds%22%3E"%3C/span%3Eimport/import-test-d.css%3Cspan%20class=%22pl-pds%22%3E"%3C/span%3E%3C/span%3E) screen and (max-width: 603px);

0 commit comments

Comments
 (0)