This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
forked from colyseus/colyseus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.mjs
113 lines (95 loc) · 3.39 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
import fs from 'fs';
import path from 'path';
import util from 'util';
import { fileURLToPath } from 'url';
import minimist from 'minimist';
import typescript from '@rollup/plugin-typescript';
import { nodeResolve } from '@rollup/plugin-node-resolve'
import commonJs from '@rollup/plugin-commonjs'
import externals from 'rollup-plugin-node-externals';
import { getPackages } from '@lerna/project';
import { filterPackages } from '@lerna/filter-packages';
import batchPackages from '@lerna/batch-packages';
// we need to change up how __dirname is used for ES6 purposes
const __dirname = path.dirname(fileURLToPath(import.meta.url));
/**
* Get a list of the non-private sorted packages
*/
async function getSortedPackages(scope, ignore) {
const packages = await getPackages(__dirname);
const filtered = filterPackages(packages,
scope,
ignore,
false);
return batchPackages(filtered)
.reduce((arr, batch) => arr.concat(batch), []);
}
async function main() {
// Support --scope and --ignore globs if passed in via commandline
const { scope, ignore } = minimist(process.argv.slice(2));
const packages = await getSortedPackages(scope, ignore);
const configs = packages.map(pkg => {
// Absolute path to package directory
const basePath = path.relative(__dirname, pkg.location);
// "main" field from package.json file.
const pkgJSON = pkg.toJSON();
// Absolute path to input file
const input = path.join(basePath, pkgJSON.input);
// Skip rollup build if package has "build" configured.
if (pkgJSON.scripts?.build) {
console.log(pkgJSON.name, "has custom build! skipping rollup build.");
return;
}
// Copy README.md and LICENSE into child package folder.
if (!fs.existsSync(path.join(basePath, "README.md"))) {
fs.copyFileSync(path.resolve(__dirname, "README.md"), path.join(basePath, "README.md"));
fs.copyFileSync(path.resolve(__dirname, "LICENSE"), path.join(basePath, "LICENSE"));
}
const tsconfig = {
rootDir: path.join(basePath, "src"),
declarationDir: path.join(basePath, "build"),
declaration: true,
include: [path.join(basePath, "src", "**", "*.ts")],
};
//
// Here's the individual rollup.config.js for each package
// Uses two separate builds: one for CJS and other for ESM.
//
return [{
input,
preserveModules: true,
output: [
{ dir: path.join(basePath, 'build'), format: 'cjs', sourcemap: true },
],
plugins: [
externals({ deps: true, peerDeps: true, devDeps: true, packagePath: path.join(basePath, "package.json"), }),
nodeResolve(),
commonJs(),
typescript({
...tsconfig,
module: "ESNext",
target: "ESNext",
}),
],
}, {
input,
preserveModules: true,
output: [
{ dir: path.join(basePath, 'build'), format: 'esm', entryFileNames: '[name].mjs', sourcemap: true },
],
plugins: [
externals({ deps: true, peerDeps: true, devDeps: true, packagePath: path.join(basePath, "package.json"), }),
nodeResolve(),
commonJs(),
typescript({
...tsconfig,
module: "ESNext",
target: "ESNext",
}),
],
}];
});
console.log("ROLLUP CONFIGS:", util.inspect(configs, false, Infinity, true));
return configs.filter(c => c !== undefined);
}
export default (await main()).flat();