Skip to content

Commit

Permalink
Fix diagnostic for bundler config errors (#5704)
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic authored Feb 2, 2021
1 parent ac82202 commit 1188e59
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/bundlers/default/src/DefaultBundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ async function loadBundlerConfig(options: PluginOptions) {
CONFIG_SCHEMA,
{
data: config,
source: JSON.stringify(config),
source: await options.inputFS.readFile(result.files[0].filePath, 'utf8'),
filePath: result.files[0].filePath,
prependKey: `/${encodeJSONKeyComponent('@parcel/bundler-default')}`,
},
Expand Down
33 changes: 33 additions & 0 deletions packages/core/integration-tests/test/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -1853,4 +1853,37 @@ describe('html', function() {
`<img src="data:image/svg+xml,%3Csvg%20width%3D%22120%22%20height%3D%27120%27%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E%0A%20%20%3Cfilter%20id%3D%22blur-_.%21~%2a%22%3E%0A%20%20%20%20%3CfeGaussianBlur%20stdDeviation%3D%225%22%2F%3E%0A%20%20%3C%2Ffilter%3E%0A%20%20%3Ccircle%20cx%3D%2260%22%20cy%3D%2260%22%20r%3D%2250%22%20fill%3D%22green%22%20filter%3D%22url%28%23blur-_.%21~%2a%29%22%20%2F%3E%0A%3C%2Fsvg%3E%0A">`,
);
});

it('should print a diagnostic for invalid bundler options', async () => {
let dir = path.join(__dirname, 'integration/invalid-bundler-config');
let pkg = path.join(dir, 'package.json');
let code = await inputFS.readFileSync(pkg, 'utf8');
await assert.rejects(() => bundle(path.join(dir, 'index.html')), {
name: 'BuildError',
diagnostics: [
{
message: 'Invalid config for @parcel/bundler-default',
origin: '@parcel/bundler-default',
filePath: pkg,
language: 'json',
codeFrame: {
code,
codeHighlights: [
{
message: 'Did you mean "minBundleSize", "minBundles"?',
start: {
column: 30,
line: 3,
},
end: {
column: 45,
line: 3,
},
},
],
},
},
],
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Hello</h1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"browserslist": "Chrome 80",
"@parcel/bundler-default": {"minBundleSizes": 0
}
}
Empty file.
7 changes: 4 additions & 3 deletions packages/core/utils/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ export async function loadConfig(
filenames: Array<FilePath>,
opts: ?ConfigOptions,
): Promise<ConfigOutput | null> {
let parse = opts?.parse ?? true;
let configFile = await resolveConfig(fs, filepath, filenames);
if (configFile) {
let cachedOutput = configCache.get(configFile);
let cachedOutput = configCache.get(String(parse) + configFile);
if (cachedOutput) {
return cachedOutput;
}
Expand All @@ -67,7 +68,7 @@ export async function loadConfig(
if (!configContent) return null;

let config;
if (opts && opts.parse === false) {
if (parse === false) {
config = configContent;
} else {
let parse = getParser(extname);
Expand All @@ -79,7 +80,7 @@ export async function loadConfig(
files: [{filePath: configFile}],
};

configCache.set(configFile, output);
configCache.set(String(parse) + configFile, output);
return output;
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND' || err.code === 'ENOENT') {
Expand Down

0 comments on commit 1188e59

Please sign in to comment.