-
Notifications
You must be signed in to change notification settings - Fork 18
/
rollup.config.mjs
164 lines (151 loc) · 4.5 KB
/
rollup.config.mjs
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
import { readFileSync } from "fs";
import { babel } from "@rollup/plugin-babel";
import commonjs from "@rollup/plugin-commonjs";
import json from "@rollup/plugin-json";
import resolve from "@rollup/plugin-node-resolve";
import replace from "@rollup/plugin-replace";
const extensions = [".mjs", ".js", ".jsx", ".ts", ".tsx"];
const plugins = [
replace({
preventAssignment: true,
values: {
"process.env.VERSION": JSON.stringify(process.env.VERSION || "VERSION"),
},
}),
resolve({
extensions,
}),
commonjs({
include: ["node_modules/**", "packages/**/node_modules/**"],
}),
babel({
extensions,
exclude: ["node_modules/**", "packages/**/node_modules/**"],
babelHelpers: "runtime",
}),
json({
preferConst: true,
indent: " ",
}),
];
// This function is the external function of rollup configuration.
// The effect is to tell rollup to treat @babel/* depdendencies as external.
// The main purpose to is debug bundle content.
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function makeBabelExternal(id) {
return /^@babel/.test(id);
}
function makeReactNativeExternal() {
const reactNativePackageJSONString = readFileSync(
"packages/authgear-react-native/package.json",
{ encoding: "utf8" }
);
const reactNativePackageJSON = JSON.parse(reactNativePackageJSONString);
const deps = Object.keys(reactNativePackageJSON["dependencies"] || {});
if (deps.length > 0) {
throw new Error("@authgear/react-native should not have any depdendencies");
}
const peerDeps = Object.keys(
reactNativePackageJSON["peerDependencies"] || []
);
function external(id) {
return peerDeps.indexOf(id) >= 0;
}
return external;
}
function makeCapacitorExternal() {
const packageJSONString = readFileSync(
"packages/authgear-capacitor/package.json",
{ encoding: "utf8" }
);
const packageJSON = JSON.parse(packageJSONString);
const deps = Object.keys(packageJSON["depdendencies"] || {});
if (deps.length > 0) {
throw new Error("@authgear/capacitor should not have any dependencies");
}
const peerDeps = Object.keys(packageJSON["peerDependencies"] || []);
function external(id) {
return peerDeps.indexOf(id) >= 0;
}
return external;
}
export default function makeConfig(commandLineArgs) {
const configBundleType = commandLineArgs.configBundleType;
switch (configBundleType) {
case "iife":
return {
plugins,
input: "packages/authgear-web/src/index.ts",
output: {
file: "packages/authgear-web/dist/authgear-web.iife.js",
format: "iife",
name: "authgear",
exports: "named",
},
};
case "web-cjs":
return {
plugins,
input: "packages/authgear-web/src/index.ts",
output: {
file: "packages/authgear-web/dist/authgear-web.cjs.js",
format: "cjs",
exports: "named",
},
// external: makeBabelExternal,
};
case "web-module":
return {
plugins,
input: "packages/authgear-web/src/index.ts",
output: {
file: "packages/authgear-web/dist/authgear-web.module.js",
format: "esm",
},
// external: makeBabelExternal,
};
case "react-native":
return {
plugins,
input: "packages/authgear-react-native/src/index.ts",
output: {
file: "packages/authgear-react-native/dist/authgear-react-native.js",
format: "cjs",
exports: "named",
},
external: makeReactNativeExternal(),
};
case "capacitor":
return {
plugins,
input: "packages/authgear-capacitor/src/index.ts",
output: [
{
file: "packages/authgear-capacitor/dist/plugin.iife.js",
format: "iife",
name: "authgearCapacitor",
globals: {
"@capacitor/core": "capacitorExports",
},
sourcemap: true,
inlineDynamicImports: true,
},
{
file: "packages/authgear-capacitor/dist/plugin.cjs.js",
format: "cjs",
sourcemap: true,
inlineDynamicImports: true,
},
{
file: "packages/authgear-capacitor/dist/plugin.module.js",
format: "esm",
sourcemap: true,
inlineDynamicImports: true,
},
],
external: makeCapacitorExternal(),
};
default:
throw new Error("unknown bundle type: " + configBundleType);
}
}