forked from apollographql/apollo-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollup.config.js
133 lines (120 loc) · 2.85 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
import nodeResolve from '@rollup/plugin-node-resolve';
import { terser as minify } from 'rollup-plugin-terser';
import path from 'path';
const entryPoints = require('./entryPoints');
const distDir = './dist';
function isExternal(id, parentId, entryPointsAreExternal = true) {
// Rollup v2.26.8 started passing absolute id strings to this function, thanks
// apparently to https://github.com/rollup/rollup/pull/3753, so we relativize
// the id again in those cases.
if (path.isAbsolute(id)) {
const posixId = toPosixPath(id);
const posixParentId = toPosixPath(parentId);
id = path.posix.relative(
path.posix.dirname(posixParentId),
posixId,
);
if (!id.startsWith(".")) {
id = "./" + id;
}
}
const isRelative =
id.startsWith("./") ||
id.startsWith("../");
if (!isRelative) {
return true;
}
if (entryPointsAreExternal &&
entryPoints.check(id, parentId)) {
return true;
}
return false;
}
// Adapted from https://github.com/meteor/meteor/blob/devel/tools/static-assets/server/mini-files.ts
function toPosixPath(p) {
// Sometimes, you can have a path like \Users\IEUser on windows, and this
// actually means you want C:\Users\IEUser
if (p[0] === '\\') {
p = process.env.SystemDrive + p;
}
p = p.replace(/\\/g, '/');
if (p[1] === ':') {
// Transform "C:/bla/bla" to "/c/bla/bla"
p = '/' + p[0] + p.slice(2);
}
return p;
}
function prepareCJS(input, output) {
return {
input,
external(id, parentId) {
return isExternal(id, parentId, false);
},
output: {
file: output,
format: 'cjs',
sourcemap: true,
exports: 'named',
externalLiveBindings: false,
},
plugins: [
nodeResolve(),
],
};
}
function prepareCJSMinified(input) {
return {
input,
output: {
file: input.replace('.cjs', '.min.cjs'),
format: 'cjs',
},
plugins: [
minify({
mangle: {
toplevel: true,
},
compress: {
toplevel: true,
global_defs: {
'@__DEV__': 'false',
},
},
}),
],
};
}
function prepareBundle({
dirs,
bundleName = dirs[dirs.length - 1],
extensions,
}) {
const dir = path.join(distDir, ...dirs);
return {
input: `${dir}/index.js`,
external(id, parentId) {
return isExternal(id, parentId, true);
},
output: {
file: `${dir}/${bundleName}.cjs`,
format: 'cjs',
sourcemap: true,
exports: 'named',
externalLiveBindings: false,
},
plugins: [
extensions ? nodeResolve({ extensions }) : nodeResolve(),
],
};
}
export default [
...entryPoints.map(prepareBundle),
// Convert the ESM entry point to a single CJS bundle.
prepareCJS(
'./dist/index.js',
'./dist/apollo-client.cjs',
),
prepareCJSMinified(
'./dist/apollo-client.cjs',
),
];