-
Notifications
You must be signed in to change notification settings - Fork 119
/
rollup.js
152 lines (134 loc) · 3.82 KB
/
rollup.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import typescript from "rollup-plugin-typescript";
import nodeResolve from "@rollup/plugin-node-resolve";
import filesize from "rollup-plugin-filesize";
import { terser } from "rollup-plugin-terser";
import * as path from "path";
import * as fs from "fs";
/**
* Since Rollup runs inside each package we can just get the current
* package we are bundling.
*/
const pkg = require(path.join(process.cwd(), "package.json"));
/**
* and dig out its name.
*/
const { name } = pkg;
/**
* to construct a copyright banner
*/
const copyright = `/* @preserve
* ${pkg.name} - v${pkg.version} - ${pkg.license}
* Copyright (c) 2017-${new Date().getFullYear()} Esri, Inc.
* ${new Date().toString()}
*/`;
/**
* The module name will be the name of the global variable used in UMD builds.
* All exported members of each package will be attached to this global.
*/
const moduleName = "arcgisRest";
/**
* Now we need to discover all the `@esri/arcgis-rest-*` package names so we can create
* the `globals` and `externals` to pass to Rollup. @esri/arcgis-rest-fetch is excluded
* from this because we WANT to bundle it since it only contains references to global
* fetch
*/
const packageNames = fs
.readdirSync(path.join(__dirname, "packages"))
.filter((p) => p[0] !== ".")
.filter((p) => p !== "arcgis-rest-fetch")
.filter((p) => p !== "arcgis-rest-form-data")
.map((p) => {
return require(path.join(__dirname, "packages", p, "package.json")).name;
}, {});
/**
* Rollup will use this map to determine where to lookup modules on the global
* window object when neither AMD or CommonJS is being used. This configuration
* will cause Rollup to lookup all imports from our packages on a single global
* `arcgisRest` object.
*/
const globals = packageNames.reduce((globals, p) => {
globals[p] = moduleName;
return globals;
}, {});
const baseConfig = {
input: "./src/index.ts",
context: "window",
inlineDynamicImports: true
};
const baseOutput = {
sourcemap: true,
banner: copyright,
name: moduleName,
extend: true // causes this module to extend the global specified by `moduleName`
};
const basePlugins = function () {
return [
typescript({
target: "ES2017" // force typescript compile target
}),
nodeResolve()
];
};
const productionPlugins = function () {
return [
filesize(),
terser({
format: {
comments: function (node, comment) {
var text = comment.value;
var type = comment.type;
if (type == "comment2") {
// multiline comment
return /@preserve|@license|@cc_on/i.test(text);
}
}
}
})
];
};
const packageName = name.replace("@esri/arcgis-rest-", "");
const umdConfig = {
...baseConfig,
output: {
file: `./dist/bundled/${packageName}.umd.js`,
format: "umd",
...baseOutput,
globals
},
external: packageNames,
plugins: [...basePlugins()]
};
const umdMinConfig = {
...baseConfig,
output: {
file: `./dist/bundled/${packageName}.umd.min.js`,
format: "umd",
...baseOutput,
globals
},
external: packageNames,
plugins: [...basePlugins(), ...productionPlugins()]
};
const esmConfig = {
...baseConfig,
output: {
file: `./dist/bundled/${packageName}.esm.js`,
format: "es",
...baseOutput,
globals: name === "arcgis-rest-request" ? undefined : globals
},
external: name === "arcgis-rest-request" ? undefined : packageNames,
plugins: [...basePlugins()]
};
const esmMinConfig = {
...baseConfig,
output: {
file: `./dist/bundled/${packageName}.esm.min.js`,
format: "es",
...baseOutput,
globals: name === "arcgis-rest-request" ? undefined : globals
},
external: name === "arcgis-rest-request" ? undefined : packageNames,
plugins: [...basePlugins(), ...productionPlugins()]
};
export default [umdConfig, umdMinConfig, esmConfig, esmMinConfig];