Skip to content

Commit

Permalink
refactor: code (#453)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Sep 1, 2020
1 parent 3cd85df commit d8d84f7
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
30 changes: 18 additions & 12 deletions src/utils.js
Expand Up @@ -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;
}
Expand All @@ -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;
Expand All @@ -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})`
)
);
}
Expand All @@ -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})`
)
);
}
Expand All @@ -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})`
)
);
}
Expand Down
2 changes: 1 addition & 1 deletion test/options/__snapshots__/parser.test.js.snap
Expand Up @@ -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'",
]
`;

Expand Down
2 changes: 1 addition & 1 deletion test/options/__snapshots__/stringifier.test.js.snap
Expand Up @@ -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'",
]
`;

Expand Down
2 changes: 1 addition & 1 deletion test/options/__snapshots__/syntax.test.js.snap
Expand Up @@ -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'",
]
`;

Expand Down

0 comments on commit d8d84f7

Please sign in to comment.