2018-05-24 15:30:45 -03:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
const path = require("path");
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @typedef {Object} Bundle
|
|
|
|
|
* @property {string} input - input of the bundle
|
|
|
|
|
* @property {string?} output - path of the output file in the `dist/` folder
|
|
|
|
|
* @property {string?} name - name for the UMD bundle (for plugins, it'll be `prettierPlugins.${name}`)
|
|
|
|
|
* @property {'node' | 'universal'} target - should generate a CJS only for node or UMD bundle
|
|
|
|
|
* @property {'core' | 'plugin'} type - it's a plugin bundle or core part of prettier
|
|
|
|
|
* @property {'rollup' | 'webpack'} [bundler='rollup'] - define which bundler to use
|
|
|
|
|
* @property {CommonJSConfig} [commonjs={}] - options for `rollup-plugin-commonjs`
|
2019-06-07 18:20:40 -03:00
|
|
|
* @property {string[]} externals - array of paths that should not be included in the final bundle
|
2018-05-24 15:30:45 -03:00
|
|
|
* @property {Object.<string, string>} replace - map of strings to replace when processing the bundle
|
2018-06-21 10:29:42 +08:00
|
|
|
* @property {string[]} babelPlugins - babel plugins
|
2019-09-16 17:13:32 +08:00
|
|
|
* @property {Object?} terserOptions - options for `terser`
|
2020-05-11 18:00:22 +08:00
|
|
|
* @property {boolean?} minify - minify
|
2018-05-24 15:30:45 -03:00
|
|
|
|
|
|
|
|
* @typedef {Object} CommonJSConfig
|
|
|
|
|
* @property {string[]} ignore - paths of CJS modules to ignore
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/** @type {Bundle[]} */
|
|
|
|
|
const parsers = [
|
|
|
|
|
{
|
2020-03-21 22:07:52 +02:00
|
|
|
input: "src/language-js/parser-babel.js",
|
2018-05-24 15:30:45 -03:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
input: "src/language-js/parser-flow.js",
|
2020-03-21 22:07:52 +02:00
|
|
|
strict: false,
|
2018-05-24 15:30:45 -03:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
input: "src/language-js/parser-typescript.js",
|
|
|
|
|
},
|
2018-11-04 23:03:07 +08:00
|
|
|
{
|
|
|
|
|
input: "src/language-js/parser-angular.js",
|
|
|
|
|
},
|
2018-05-24 15:30:45 -03:00
|
|
|
{
|
|
|
|
|
input: "src/language-css/parser-postcss.js",
|
|
|
|
|
// postcss has dependency cycles that don't work with rollup
|
2019-09-16 17:13:32 +08:00
|
|
|
bundler: "webpack",
|
|
|
|
|
terserOptions: {
|
2019-10-30 20:33:52 +08:00
|
|
|
// prevent terser generate extra .LICENSE file
|
|
|
|
|
extractComments: false,
|
|
|
|
|
terserOptions: {
|
2020-06-12 06:42:42 +05:30
|
|
|
// prevent U+FFFE in the output
|
|
|
|
|
output: {
|
|
|
|
|
ascii_only: true,
|
|
|
|
|
},
|
2019-10-30 20:33:52 +08:00
|
|
|
mangle: {
|
|
|
|
|
// postcss need keep_fnames when minify
|
2020-01-29 08:03:36 +08:00
|
|
|
keep_fnames: true,
|
|
|
|
|
// we don't transform class anymore, so we need keep_classnames too
|
2020-03-21 22:07:52 +02:00
|
|
|
keep_classnames: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2018-05-24 15:30:45 -03:00
|
|
|
},
|
|
|
|
|
{
|
2020-03-21 22:07:52 +02:00
|
|
|
input: "src/language-graphql/parser-graphql.js",
|
2018-05-24 15:30:45 -03:00
|
|
|
},
|
|
|
|
|
{
|
2020-03-21 22:07:52 +02:00
|
|
|
input: "src/language-markdown/parser-markdown.js",
|
2018-05-24 15:30:45 -03:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
input: "src/language-handlebars/parser-glimmer.js",
|
|
|
|
|
commonjs: {
|
2020-03-21 22:07:52 +02:00
|
|
|
ignore: ["source-map"],
|
|
|
|
|
},
|
2018-05-24 15:30:45 -03:00
|
|
|
},
|
|
|
|
|
{
|
2020-03-21 22:07:52 +02:00
|
|
|
input: "src/language-html/parser-html.js",
|
2018-06-21 10:29:42 +08:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
input: "src/language-yaml/parser-yaml.js",
|
2020-03-21 22:07:52 +02:00
|
|
|
},
|
|
|
|
|
].map((parser) => ({
|
2020-01-20 18:49:52 +08:00
|
|
|
type: "plugin",
|
2020-01-29 17:45:57 +08:00
|
|
|
target: "universal",
|
2020-03-21 22:07:52 +02:00
|
|
|
name: getFileOutput(parser).replace(/\.js$/, "").split("-")[1],
|
|
|
|
|
...parser,
|
2020-01-20 18:49:52 +08:00
|
|
|
}));
|
2018-05-24 15:30:45 -03:00
|
|
|
|
|
|
|
|
/** @type {Bundle[]} */
|
2018-05-29 14:59:57 -03:00
|
|
|
const coreBundles = [
|
2018-05-24 15:30:45 -03:00
|
|
|
{
|
|
|
|
|
input: "index.js",
|
|
|
|
|
type: "core",
|
|
|
|
|
target: "node",
|
2019-06-07 18:20:40 -03:00
|
|
|
externals: [path.resolve("src/common/third-party.js")],
|
2019-05-17 10:15:45 -03:00
|
|
|
replace: {
|
|
|
|
|
// from @iarna/toml/parse-string
|
2020-03-21 22:07:52 +02:00
|
|
|
"eval(\"require('util').inspect\")": "require('util').inspect",
|
|
|
|
|
},
|
2018-05-24 15:30:45 -03:00
|
|
|
},
|
2019-03-25 09:44:22 -07:00
|
|
|
{
|
2020-01-14 18:58:42 +08:00
|
|
|
input: "src/document/index.js",
|
2019-03-25 09:44:22 -07:00
|
|
|
name: "doc",
|
|
|
|
|
type: "core",
|
|
|
|
|
output: "doc.js",
|
2020-03-21 22:07:52 +02:00
|
|
|
target: "universal",
|
2020-05-11 18:00:22 +08:00
|
|
|
minify: false,
|
2019-03-25 09:44:22 -07:00
|
|
|
},
|
2018-05-24 15:30:45 -03:00
|
|
|
{
|
|
|
|
|
input: "standalone.js",
|
|
|
|
|
name: "prettier",
|
|
|
|
|
type: "core",
|
2020-03-21 22:07:52 +02:00
|
|
|
target: "universal",
|
2020-07-17 09:06:00 +08:00
|
|
|
// TODO: Find a better way to remove parsers
|
|
|
|
|
replace: Object.fromEntries(
|
|
|
|
|
parsers.map(({ name }) => [`require("./parser-${name}")`, "({})"])
|
|
|
|
|
),
|
2018-05-24 15:30:45 -03:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
input: "bin/prettier.js",
|
|
|
|
|
type: "core",
|
|
|
|
|
output: "bin-prettier.js",
|
2019-06-07 18:20:40 -03:00
|
|
|
target: "node",
|
2020-05-06 18:43:04 +08:00
|
|
|
externals: [
|
|
|
|
|
path.resolve("src/index.js"),
|
|
|
|
|
path.resolve("src/common/third-party.js"),
|
|
|
|
|
],
|
2018-05-24 15:30:45 -03:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
input: "src/common/third-party.js",
|
|
|
|
|
type: "core",
|
|
|
|
|
target: "node",
|
|
|
|
|
replace: {
|
2018-11-28 17:49:33 -08:00
|
|
|
// cosmiconfig@5 -> import-fresh uses `require` to resolve js config, which caused Error:
|
2018-07-29 16:57:36 +08:00
|
|
|
// Dynamic requires are not currently supported by rollup-plugin-commonjs.
|
2018-11-28 17:49:33 -08:00
|
|
|
"require(filePath)": "eval('require')(filePath)",
|
2020-03-22 00:46:01 +02:00
|
|
|
"parent.eval('require')(filePath)": "parent.require(filePath)",
|
2020-01-10 09:21:44 +08:00
|
|
|
"require.cache": "eval('require').cache",
|
|
|
|
|
// cosmiconfig@6 -> import-fresh can't find parentModule, since module is bundled
|
2020-03-21 22:07:52 +02:00
|
|
|
"parentModule(__filename)": "__filename",
|
|
|
|
|
},
|
|
|
|
|
},
|
2018-05-29 14:59:57 -03:00
|
|
|
];
|
2018-05-24 15:30:45 -03:00
|
|
|
|
|
|
|
|
function getFileOutput(bundle) {
|
|
|
|
|
return bundle.output || path.basename(bundle.input);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-21 22:07:52 +02:00
|
|
|
module.exports = coreBundles.concat(parsers).map((bundle) => ({
|
2020-01-20 18:49:52 +08:00
|
|
|
...bundle,
|
2020-03-21 22:07:52 +02:00
|
|
|
output: getFileOutput(bundle),
|
2020-01-20 18:49:52 +08:00
|
|
|
}));
|