Skip to content

Commit

Permalink
fix(less#3294): use loadFileSync when loading plugins with syncImport…
Browse files Browse the repository at this point in the history
…: true
  • Loading branch information
Justineo committed May 29, 2020
1 parent 0f271f3 commit 074b828
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
8 changes: 8 additions & 0 deletions lib/less-node/plugin-loader.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export default environment => {
}
}
};
let loadedFile;
let promise;
const context = utils.clone(this.context);

Expand All @@ -147,19 +148,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

0 comments on commit 074b828

Please sign in to comment.