Skip to content

Commit

Permalink
[FIX] Apply less.js fix for import race condition
Browse files Browse the repository at this point in the history
This change applies commit ccd8ebbfdfa300b6e748e8d7c12e3dbb0efd8371
of the less.js project to resolve #24.

Fixes: #24
See: less/less.js@ccd8ebb
  • Loading branch information
matz3 committed Nov 14, 2019
1 parent 989f724 commit 694f6c4
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 10 deletions.
5 changes: 3 additions & 2 deletions lib/less/importsPush.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
*
*
* This file contains a modified version of the local function "imports.push" within the constructor of "less.Parser"
* which has been taken from LESS v1.6.3 (https://github.com/less/less.js/blob/v1.6.3/lib/less/parser.js#L70-L111).
* which has been taken from LESS v1.6.3 (https://github.com/less/less.js/blob/v1.6.3/lib/less/parser.js#L70-L111)
* and applied commit https://github.com/less/less.js/commit/ccd8ebbfdfa300b6e748e8d7c12e3dbb0efd8371.
* Modifications are marked with comments in the code.
*
*/
Expand All @@ -29,7 +30,7 @@ module.exports = function createImportsPushFunction(env, fileLoader, parserFacto
/* BEGIN MODIFICATION */

// Replaced "rootFilename" with "env.filename"
const importedPreviously = fullPath in parserImports.files || fullPath === env.filename;
const importedPreviously = fullPath === env.filename;

/* END MODIFICATION */

Expand Down
2 changes: 1 addition & 1 deletion lib/thirdparty/less/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# less.js

This folder contains the `lib/less` sub-folder of [v1.6.3](https://github.com/less/less.js/tree/v1.6.3/lib/less) of the [less.js project](https://github.com/less/less.js).
This folder contains the `lib/less` sub-folder of [v1.6.3](https://github.com/less/less.js/tree/v1.6.3/lib/less) of the [less.js project](https://github.com/less/less.js) with commit [`ccd8ebbfdfa300b6e748e8d7c12e3dbb0efd8371`](https://github.com/less/less.js/commit/ccd8ebbfdfa300b6e748e8d7c12e3dbb0efd8371) applied on top of it to resolve https://github.com/SAP/less-openui5/issues/24.

## [License](./LICENSE)

Expand Down
34 changes: 29 additions & 5 deletions lib/thirdparty/less/import-visitor.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
(function (tree) {
tree.importVisitor = function(importer, finish, evalEnv) {
tree.importVisitor = function(importer, finish, evalEnv, onceFileDetectionMap, recursionDetector) {
this._visitor = new tree.visitor(this);
this._importer = importer;
this._finish = finish;
this.env = evalEnv || new tree.evalEnv();
this.importCount = 0;
this.onceFileDetectionMap = onceFileDetectionMap || {};
this.recursionDetector = {};
if (recursionDetector) {
for(var fullFilename in recursionDetector) {
if (recursionDetector.hasOwnProperty(fullFilename)) {
this.recursionDetector[fullFilename] = true;
}
}
}
};

tree.importVisitor.prototype = {
Expand Down Expand Up @@ -51,10 +60,22 @@
env.importMultiple = true;
}

this._importer.push(importNode.getPath(), importNode.currentFileInfo, importNode.options, function (e, root, imported, fullPath) {
this._importer.push(importNode.getPath(), importNode.currentFileInfo, importNode.options, function (e, root, importedAtRoot, fullPath) {
if (e && !e.filename) { e.index = importNode.index; e.filename = importNode.currentFileInfo.filename; }

if (imported && !env.importMultiple) { importNode.skip = imported; }
if (!env.importMultiple) {
if (importedAtRoot) {
importNode.skip = true;
} else {
importNode.skip = function() {
if (fullPath in importVisitor.onceFileDetectionMap) {
return true;
}
importVisitor.onceFileDetectionMap[fullPath] = true;
return false;
};
}
}

var subFinish = function(e) {
importVisitor.importCount--;
Expand All @@ -67,8 +88,11 @@
if (root) {
importNode.root = root;
importNode.importedFilename = fullPath;
if (!inlineCSS && !importNode.skip) {
new(tree.importVisitor)(importVisitor._importer, subFinish, env)
var duplicateImport = importedAtRoot || fullPath in importVisitor.recursionDetector;

if (!inlineCSS && (env.importMultiple || !duplicateImport)) {
importVisitor.recursionDetector[fullPath] = true;
new(tree.importVisitor)(importVisitor._importer, subFinish, env, importVisitor.onceFileDetectionMap, importVisitor.recursionDetector)
.run(root);
return;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/thirdparty/less/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ less.Parser = function Parser(env) {
var fileParsedFunc = function (e, root, fullPath) {
parserImports.queue.splice(parserImports.queue.indexOf(path), 1); // Remove the path from the queue

var importedPreviously = fullPath in parserImports.files || fullPath === rootFilename;
var importedPreviously = fullPath === rootFilename;

parserImports.files[fullPath] = root; // Store the root

Expand Down
9 changes: 8 additions & 1 deletion lib/thirdparty/less/tree/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,14 @@ tree.Import.prototype = {
eval: function (env) {
var ruleset, features = this.features && this.features.eval(env);

if (this.skip) { return []; }
if (this.skip) {
if (typeof this.skip === "function") {
this.skip = this.skip();
}
if (this.skip) {
return [];
}
}

if (this.options.inline) {
//todo needs to reference css file not import
Expand Down

0 comments on commit 694f6c4

Please sign in to comment.