forked from apollographql/apollo-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.js
116 lines (107 loc) · 2.87 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
import nodeResolve from 'rollup-plugin-node-resolve';
import typescriptPlugin from 'rollup-plugin-typescript2';
import typescript from 'typescript';
import path from 'path';
import invariantPlugin from 'rollup-plugin-invariant';
import { terser as minify } from 'rollup-plugin-terser';
function onwarn(message) {
const suppressed = ['UNRESOLVED_IMPORT', 'THIS_IS_UNDEFINED'];
if (!suppressed.find(code => message.code === code)) {
return console.warn(message.message);
}
}
const defaultGlobals = {
'apollo-client': 'apollo.core',
'apollo-cache': 'apolloCache.core',
'apollo-link': 'apolloLink.core',
'apollo-link-dedup': 'apolloLink.dedup',
'apollo-utilities': 'apollo.utilities',
'graphql-anywhere': 'graphqlAnywhere',
'graphql-anywhere/lib/async': 'graphqlAnywhere.async',
'apollo-boost': 'apollo.boost',
'tslib': 'tslib',
'ts-invariant': 'invariant',
};
export function rollup({
name,
input = './src/index.ts',
outputPrefix = 'bundle',
extraGlobals = {},
}) {
const projectDir = path.join(__filename, '..');
console.info(`Building project esm ${projectDir}`);
const tsconfig = `${projectDir}/tsconfig.json`;
const globals = {
...defaultGlobals,
...extraGlobals,
};
function external(id) {
return Object.prototype.hasOwnProperty.call(globals, id);
}
function outputFile(format) {
return './lib/' + outputPrefix + '.' + format + '.js';
}
function convert(format) {
return {
input: outputFile('esm'),
external,
output: {
file: outputFile(format),
format,
sourcemap: true,
name,
globals,
},
onwarn,
};
}
return [
{
input,
external,
output: {
file: outputFile('esm'),
format: 'esm',
sourcemap: true,
},
plugins: [
nodeResolve({
extensions: ['.ts', '.tsx'],
module: true,
}),
typescriptPlugin({ typescript, tsconfig }),
invariantPlugin({
// Instead of completely stripping InvariantError messages in
// production, this option assigns a numeric code to the
// production version of each error (unique to the call/throw
// location), which makes it much easier to trace production
// errors back to the unminified code where they were thrown,
// where the full error string can be found. See #4519.
errorCodes: true,
}),
],
onwarn,
},
convert('umd'),
convert('cjs'),
{
input: outputFile('cjs'),
output: {
file: outputFile('cjs.min'),
format: 'cjs',
},
plugins: [
minify({
mangle: {
toplevel: true,
},
compress: {
global_defs: {
'@process.env.NODE_ENV': JSON.stringify('production'),
},
},
}),
],
},
];
}