diff --git a/package.json b/package.json index 792405c3bd8a..d092df6dec31 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "camelcase": "4.1.0", "chalk": "2.1.0", "cjk-regex": "1.0.2", - "cosmiconfig": "3.1.0", + "cosmiconfig": "5.0.5", "dashify": "0.2.2", "dedent": "0.7.0", "diff": "3.2.0", diff --git a/scripts/build/build.js b/scripts/build/build.js index d1543c8c0083..aeb21b97bc55 100644 --- a/scripts/build/build.js +++ b/scripts/build/build.js @@ -98,7 +98,7 @@ async function run(params) { await execa("rm", ["-rf", ".cache"]); } - const bundleCache = new Cache(".cache/", "v2"); + const bundleCache = new Cache(".cache/", "v3"); await bundleCache.load(); console.log(chalk.inverse(" Building packages ")); diff --git a/scripts/build/config.js b/scripts/build/config.js index 86db60185f27..ae1143eed243 100644 --- a/scripts/build/config.js +++ b/scripts/build/config.js @@ -113,13 +113,9 @@ const coreBundles = [ type: "core", target: "node", replace: { - // The require-from-string module (a dependency of cosmiconfig) assumes - // that `module.parent` exists, but it only does for `require`:ed modules. - // Usually, require-from-string is _always_ `require`:ed, but when bundled - // with rollup the module is turned into a plain function located directly - // in index.js so `module.parent` does not exist. Defaulting to `module` - // instead seems to work. - "module.parent": "(module.parent || module)" + // cosmiconfig@5 uses `require` to resolve js config, which caused Error: + // Dynamic requires are not currently supported by rollup-plugin-commonjs. + "require(filepath)": "eval('require')(filepath)" } } ]; diff --git a/src/config/resolve-config.js b/src/config/resolve-config.js index c8f0f43433ab..b3ae94f1fa80 100644 --- a/src/config/resolve-config.js +++ b/src/config/resolve-config.js @@ -7,19 +7,33 @@ const mem = require("mem"); const resolveEditorConfig = require("./resolve-config-editorconfig"); -const getExplorerMemoized = mem(opts => - thirdParty.cosmiconfig("prettier", { - sync: opts.sync, +const getExplorerMemoized = mem(opts => { + const explorer = thirdParty.cosmiconfig("prettier", { cache: opts.cache, - rcExtensions: true, transform: result => { if (result && result.config) { + if (typeof result.config !== "object") { + throw new Error( + `Config is only allowed to be an object, ` + + `but received ${typeof result.config} in "${result.filepath}"` + ); + } + delete result.config.$schema; } return result; } - }) -); + }); + + const load = opts.sync ? explorer.loadSync : explorer.load; + const search = opts.sync ? explorer.searchSync : explorer.search; + + return { + // cosmiconfig v4 interface + load: (searchPath, configPath) => + configPath ? load(configPath) : search(searchPath) + }; +}); /** @param {{ cache: boolean, sync: boolean }} opts */ function getLoadFunction(opts) { diff --git a/tests_integration/__tests__/__snapshots__/config-invalid.js.snap b/tests_integration/__tests__/__snapshots__/config-invalid.js.snap index b5f6964d2aa7..6c27f81bf72a 100644 --- a/tests_integration/__tests__/__snapshots__/config-invalid.js.snap +++ b/tests_integration/__tests__/__snapshots__/config-invalid.js.snap @@ -19,7 +19,7 @@ exports[`show warning with unknown option (stdout) 1`] = `""`; exports[`show warning with unknown option (write) 1`] = `Array []`; exports[`throw error with invalid config format (stderr) 1`] = ` -"[error] Invalid configuration file: Failed to parse \\"/tests_integration/cli/config/invalid/file/.prettierrc\\" as JSON, JS, or YAML. +"[error] Invalid configuration file: Config is only allowed to be an object, but received string in \\"/tests_integration/cli/config/invalid/file/.prettierrc\\" " `; diff --git a/yarn.lock b/yarn.lock index 4da894405c15..0a4d8d28df4d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1504,14 +1504,13 @@ core-util-is@~1.0.0: version "1.0.2" resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7" -cosmiconfig@3.1.0: - version "3.1.0" - resolved "https://registry.yarnpkg.com/cosmiconfig/-/cosmiconfig-3.1.0.tgz#640a94bf9847f321800403cd273af60665c73397" +cosmiconfig@5.0.5: + version "5.0.5" + resolved "https://registry.yarnpkg.com/cosmiconfig/-/cosmiconfig-5.0.5.tgz#a809e3c2306891ce17ab70359dc8bdf661fe2cd0" dependencies: is-directory "^0.3.1" js-yaml "^3.9.0" - parse-json "^3.0.0" - require-from-string "^2.0.1" + parse-json "^4.0.0" create-ecdh@^4.0.0: version "4.0.0" @@ -3592,6 +3591,10 @@ json-loader@^0.5.4: version "0.5.4" resolved "https://registry.yarnpkg.com/json-loader/-/json-loader-0.5.4.tgz#8baa1365a632f58a3c46d20175fc6002c96e37de" +json-parse-better-errors@^1.0.1: + version "1.0.2" + resolved "https://registry.yarnpkg.com/json-parse-better-errors/-/json-parse-better-errors-1.0.2.tgz#bb867cfb3450e69107c131d1c514bab3dc8bcaa9" + json-schema-traverse@^0.3.0: version "0.3.1" resolved "https://registry.yarnpkg.com/json-schema-traverse/-/json-schema-traverse-0.3.1.tgz#349a6d44c53a51de89b40805c5d5e59b417d3340" @@ -4308,11 +4311,12 @@ parse-json@^2.2.0: dependencies: error-ex "^1.2.0" -parse-json@^3.0.0: - version "3.0.0" - resolved "https://registry.yarnpkg.com/parse-json/-/parse-json-3.0.0.tgz#fa6f47b18e23826ead32f263e744d0e1e847fb13" +parse-json@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/parse-json/-/parse-json-4.0.0.tgz#be35f5425be1f7f6c747184f98a788cb99477ee0" dependencies: error-ex "^1.3.1" + json-parse-better-errors "^1.0.1" parse5@3.0.3: version "3.0.3" @@ -4873,10 +4877,6 @@ require-directory@^2.1.1: version "2.1.1" resolved "https://registry.yarnpkg.com/require-directory/-/require-directory-2.1.1.tgz#8c64ad5fd30dab1c976e2344ffe7f792a6a6df42" -require-from-string@^2.0.1: - version "2.0.1" - resolved "https://registry.yarnpkg.com/require-from-string/-/require-from-string-2.0.1.tgz#c545233e9d7da6616e9d59adfb39fc9f588676ff" - require-main-filename@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/require-main-filename/-/require-main-filename-1.0.1.tgz#97f717b69d48784f5f526a6c5aa8ffdda055a4d1"