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
master
Jed Fox 2018-12-11 05:59:59 -05:00 committed by GitHub
parent 5e89969a9e
commit b74f94d6dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 10 deletions

View File

@ -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") {

View File

@ -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}"`);
}
}
}
}