From 074b828717cef2e02d5a098957018a802cae72e2 Mon Sep 17 00:00:00 2001 From: Justineo Date: Fri, 29 May 2020 18:55:10 +0800 Subject: [PATCH] fix(#3294): use loadFileSync when loading plugins with syncImport: true --- lib/less-node/plugin-loader.js | 8 ++++++++ lib/less/import-manager.js | 36 ++++++++++++++++++++++++---------- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/lib/less-node/plugin-loader.js b/lib/less-node/plugin-loader.js index 8bdcb42ef..e55218192 100644 --- a/lib/less-node/plugin-loader.js +++ b/lib/less-node/plugin-loader.js @@ -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 => { @@ -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); } } diff --git a/lib/less/import-manager.js b/lib/less/import-manager.js index 471484505..d811d04a5 100644 --- a/lib/less/import-manager.js +++ b/lib/less/import-manager.js @@ -138,6 +138,7 @@ export default environment => { } } }; + let loadedFile; let promise; const context = utils.clone(this.context); @@ -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); } }