Skip to content

Commit 6c799d5

Browse files
authored
Merge pull request #11 from meszaros-lajos-gyorgy/master
Added onImport config key for compatibility with postcss-import
2 parents eaa6c63 + 3389c04 commit 6c799d5

File tree

2 files changed

+39
-11
lines changed

2 files changed

+39
-11
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,20 @@ grunt.initConfig({
147147
}
148148
});
149149
```
150+
151+
#### Misc
152+
153+
Similarly to `postcss-import`, the list of imported files can be viewed by
154+
assigning a function to the `onImport` key among the options:
155+
156+
```javascript
157+
less({
158+
/* other Less.js options */
159+
onImport: function(sources){
160+
console.log(sources)
161+
}
162+
})
163+
```
164+
165+
The received sources will be an array of strings, containing the absolute path
166+
to the files, which were imported, including the source file.

index.js

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ var postcss = require('postcss')
66

77
var render = require("./lib/render")(less.ParseTree, less.transformTree);
88

9+
function isFunction(f){
10+
return typeof f === 'function';
11+
}
12+
913
function LessPlugin() {
1014
var cacheInput;
1115

@@ -272,16 +276,19 @@ function LessPlugin() {
272276
}
273277
}
274278

275-
return new Promise(function (resolve, reject) {
276-
277-
cacheInput = cacheInput.toString();
278-
if(!cacheInput) {
279-
// TODO: explain the error
280-
reject(new CssSyntaxError(
281-
"No input is present"
282-
));
283-
}
284-
render(cacheInput, opts, function(err, tree, evaldRoot, imports) {
279+
return new Promise(function (resolve, reject) {
280+
var onImport = opts.onImport;
281+
282+
delete opts.onImport
283+
284+
cacheInput = cacheInput.toString();
285+
if(!cacheInput) {
286+
// TODO: explain the error
287+
reject(new CssSyntaxError(
288+
"No input is present"
289+
));
290+
}
291+
render(cacheInput, opts, function(err, tree, evaldRoot, imports) {
285292
if(err) {
286293
// Build PostCSS error
287294
return reject(new CssSyntaxError(
@@ -299,7 +306,11 @@ function LessPlugin() {
299306
context = {};
300307
// Convert Less AST to PostCSS AST
301308
convertImports(imports.contents);
302-
309+
310+
if(isFunction(onImport)){
311+
onImport(Object.keys(postCssInputs))
312+
}
313+
303314
processRules(css, evaldRoot.rules);
304315
resolve();
305316
}

0 commit comments

Comments
 (0)