Skip to content

Commit

Permalink
fix(#3294): use loadFileSync when loading plugins with syncImport: tr…
Browse files Browse the repository at this point in the history
…ue (#3506)

* fix(#3294): use loadFileSync when loading plugins with syncImport: true

* test: add tests for sync plugins
  • Loading branch information
Justineo committed Jun 17, 2020
1 parent 95b9007 commit e018ba8
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 10 deletions.
8 changes: 8 additions & 0 deletions lib/less-node/plugin-loader.js
Expand Up @@ -30,6 +30,10 @@ class PluginLoader extends AbstractPluginLoader {
context.prefixes = ['less-plugin-', ''];
}

if (context.syncImport) {
return fileManager.loadFileSync(filename, basePath, context, environment);
}

return new Promise((fulfill, reject) => {
fileManager.loadFile(filename, basePath, context, environment).then(
data => {
Expand All @@ -45,7 +49,11 @@ class PluginLoader extends AbstractPluginLoader {
reject(err);
});
});
}

loadPluginSync(filename, basePath, context, environment, fileManager) {
context.syncImport = true;
return this.loadPlugin(filename, basePath, context, environment, fileManager);
}
}

Expand Down
36 changes: 26 additions & 10 deletions lib/less/import-manager.js
Expand Up @@ -125,6 +125,7 @@ export default environment => {
});
}
};
let loadedFile;
let promise;
const context = utils.clone(this.context);

Expand All @@ -134,19 +135,34 @@ export default environment => {

if (importOptions.isPlugin) {
context.mime = 'application/javascript';
promise = pluginLoader.loadPlugin(path, currentFileInfo.currentDirectory, context, environment, fileManager);

if (context.syncImport) {
loadedFile = pluginLoader.loadPluginSync(path, currentFileInfo.currentDirectory, context, environment, fileManager);
} else {
promise = pluginLoader.loadPlugin(path, currentFileInfo.currentDirectory, context, environment, fileManager);
}
}
else {
promise = fileManager.loadFile(path, currentFileInfo.currentDirectory, context, environment,
(err, loadedFile) => {
if (err) {
fileParsedFunc(err);
} else {
loadFileCallback(loadedFile);
}
});
if (context.syncImport) {
loadedFile = fileManager.loadFileSync(path, currentFileInfo.currentDirectory, context, environment);
} else {
promise = fileManager.loadFile(path, currentFileInfo.currentDirectory, context, environment,
(err, loadedFile) => {
if (err) {
fileParsedFunc(err);
} else {
loadFileCallback(loadedFile);
}
});
}
}
if (promise) {
if (loadedFile) {
if (!loadedFile.filename) {
fileParsedFunc(loadedFile);
} else {
loadFileCallback(loadedFile);
}
} else if (promise) {
promise.then(loadFileCallback, fileParsedFunc);
}
}
Expand Down
1 change: 1 addition & 0 deletions test/css/import.css
Expand Up @@ -7,6 +7,7 @@
color: red;
width: 10px;
height: 30%;
value: 3.141592653589793;
}
@media screen and (max-width: 600px) {
body {
Expand Down
1 change: 1 addition & 0 deletions test/index.js
Expand Up @@ -78,6 +78,7 @@ testMap.forEach(function(args) {
lessTester.runTestSet.apply(lessTester, args)
});
lessTester.testSyncronous({syncImport: true}, 'import');
lessTester.testSyncronous({syncImport: true}, 'plugin');
lessTester.testSyncronous({syncImport: true}, 'math/strict-legacy/css');
lessTester.testNoOptions();
lessTester.testJSImport();
Expand Down
3 changes: 3 additions & 0 deletions test/less/import.less
@@ -1,4 +1,6 @@
/** comment at the top**/
@plugin "./plugin/plugin-simple";

@import url(/absolute/something.css) screen and (color) and (max-width: 600px);

@import (optional) "file-does-not-exist.does-not-exist";
Expand All @@ -10,6 +12,7 @@
.mixin;
width: 10px;
height: (@a + 10%);
value: pi-anon();
}
@import "import/import-test-e" screen and (max-width: 600px);

Expand Down

0 comments on commit e018ba8

Please sign in to comment.