Skip to content

Commit 3805034

Browse files
committed
Improve Rollup setup
1 parent 99d4b86 commit 3805034

File tree

11 files changed

+424
-203
lines changed

11 files changed

+424
-203
lines changed

package.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,9 @@
7575
"yaml-unist-parser": "1.0.0"
7676
},
7777
"devDependencies": {
78-
"@babel/cli": "7.2.0",
7978
"@babel/core": "7.2.0",
80-
"@babel/preset-env": "7.2.0",
81-
"babel-loader": "8.0.4",
79+
"@babel/preset-env": "7.4.5",
80+
"babel-loader": "8.0.6",
8281
"benchmark": "2.1.4",
8382
"builtin-modules": "2.0.0",
8483
"codecov": "codecov/codecov-node#e427d900309adb50746a39a50aa7d80071a5ddd0",

scripts/build/bundler.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const babel = require("rollup-plugin-babel");
1515
const nativeShims = require("./rollup-plugins/native-shims");
1616
const executable = require("./rollup-plugins/executable");
1717
const evaluate = require("./rollup-plugins/evaluate");
18+
const externals = require("./rollup-plugins/externals");
1819

1920
const EXTERNALS = [
2021
"assert",
@@ -122,13 +123,14 @@ function getRollupConfig(bundle) {
122123
bundle.commonjs
123124
)
124125
),
126+
externals(bundle.externals),
125127
bundle.target === "universal" && nodeGlobals(),
126128
babelConfig && babel(babelConfig),
127129
bundle.type === "plugin" && terser()
128130
].filter(Boolean);
129131

130132
if (bundle.target === "node") {
131-
config.external = EXTERNALS.concat(bundle.external);
133+
config.external = EXTERNALS;
132134
}
133135

134136
return config;

scripts/build/config.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const PROJECT_ROOT = path.resolve(__dirname, "../..");
1212
* @property {'core' | 'plugin'} type - it's a plugin bundle or core part of prettier
1313
* @property {'rollup' | 'webpack'} [bundler='rollup'] - define which bundler to use
1414
* @property {CommonJSConfig} [commonjs={}] - options for `rollup-plugin-commonjs`
15+
* @property {string[]} externals - array of paths that should not be included in the final bundle
1516
* @property {Object.<string, string>} replace - map of strings to replace when processing the bundle
1617
* @property {string[]} babelPlugins - babel plugins
1718
@@ -108,6 +109,7 @@ const coreBundles = [
108109
input: "index.js",
109110
type: "core",
110111
target: "node",
112+
externals: [path.resolve("src/common/third-party.js")],
111113
replace: {
112114
// from @iarna/toml/parse-string
113115
"eval(\"require('util').inspect\")": "require('util').inspect"
@@ -130,7 +132,8 @@ const coreBundles = [
130132
input: "bin/prettier.js",
131133
type: "core",
132134
output: "bin-prettier.js",
133-
target: "node"
135+
target: "node",
136+
externals: [path.resolve("src/common/third-party.js")]
134137
},
135138
{
136139
input: "src/common/third-party.js",
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"use strict";
2+
3+
const path = require("path");
4+
5+
module.exports = function(modules = []) {
6+
const requires = modules.reduce((obj, mod) => {
7+
obj[mod] = path.basename(mod).replace(/\.js$/, "");
8+
return obj;
9+
}, {});
10+
11+
return {
12+
name: "externals",
13+
14+
load(importee) {
15+
if (requires[importee]) {
16+
return `export default eval("require")("./${requires[importee]}");`;
17+
}
18+
}
19+
};
20+
};

src/cli/util.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const constant = require("./constant");
1717
const coreOptions = require("../main/core-options");
1818
const optionsModule = require("../main/options");
1919
const optionsNormalizer = require("../main/options-normalizer");
20-
const thirdParty = eval("require")("../common/third-party");
20+
const thirdParty = require("../common/third-party");
2121
const arrayify = require("../utils/arrayify");
2222
const isTTY = require("../utils/is-tty");
2323

src/common/load-plugins.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const fs = require("fs");
55
const globby = require("globby");
66
const path = require("path");
77
const resolve = require("resolve");
8-
const thirdParty = eval("require")("./third-party");
8+
const thirdParty = require("./third-party");
99
const internalPlugins = require("./internal-plugins");
1010
const partition = require("../utils/partition");
1111

src/common/third-party.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"use strict";
22

3-
exports.cosmiconfig = require("cosmiconfig");
4-
exports.findParentDir = require("find-parent-dir").sync;
5-
exports.getStream = require("get-stream");
6-
exports.isCI = () => require("is-ci");
3+
module.exports = {
4+
cosmiconfig: require("cosmiconfig"),
5+
findParentDir: require("find-parent-dir").sync,
6+
getStream: require("get-stream"),
7+
isCI: () => require("is-ci")
8+
};

src/config/resolve-config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22

3-
const thirdParty = eval("require")("../common/third-party");
3+
const thirdParty = require("../common/third-party");
44
const minimatch = require("minimatch");
55
const resolve = require("resolve");
66
const path = require("path");

src/doc/index.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"use strict";
22

3-
exports.builders = require("./doc-builders");
4-
exports.printer = require("./doc-printer");
5-
exports.utils = require("./doc-utils");
6-
exports.debug = require("./doc-debug");
3+
module.exports = {
4+
builders: require("./doc-builders"),
5+
printer: require("./doc-printer"),
6+
utils: require("./doc-utils"),
7+
debug: require("./doc-debug")
8+
};

src/utils/is-tty.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22

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

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

0 commit comments

Comments
 (0)