Skip to content

Commit

Permalink
Improve Rollup setup
Browse files Browse the repository at this point in the history
  • Loading branch information
duailibe committed Jun 7, 2019
1 parent 99d4b86 commit 3805034
Show file tree
Hide file tree
Showing 11 changed files with 424 additions and 203 deletions.
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,9 @@
"yaml-unist-parser": "1.0.0"
},
"devDependencies": {
"@babel/cli": "7.2.0",
"@babel/core": "7.2.0",
"@babel/preset-env": "7.2.0",
"babel-loader": "8.0.4",
"@babel/preset-env": "7.4.5",
"babel-loader": "8.0.6",
"benchmark": "2.1.4",
"builtin-modules": "2.0.0",
"codecov": "codecov/codecov-node#e427d900309adb50746a39a50aa7d80071a5ddd0",
Expand Down
4 changes: 3 additions & 1 deletion scripts/build/bundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const babel = require("rollup-plugin-babel");
const nativeShims = require("./rollup-plugins/native-shims");
const executable = require("./rollup-plugins/executable");
const evaluate = require("./rollup-plugins/evaluate");
const externals = require("./rollup-plugins/externals");

const EXTERNALS = [
"assert",
Expand Down Expand Up @@ -122,13 +123,14 @@ function getRollupConfig(bundle) {
bundle.commonjs
)
),
externals(bundle.externals),
bundle.target === "universal" && nodeGlobals(),
babelConfig && babel(babelConfig),
bundle.type === "plugin" && terser()
].filter(Boolean);

if (bundle.target === "node") {
config.external = EXTERNALS.concat(bundle.external);
config.external = EXTERNALS;
}

return config;
Expand Down
5 changes: 4 additions & 1 deletion scripts/build/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const PROJECT_ROOT = path.resolve(__dirname, "../..");
* @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`
* @property {string[]} externals - array of paths that should not be included in the final bundle
* @property {Object.<string, string>} replace - map of strings to replace when processing the bundle
* @property {string[]} babelPlugins - babel plugins
Expand Down Expand Up @@ -108,6 +109,7 @@ const coreBundles = [
input: "index.js",
type: "core",
target: "node",
externals: [path.resolve("src/common/third-party.js")],
replace: {
// from @iarna/toml/parse-string
"eval(\"require('util').inspect\")": "require('util').inspect"
Expand All @@ -130,7 +132,8 @@ const coreBundles = [
input: "bin/prettier.js",
type: "core",
output: "bin-prettier.js",
target: "node"
target: "node",
externals: [path.resolve("src/common/third-party.js")]
},
{
input: "src/common/third-party.js",
Expand Down
20 changes: 20 additions & 0 deletions scripts/build/rollup-plugins/externals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"use strict";

const path = require("path");

module.exports = function(modules = []) {
const requires = modules.reduce((obj, mod) => {
obj[mod] = path.basename(mod).replace(/\.js$/, "");
return obj;
}, {});

return {
name: "externals",

load(importee) {
if (requires[importee]) {
return `export default eval("require")("./${requires[importee]}");`;
}
}
};
};
2 changes: 1 addition & 1 deletion src/cli/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const constant = require("./constant");
const coreOptions = require("../main/core-options");
const optionsModule = require("../main/options");
const optionsNormalizer = require("../main/options-normalizer");
const thirdParty = eval("require")("../common/third-party");
const thirdParty = require("../common/third-party");
const arrayify = require("../utils/arrayify");
const isTTY = require("../utils/is-tty");

Expand Down
2 changes: 1 addition & 1 deletion src/common/load-plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const fs = require("fs");
const globby = require("globby");
const path = require("path");
const resolve = require("resolve");
const thirdParty = eval("require")("./third-party");
const thirdParty = require("./third-party");
const internalPlugins = require("./internal-plugins");
const partition = require("../utils/partition");

Expand Down
10 changes: 6 additions & 4 deletions src/common/third-party.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"use strict";

exports.cosmiconfig = require("cosmiconfig");
exports.findParentDir = require("find-parent-dir").sync;
exports.getStream = require("get-stream");
exports.isCI = () => require("is-ci");
module.exports = {
cosmiconfig: require("cosmiconfig"),
findParentDir: require("find-parent-dir").sync,
getStream: require("get-stream"),
isCI: () => require("is-ci")
};
2 changes: 1 addition & 1 deletion src/config/resolve-config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

const thirdParty = eval("require")("../common/third-party");
const thirdParty = require("../common/third-party");
const minimatch = require("minimatch");
const resolve = require("resolve");
const path = require("path");
Expand Down
10 changes: 6 additions & 4 deletions src/doc/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"use strict";

exports.builders = require("./doc-builders");
exports.printer = require("./doc-printer");
exports.utils = require("./doc-utils");
exports.debug = require("./doc-debug");
module.exports = {
builders: require("./doc-builders"),
printer: require("./doc-printer"),
utils: require("./doc-utils"),
debug: require("./doc-debug")
};
2 changes: 1 addition & 1 deletion src/utils/is-tty.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

const thirdParty = eval("require")("../common/third-party");
const thirdParty = require("../common/third-party");

// Some CI pipelines incorrectly report process.stdout.isTTY status,
// which causes unwanted lines in the output. An additional check for isCI() helps.
Expand Down

0 comments on commit 3805034

Please sign in to comment.