-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollup.config.js
223 lines (187 loc) · 4.73 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import fs from "fs-extra";
import path from "path";
import externals from "rollup-plugin-node-externals";
import cjs from "@rollup/plugin-commonjs";
import nodeResolve from "@rollup/plugin-node-resolve";
import json from "@rollup/plugin-json";
import { startService } from "esbuild";
import { createFilter } from "@rollup/pluginutils";
const defaultLoaders = {
".js": "js",
".jsx": "jsx",
".ts": "ts",
".tsx": "tsx"
};
const esbuildRollupPlugin = (options = {}) => {
let target;
const loaders = {
...defaultLoaders
};
if (options.loaders) {
for (const key of Object.keys(options.loaders)) {
const value = options.loaders[key];
if (typeof value === "string") {
loaders[key] = value;
} else if (value === false) {
delete loaders[key];
}
}
}
const extensions = Object.keys(loaders);
const INCLUDE_REGEXP = new RegExp(`\\.(${extensions.map(ext => ext.slice(1)).join("|")})$`);
const EXCLUDE_REGEXP = /node_modules/;
const filter = createFilter(options.include || INCLUDE_REGEXP, options.exclude || EXCLUDE_REGEXP);
let service;
const stopService = () => {
if (service) {
service.stop();
service = undefined;
}
};
// The order is:
// buildStart -> resolveId -> transform -> buildEnd -> renderChunk -> generateBundle
const resolveFile = (resolved, index = false) => {
for (const ext of extensions) {
const file = index ? path.join(resolved, `index${ext}`) : `${resolved}${ext}`;
if (fs.existsSync(file)) return file;
}
return null;
};
return {
name: "esbuild",
async buildStart() {
if (!service) {
service = await startService();
}
},
resolveId(importee, importer) {
if (importer && importee[0] === ".") {
const resolved = path.resolve(importer ? path.dirname(importer) : process.cwd(), importee);
let file = resolveFile(resolved);
if (file) return file;
if (!file && fs.existsSync(resolved) && fs.statSync(resolved).isDirectory()) {
file = resolveFile(resolved, true);
if (file) return file;
}
}
},
async transform(code, id) {
if (!filter(id)) {
return null;
}
const ext = path.extname(id);
const loader = loaders[ext];
if (!loader || !service) {
return null;
}
const defaultOptions = {};
target = options.target || defaultOptions.target || "esnext";
const result = await service.transform(code, {
loader,
target,
jsxFactory: options.jsxFactory || defaultOptions.jsxFactory,
jsxFragment: options.jsxFragment || defaultOptions.jsxFragment,
define: options.define,
sourcemap: options.sourceMap !== false,
sourcefile: id
});
printWarnings(id, result, this);
return result;
},
buildEnd(error) {
// Stop the service early if there's error
if (error && !this.meta.watchMode) {
stopService();
}
},
async renderChunk(code) {
if (options.minify && service) {
const result = await service.transform(code, {
loader: "js",
minify: true,
target
});
if (result.cpde) {
return result;
}
}
return null;
},
generateBundle() {
if (!this.meta.watchMode) {
stopService();
}
}
};
};
function printWarnings(id, result, plugin) {
if (result.warnings) {
for (const warning of result.warnings) {
let message = `[esbuild]`;
if (warning.location) {
message += ` (${path.relative(process.cwd(), id)}:${warning.location.line}:${warning.location.column})`;
}
message += ` ${warning.text}`;
plugin.warn(message);
}
}
}
const packagesRoot = path.resolve(__dirname, "./packages/");
const pkgDirList = fs.readdirSync(packagesRoot);
function resolvePkgInput(pkgDir) {
return path.resolve(packagesRoot, pkgDir, "./lib/index.ts");
}
function resolvePkgOutput(pkgDir, format) {
return path.resolve(packagesRoot, pkgDir, `./dist/index.${format === "es" ? "mjs" : "js"}`);
}
function getPkgDeps(pkgDir) {
const meta = fs.readJSONSync(path.resolve(packagesRoot, pkgDir, "package.json"));
return {
...(meta.dependencies || {}),
...(meta.peerDependencies || {}),
...(meta.devDependencies || {})
};
}
const utilsDeps = getPkgDeps("utils");
const configs = pkgDirList.map(pkgDir => {
const deps = {
...getPkgDeps(pkgDir),
...utilsDeps
};
return {
input: resolvePkgInput(pkgDir),
output: [
{
format: "cjs",
file: resolvePkgOutput(pkgDir),
sourcemap: true
},
{
format: "es",
file: resolvePkgOutput(pkgDir, "es"),
sourcemap: true
}
],
plugins: [
json({
namedExports: false
}),
nodeResolve({
preferBuiltins: false
}),
cjs(),
externals(),
esbuildRollupPlugin()
],
external: Object.keys(deps),
onwarn: (msg, warn) => {
if (!/Circular/.test(msg)) {
warn(msg);
}
},
treeshake: {
moduleSideEffects: false
}
};
});
export default configs;