-
Notifications
You must be signed in to change notification settings - Fork 507
/
createRollupConfig.ts
191 lines (181 loc) · 5.69 KB
/
createRollupConfig.ts
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import { safeVariableName, safePackageName, external } from './utils';
import { paths } from './constants';
import { RollupOptions } from 'rollup';
import { terser } from 'rollup-plugin-terser';
import { DEFAULT_EXTENSIONS } from '@babel/core';
// import babel from 'rollup-plugin-babel';
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import replace from '@rollup/plugin-replace';
import resolve from '@rollup/plugin-node-resolve';
import sourceMaps from 'rollup-plugin-sourcemaps';
import typescript from 'rollup-plugin-typescript2';
import { extractErrors } from './errors/extractErrors';
import { babelPluginTsdx } from './babelPluginTsdx';
import { TsdxOptions } from './types';
import * as fs from 'fs-extra';
const errorCodeOpts = {
errorMapFilePath: paths.appErrorsJson,
};
// shebang cache map thing because the transform only gets run once
let shebang: any = {};
export async function createRollupConfig(
opts: TsdxOptions
): Promise<RollupOptions> {
const findAndRecordErrorCodes = await extractErrors({
...errorCodeOpts,
...opts,
});
const shouldMinify =
opts.minify !== undefined ? opts.minify : opts.env === 'production';
const outputName = [
`${paths.appDist}/${safePackageName(opts.name)}`,
opts.format,
opts.env,
shouldMinify ? 'min' : '',
'js',
]
.filter(Boolean)
.join('.');
let tsconfigJSON;
try {
tsconfigJSON = await fs.readJSON(paths.tsconfigJson);
} catch (e) {}
return {
// Tell Rollup the entry point to the package
input: opts.input,
// Tell Rollup which packages to ignore
external: (id: string) => {
if (id === 'babel-plugin-transform-async-to-promises/helpers') {
return false;
}
return external(id);
},
// Rollup has treeshaking by default, but we can optimize it further...
treeshake: {
// We assume reading a property of an object never has side-effects.
// This means tsdx WILL remove getters and setters defined directly on objects.
// Any getters or setters defined on classes will not be effected.
//
// @example
//
// const foo = {
// get bar() {
// console.log('effect');
// return 'bar';
// }
// }
//
// const result = foo.bar;
// const illegalAccess = foo.quux.tooDeep;
//
// Punchline....Don't use getters and setters
propertyReadSideEffects: false,
},
// Establish Rollup output
output: {
// Set filenames of the consumer's package
file: outputName,
// Pass through the file format
format: opts.format,
// Do not let Rollup call Object.freeze() on namespace import objects
// (i.e. import * as namespaceImportObject from...) that are accessed dynamically.
freeze: false,
// Respect tsconfig esModuleInterop when setting __esModule.
esModule: tsconfigJSON ? tsconfigJSON.esModuleInterop : false,
name: opts.name || safeVariableName(opts.name),
sourcemap: true,
globals: { react: 'React', 'react-native': 'ReactNative' },
exports: 'named',
},
plugins: [
!!opts.extractErrors && {
async transform(source: any) {
await findAndRecordErrorCodes(source);
return source;
},
},
resolve({
mainFields: [
'module',
'main',
opts.target !== 'node' ? 'browser' : undefined,
].filter(Boolean) as string[],
}),
opts.format === 'umd' &&
commonjs({
// use a regex to make sure to include eventual hoisted packages
include: /\/node_modules\//,
}),
json(),
{
// Custom plugin that removes shebang from code because newer
// versions of bublé bundle their own private version of `acorn`
// and I don't know a way to patch in the option `allowHashBang`
// to acorn. Taken from microbundle.
// See: https://github.com/Rich-Harris/buble/pull/165
transform(code: string) {
let reg = /^#!(.*)/;
let match = code.match(reg);
shebang[opts.name] = match ? '#!' + match[1] : '';
code = code.replace(reg, '');
return {
code,
map: null,
};
},
},
typescript({
typescript: require('typescript'),
cacheRoot: `./node_modules/.cache/tsdx/${opts.format}/`,
tsconfig: opts.tsconfig,
tsconfigDefaults: {
compilerOptions: {
sourceMap: true,
declaration: true,
jsx: 'react',
},
},
tsconfigOverride: {
compilerOptions: {
// TS -> esnext, then leave the rest to babel-preset-env
target: 'esnext',
},
},
check: opts.transpileOnly === false,
}),
babelPluginTsdx({
exclude: 'node_modules/**',
extensions: [...DEFAULT_EXTENSIONS, 'ts', 'tsx'],
passPerPreset: true,
custom: {
targets: opts.target === 'node' ? { node: '8' } : undefined,
extractErrors: opts.extractErrors,
format: opts.format,
// defines: opts.defines,
},
}),
opts.env !== undefined &&
replace({
'process.env.NODE_ENV': JSON.stringify(opts.env),
}),
sourceMaps(),
// sizeSnapshot({
// printInfo: false,
// }),
shouldMinify &&
terser({
sourcemap: true,
output: { comments: false },
compress: {
keep_infinity: true,
pure_getters: true,
passes: 10,
},
ecma: 5,
toplevel: opts.format === 'cjs',
warnings: true,
}),
],
};
}