From 1188e59f87ec8d899152932d7c3eec1b18cc2579 Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com> Date: Tue, 2 Feb 2021 20:17:18 +0100 Subject: [PATCH] Fix diagnostic for bundler config errors (#5704) --- .../bundlers/default/src/DefaultBundler.js | 2 +- packages/core/integration-tests/test/html.js | 33 +++++++++++++++++++ .../invalid-bundler-config/index.html | 1 + .../invalid-bundler-config/package.json | 5 +++ .../invalid-bundler-config/yarn.lock | 0 packages/core/utils/src/config.js | 7 ++-- 6 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 packages/core/integration-tests/test/integration/invalid-bundler-config/index.html create mode 100644 packages/core/integration-tests/test/integration/invalid-bundler-config/package.json create mode 100644 packages/core/integration-tests/test/integration/invalid-bundler-config/yarn.lock diff --git a/packages/bundlers/default/src/DefaultBundler.js b/packages/bundlers/default/src/DefaultBundler.js index 56be5f70306..42601ce94c0 100644 --- a/packages/bundlers/default/src/DefaultBundler.js +++ b/packages/bundlers/default/src/DefaultBundler.js @@ -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')}`, }, diff --git a/packages/core/integration-tests/test/html.js b/packages/core/integration-tests/test/html.js index 8c00c052172..4b15a125ea7 100644 --- a/packages/core/integration-tests/test/html.js +++ b/packages/core/integration-tests/test/html.js @@ -1853,4 +1853,37 @@ describe('html', function() { ``, ); }); + + 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, + }, + }, + ], + }, + }, + ], + }); + }); }); diff --git a/packages/core/integration-tests/test/integration/invalid-bundler-config/index.html b/packages/core/integration-tests/test/integration/invalid-bundler-config/index.html new file mode 100644 index 00000000000..986a4a1a25b --- /dev/null +++ b/packages/core/integration-tests/test/integration/invalid-bundler-config/index.html @@ -0,0 +1 @@ +

Hello

diff --git a/packages/core/integration-tests/test/integration/invalid-bundler-config/package.json b/packages/core/integration-tests/test/integration/invalid-bundler-config/package.json new file mode 100644 index 00000000000..b4cdccaa310 --- /dev/null +++ b/packages/core/integration-tests/test/integration/invalid-bundler-config/package.json @@ -0,0 +1,5 @@ +{ + "browserslist": "Chrome 80", + "@parcel/bundler-default": {"minBundleSizes": 0 + } +} diff --git a/packages/core/integration-tests/test/integration/invalid-bundler-config/yarn.lock b/packages/core/integration-tests/test/integration/invalid-bundler-config/yarn.lock new file mode 100644 index 00000000000..e69de29bb2d diff --git a/packages/core/utils/src/config.js b/packages/core/utils/src/config.js index 314cae8adc0..d45a3f9b583 100644 --- a/packages/core/utils/src/config.js +++ b/packages/core/utils/src/config.js @@ -43,9 +43,10 @@ export async function loadConfig( filenames: Array, opts: ?ConfigOptions, ): Promise { + 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; } @@ -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); @@ -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') {