-
Notifications
You must be signed in to change notification settings - Fork 267
/
rollup.config.js
158 lines (147 loc) · 4.37 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import replace from '@rollup/plugin-replace';
import alias from '@rollup/plugin-alias';
import cleanup from 'rollup-plugin-cleanup';
import typescript from 'rollup-plugin-typescript2';
import license from 'rollup-plugin-license';
import multiInput from 'rollup-plugin-multi-input';
import { visualizer } from 'rollup-plugin-visualizer';
import { existsSync, mkdirSync, writeFileSync } from 'fs';
import pkg from './package.json';
const path = require('path');
let platforms = ['browser', 'node'];
let entries = {
'okta-auth-js': 'lib/exports/default.ts',
'core': 'lib/exports/core.ts',
'authn': 'lib/exports/authn.ts',
'idx': 'lib/exports/idx.ts',
'myaccount': 'lib/exports/myaccount.ts'
};
let preserveModules = true;
const combinedOutputDir = true; // all entries share an output dir
function getOuptutDir(entryName, env) {
return combinedOutputDir ? `build/esm/${env}` : `build/esm/${entryName}/${env}`;
}
// if ENTRY env var is passed, filter the entries to include only the named ENTRY
if (process.env.ENTRY) {
entries = {
[process.env.ENTRY]: entries[process.env.ENTRY]
};
}
// if PLATFORM env var is passed, filter the platforms to include only the named PLATFORM
if (process.env.PLATFORM) {
platforms = platforms.filter(platform => platform === process.env.PLATFORM);
}
// if ANALZYE env var is passed, output analyzer html (must output single bundle)
if (process.env.ANALYZE) {
preserveModules = false;
}
const makeExternalPredicate = (env) => {
const externalArr = [
...Object.keys(pkg.peerDependencies || {}),
...Object.keys(pkg.dependencies || {}),
];
if (env === 'node') {
externalArr.push('crypto');
}
if (externalArr.length === 0) {
return () => false;
}
const pattern = new RegExp(`^(${externalArr.join('|')})($|/)`);
return id => pattern.test(id);
};
const extensions = ['js', 'ts'];
const output = {
format: 'es',
exports: 'named',
sourcemap: true,
preserveModules,
// not using .mjs extension because it causes issues with Vite
// entryFileNames: '[name].mjs'
};
function createPackageJson(dirName) {
return {
name: 'create-package-json',
generateBundle() {
// Add an extra package.json underneath ESM to indicate module type
// This helps tools like Jest identify this code as ESM
if (!existsSync(dirName)) {
mkdirSync(dirName, { recursive: true });
}
writeFileSync(`${dirName}/package.json`, JSON.stringify({
name: pkg.name,
version: pkg.version,
type: 'module'
}, null, 4));
}
};
}
const getPlugins = (env, entryName) => {
const outputDir = getOuptutDir(entryName, env);
let plugins = [
replace({
'SDK_VERSION': JSON.stringify(pkg.version),
...(env === 'browser' && { 'global.': 'window.' }),
preventAssignment: true
}),
(env === 'browser' && alias({
entries: [
{ find: /.\/node$/, replacement: './browser' }
]
})),
typescript({
// eslint-disable-next-line node/no-unpublished-require
typescript: require('typescript'),
tsconfigOverride: {
compilerOptions: {
sourceMap: true,
target: 'ES2017', // skip async/await transpile,
module: 'ES2020', // support dynamic import
declaration: false
}
}
}),
cleanup({
extensions,
comments: 'none'
}),
license({
banner: {
content: {
file: path.join(__dirname, 'scripts', 'buildtools', 'license-template'),
}
}
}),
multiInput({
relative: 'lib/',
}),
createPackageJson(outputDir)
];
// if ANALZYE env var is passed, output analyzer html
if (process.env.ANALYZE) {
plugins = plugins.concat([
visualizer({
sourcemap: true,
projectRoot: path.join(__dirname, './lib'),
filename: `./build/esm/${entryName}.${env}.analzyer.html`,
template: 'treemap' // sunburst | treemap | network
}),
]);
}
return plugins;
};
export default Object.keys(entries).reduce((res, entryName) => {
const entryValue = entries[entryName];
return res.concat(platforms.map((type) => {
return {
input: Array.isArray(entryValue) ? entryValue : [entryValue],
external: makeExternalPredicate(type),
plugins: getPlugins(type, entryName),
output: [
{
...output,
dir: getOuptutDir(entryName, type)
}
]
};
}));
}, []);