-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathrollup.config.mjs
More file actions
160 lines (146 loc) · 4.26 KB
/
rollup.config.mjs
File metadata and controls
160 lines (146 loc) · 4.26 KB
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
import typescript from "@rollup/plugin-typescript";
import commonjs from "@rollup/plugin-commonjs";
import resolve from "@rollup/plugin-node-resolve";
import json from "@rollup/plugin-json";
import replace from "@rollup/plugin-replace";
import dts from "rollup-plugin-dts";
import { defineConfig } from "rollup";
const browserIncompatiblePackages = ["@shelacek/ubjson"];
// ============================================
// Helper Functions
// ============================================
/**
* Externalize npm packages and cross-directory imports
*/
const external = (id) => {
// Don't externalize rollup helpers
if (id.startsWith("\\0")) return false;
// Normalize path separators for Windows compatibility
const normalizedId = id.replace(/\\/g, "/");
// Don't externalize source files (anything with "src/" or relative paths)
if (normalizedId.includes("src/") || normalizedId.startsWith(".")) return false;
// Externalize npm packages and cross-directory imports
return true;
};
/**
* Browser-specific external function that bundles certain dependencies
*/
const browserExternal = (id) => {
for (const pkg of browserIncompatiblePackages) {
if (id === pkg || id.includes(pkg)) return false;
}
return external(id);
};
/**
* Standard output configuration
*/
const createOutput = (dir, format) => ({
dir,
format,
entryFileNames: `[name].${format === "esm" ? "esm.js" : "cjs"}`,
chunkFileNames: `[name].${format === "esm" ? "esm.js" : "cjs"}`,
sourcemap: "hidden",
exports: "named",
// This fixes the lodash ESM import issue as described here:
// https://github.com/project-slippi/slippi-js/issues/168
paths:
format !== "esm"
? undefined
: (id) => {
if (id.startsWith("lodash/")) {
return id + ".js";
}
return id;
},
});
/**
* Standard plugin configuration
*/
const plugins = [
typescript({
tsconfig: "./tsconfig.json",
compilerOptions: {
declaration: false,
declarationMap: false,
declarationDir: undefined,
},
}),
resolve({ preferBuiltins: true }),
json(),
commonjs(),
];
/**
* Browser-specific plugin configuration
*/
const browserPlugins = [
typescript({
tsconfig: "./tsconfig.json",
compilerOptions: {
declaration: false,
declarationMap: false,
declarationDir: undefined,
},
}),
resolve({
preferBuiltins: false,
browser: true,
}),
json(),
commonjs({
include: /node_modules/,
}),
// The @shelacek/ubjson package has a runtime check that will require("util") if it detects
// that the encoders are not available globally. This check should never happen in the browser.
// This does break static import analysis done by some bundlers so it's problematic so we remove
// the require call entirely.
replace({
preventAssignment: false,
delimiters: ["", ""],
values: {
'require("util")': "{}",
"require('util')": "{}",
},
}),
];
export default defineConfig([
// ============================================
// Browser Build (bundled with dependencies)
// ============================================
{
input: { "browser/index": "src/browser/index.ts" },
output: [createOutput("dist", "esm"), createOutput("dist", "cjs")],
external: browserExternal,
plugins: browserPlugins,
},
// ============================================
// Node Build (includes common + node via imports)
// ============================================
{
input: { "node/index": "src/node/index.ts" },
output: [createOutput("dist", "esm"), createOutput("dist", "cjs")],
external,
plugins,
},
// ============================================
// TypeScript Declarations
// ============================================
{
input: {
index: "src/browser/index.ts",
"index.node": "src/node/index.ts",
},
output: { dir: "dist", format: "esm" },
external,
plugins: [
dts({
// Remove respectExternal: true as it can cause the plugin to hang
// when trying to resolve complex type dependencies.
// The external function already handles what should be externalized.
compilerOptions: {
// Skip lib checks to speed up type resolution
skipLibCheck: true,
},
}),
],
},
]);