From d8d84f7e90295acf835a0d7fb7f81c0bb9ca4589 Mon Sep 17 00:00:00 2001 From: Evilebot Tnawi Date: Tue, 1 Sep 2020 17:32:45 +0300 Subject: [PATCH] refactor: code (#453) --- src/utils.js | 30 +++++++++++-------- .../options/__snapshots__/parser.test.js.snap | 2 +- .../__snapshots__/stringifier.test.js.snap | 2 +- .../options/__snapshots__/syntax.test.js.snap | 2 +- 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/utils.js b/src/utils.js index 6957302c..b1860461 100644 --- a/src/utils.js +++ b/src/utils.js @@ -145,15 +145,11 @@ async function loadConfig(config, context, configPath, loaderContext) { if (typeof resultConfig === 'function') { resultConfig = resultConfig(patchedContext); - } else { - resultConfig = { ...resultConfig, ...patchedContext }; } - if (result.filepath) { - resultConfig.file = result.filepath; + resultConfig.file = result.filepath; - loaderContext.addDependency(result.filepath); - } + loaderContext.addDependency(result.filepath); return resultConfig; } @@ -173,13 +169,24 @@ function getPostcssOptions(loaderContext, config, options = {}) { loaderContext.emitError(error); } + const processOptionsFromConfig = { ...config }; + + // No need them + delete processOptionsFromConfig.plugins; + + const processOptionsFromOptions = { ...options }; + + // No need them + delete processOptionsFromOptions.config; + delete processOptionsFromOptions.plugins; + const processOptions = { // TODO path.resolve from: file, to: file, map: false, - ...config, - ...options, + ...processOptionsFromConfig, + ...processOptionsFromOptions, }; let needExecute = false; @@ -194,10 +201,9 @@ function getPostcssOptions(loaderContext, config, options = {}) { // eslint-disable-next-line import/no-dynamic-require, global-require processOptions.parser = require(processOptions.parser); } catch (error) { - // TODO improve loaderContext.emitError( new Error( - `Loading PostCSS parser failed: ${error.message}\n\n(@${file})` + `Loading PostCSS "${processOptions.parser}" parser failed: ${error.message}\n\n(@${file})` ) ); } @@ -210,7 +216,7 @@ function getPostcssOptions(loaderContext, config, options = {}) { } catch (error) { loaderContext.emitError( new Error( - `Loading PostCSS Stringifier failed: ${error.message}\n\n(@${file})` + `Loading PostCSS "${processOptions.stringifier}" stringifier failed: ${error.message}\n\n(@${file})` ) ); } @@ -223,7 +229,7 @@ function getPostcssOptions(loaderContext, config, options = {}) { } catch (error) { loaderContext.emitError( new Error( - `Loading PostCSS Syntax failed: ${error.message}\n\n(@${file})` + `Loading PostCSS "${processOptions.syntax}" syntax failed: ${error.message}\n\n(@${file})` ) ); } diff --git a/test/options/__snapshots__/parser.test.js.snap b/test/options/__snapshots__/parser.test.js.snap index 27f15c19..9150c341 100644 --- a/test/options/__snapshots__/parser.test.js.snap +++ b/test/options/__snapshots__/parser.test.js.snap @@ -5,7 +5,7 @@ Array [ "ModuleBuildError: Module build failed (from \`replaced original path\`): TypeError: parser is not a function", "ModuleError: Module Error (from \`replaced original path\`): -Loading PostCSS parser failed: Cannot find module 'unresolve' from 'src/utils.js'", +Loading PostCSS \\"unresolve\\" parser failed: Cannot find module 'unresolve' from 'src/utils.js'", ] `; diff --git a/test/options/__snapshots__/stringifier.test.js.snap b/test/options/__snapshots__/stringifier.test.js.snap index 53e7331d..d1381c82 100644 --- a/test/options/__snapshots__/stringifier.test.js.snap +++ b/test/options/__snapshots__/stringifier.test.js.snap @@ -5,7 +5,7 @@ Array [ "ModuleBuildError: Module build failed (from \`replaced original path\`): TypeError: this.stringify is not a function", "ModuleError: Module Error (from \`replaced original path\`): -Loading PostCSS Stringifier failed: Cannot find module 'unresolved' from 'src/utils.js'", +Loading PostCSS \\"unresolved\\" stringifier failed: Cannot find module 'unresolved' from 'src/utils.js'", ] `; diff --git a/test/options/__snapshots__/syntax.test.js.snap b/test/options/__snapshots__/syntax.test.js.snap index 346a551e..278434e2 100644 --- a/test/options/__snapshots__/syntax.test.js.snap +++ b/test/options/__snapshots__/syntax.test.js.snap @@ -5,7 +5,7 @@ Array [ "ModuleBuildError: Module build failed (from \`replaced original path\`): TypeError: Cannot read property 'parse' of undefined", "ModuleError: Module Error (from \`replaced original path\`): -Loading PostCSS Syntax failed: Cannot find module 'unresolve' from 'src/utils.js'", +Loading PostCSS \\"unresolve\\" syntax failed: Cannot find module 'unresolve' from 'src/utils.js'", ] `;