Skip to content

refactor: code #453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions src/utils.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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