From b74f94d6dde1431f7c8b6ded0d8b572b11b495a2 Mon Sep 17 00:00:00 2001 From: Jed Fox Date: Tue, 11 Dec 2018 05:59:59 -0500 Subject: [PATCH] Remove the dynamic `require()` call in the standalone bundle (#5612) * Remove the dynamic `require()` call in the standalone bundle * Liiiint * Just use `replaceStrings` * Reword error message --- scripts/build/bundler.js | 1 + src/main/parser.js | 30 ++++++++++++++++++++---------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/scripts/build/bundler.js b/scripts/build/bundler.js index e97cd2cc5..5e95c3322 100644 --- a/scripts/build/bundler.js +++ b/scripts/build/bundler.js @@ -95,6 +95,7 @@ function getRollupConfig(bundle) { }; const replaceStrings = { + "process.env.PRETTIER_TARGET": JSON.stringify(bundle.target), "process.env.NODE_ENV": JSON.stringify("production") }; if (bundle.target === "universal") { diff --git a/src/main/parser.js b/src/main/parser.js index 914651236..e47f96e26 100644 --- a/src/main/parser.js +++ b/src/main/parser.js @@ -42,16 +42,26 @@ function resolveParser(opts, parsers) { if (parsers.hasOwnProperty(opts.parser)) { return parsers[opts.parser]; } - try { - return { - parse: eval("require")(path.resolve(process.cwd(), opts.parser)), - astFormat: "estree", - locStart, - locEnd - }; - } catch (err) { - /* istanbul ignore next */ - throw new ConfigError(`Couldn't resolve parser "${opts.parser}"`); + + /* istanbul ignore next */ + if (process.env.PRETTIER_TARGET === "universal") { + throw new ConfigError( + `Couldn't resolve parser "${ + opts.parser + }". Parsers must be explicitly added to the standalone bundle.` + ); + } else { + try { + return { + parse: eval("require")(path.resolve(process.cwd(), opts.parser)), + astFormat: "estree", + locStart, + locEnd + }; + } catch (err) { + /* istanbul ignore next */ + throw new ConfigError(`Couldn't resolve parser "${opts.parser}"`); + } } } }