-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathrollup.config.js
136 lines (124 loc) · 3.25 KB
/
rollup.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const fs = require("fs");
const path = require("path");
const commonjs = require("@rollup/plugin-commonjs");
const resolve = require("@rollup/plugin-node-resolve");
const terser = require("@rollup/plugin-terser");
const istanbul = require("rollup-plugin-istanbul");
const typescript = require("@rollup/plugin-typescript");
const pkg = require("./package.json");
const banner = `/*!
* ${pkg.name} v${pkg.version}
* ${pkg.repository.url}
* (c) 2018-${new Date().getFullYear()} ${pkg.name} contributors
* Released under the ${pkg.license} license
*/`;
/**
* Create a rollup configuration for a given file
* @param {string} options.file the input file
* @param {import("rollup").ModuleFormat} options.format the format of the output module
* @param {boolean} options.terse whether to run terser plugin
* @param {boolean} options.bTestBuild whether to run istanbul plugin (if true) for coverage or to strip testing exports (if false)
* @param {boolean} options.bBundleD3 whether to bundle D3 plugins or to reference them as externals
* @returns {import('rollup').RollupOptions} the built options
*/
function bundleDragDataPlugin({
file,
format,
terse,
bTestBuild = false,
bBundleD3 = true,
}) {
/** @type {import('rollup').RollupOptions} */
const customOptions = {
input: "src/index.ts",
external: [
"chart.js",
"chart.js/helpers",
...(bBundleD3 ? [] : ["d3-drag", "d3-selection"]),
],
output: {
exports: "named",
banner,
name: "ChartJSDragDataPlugin",
file,
format,
globals: {
"chart.js": "Chart",
"chart.js/helpers": "Chart.helpers",
},
},
plugins: [
...(format === "umd" ? [commonjs()] : []),
resolve({
browser: true,
}),
...(bTestBuild
? process.env.DISABLE_ISTANBUL_COVERAGE_AT_BUILD !== "true"
? [
// in a test build, inject istanbul and keep testing exports
istanbul({
exclude: ["node_modules/**/*"],
}),
]
: []
: []),
terse ? terser() : undefined,
typescript({
tsconfig: "./tsconfig.build.json",
compilerOptions: {
outDir: path.dirname(file),
},
}),
{
// copy index.d.ts to file matching the bundle filename for jest tests to pick up typings
closeBundle() {
if (bTestBuild) {
const dir = path.dirname(file);
fs.mkdirSync(dir, { recursive: true });
fs.copyFileSync(
path.join(dir, "index.d.ts"),
file.replace(".js", ".d.ts"),
);
}
},
},
],
};
return customOptions;
}
/** @type {import('rollup').RollupOptions[]} */
const config = [
bundleDragDataPlugin({
file: pkg.main,
format: "umd",
terse: false,
}),
bundleDragDataPlugin({
file: pkg.browser,
format: "umd",
terse: true,
}),
bundleDragDataPlugin({
file: pkg.module,
format: "esm",
terse: true,
}),
// bundle for E2E testing: istanbul + bundled D3 (for browser)
bundleDragDataPlugin({
file: pkg.main
.replace(".js", "-test-browser.js")
.replace("dist/", "dist/test/"),
format: "umd",
terse: false,
bTestBuild: true,
}),
// bundle for unit/integration testing: istanbul + external D3
bundleDragDataPlugin({
file: pkg.main.replace(".js", "-test.js").replace("dist/", "dist/test/"),
format: "es",
terse: false,
bTestBuild: true,
bBundleD3: false,
}),
];
module.exports = config;