Skip to content

Commit d8d84f7

Browse files
refactor: code (#453)
1 parent 3cd85df commit d8d84f7

File tree

4 files changed

+21
-15
lines changed

4 files changed

+21
-15
lines changed

src/utils.js

+18-12
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,11 @@ async function loadConfig(config, context, configPath, loaderContext) {
145145

146146
if (typeof resultConfig === 'function') {
147147
resultConfig = resultConfig(patchedContext);
148-
} else {
149-
resultConfig = { ...resultConfig, ...patchedContext };
150148
}
151149

152-
if (result.filepath) {
153-
resultConfig.file = result.filepath;
150+
resultConfig.file = result.filepath;
154151

155-
loaderContext.addDependency(result.filepath);
156-
}
152+
loaderContext.addDependency(result.filepath);
157153

158154
return resultConfig;
159155
}
@@ -173,13 +169,24 @@ function getPostcssOptions(loaderContext, config, options = {}) {
173169
loaderContext.emitError(error);
174170
}
175171

172+
const processOptionsFromConfig = { ...config };
173+
174+
// No need them
175+
delete processOptionsFromConfig.plugins;
176+
177+
const processOptionsFromOptions = { ...options };
178+
179+
// No need them
180+
delete processOptionsFromOptions.config;
181+
delete processOptionsFromOptions.plugins;
182+
176183
const processOptions = {
177184
// TODO path.resolve
178185
from: file,
179186
to: file,
180187
map: false,
181-
...config,
182-
...options,
188+
...processOptionsFromConfig,
189+
...processOptionsFromOptions,
183190
};
184191

185192
let needExecute = false;
@@ -194,10 +201,9 @@ function getPostcssOptions(loaderContext, config, options = {}) {
194201
// eslint-disable-next-line import/no-dynamic-require, global-require
195202
processOptions.parser = require(processOptions.parser);
196203
} catch (error) {
197-
// TODO improve
198204
loaderContext.emitError(
199205
new Error(
200-
`Loading PostCSS parser failed: ${error.message}\n\n(@${file})`
206+
`Loading PostCSS "${processOptions.parser}" parser failed: ${error.message}\n\n(@${file})`
201207
)
202208
);
203209
}
@@ -210,7 +216,7 @@ function getPostcssOptions(loaderContext, config, options = {}) {
210216
} catch (error) {
211217
loaderContext.emitError(
212218
new Error(
213-
`Loading PostCSS Stringifier failed: ${error.message}\n\n(@${file})`
219+
`Loading PostCSS "${processOptions.stringifier}" stringifier failed: ${error.message}\n\n(@${file})`
214220
)
215221
);
216222
}
@@ -223,7 +229,7 @@ function getPostcssOptions(loaderContext, config, options = {}) {
223229
} catch (error) {
224230
loaderContext.emitError(
225231
new Error(
226-
`Loading PostCSS Syntax failed: ${error.message}\n\n(@${file})`
232+
`Loading PostCSS "${processOptions.syntax}" syntax failed: ${error.message}\n\n(@${file})`
227233
)
228234
);
229235
}

test/options/__snapshots__/parser.test.js.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Array [
55
"ModuleBuildError: Module build failed (from \`replaced original path\`):
66
TypeError: parser is not a function",
77
"ModuleError: Module Error (from \`replaced original path\`):
8-
Loading PostCSS parser failed: Cannot find module 'unresolve' from 'src/utils.js'",
8+
Loading PostCSS \\"unresolve\\" parser failed: Cannot find module 'unresolve' from 'src/utils.js'",
99
]
1010
`;
1111

test/options/__snapshots__/stringifier.test.js.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Array [
55
"ModuleBuildError: Module build failed (from \`replaced original path\`):
66
TypeError: this.stringify is not a function",
77
"ModuleError: Module Error (from \`replaced original path\`):
8-
Loading PostCSS Stringifier failed: Cannot find module 'unresolved' from 'src/utils.js'",
8+
Loading PostCSS \\"unresolved\\" stringifier failed: Cannot find module 'unresolved' from 'src/utils.js'",
99
]
1010
`;
1111

test/options/__snapshots__/syntax.test.js.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Array [
55
"ModuleBuildError: Module build failed (from \`replaced original path\`):
66
TypeError: Cannot read property 'parse' of undefined",
77
"ModuleError: Module Error (from \`replaced original path\`):
8-
Loading PostCSS Syntax failed: Cannot find module 'unresolve' from 'src/utils.js'",
8+
Loading PostCSS \\"unresolve\\" syntax failed: Cannot find module 'unresolve' from 'src/utils.js'",
99
]
1010
`;
1111

0 commit comments

Comments
 (0)