forked from yarnpkg/berry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollup.config.js
156 lines (148 loc) Β· 4.5 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
import cjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
import path from 'path';
import esbuild from 'rollup-plugin-esbuild';
import {defineConfig} from 'rollup';
import semver from 'semver';
import {brotliCompressSync} from 'zlib';
import pkg from './package.json';
/**
* @returns {import('rollup').Plugin}
*/
function wrapOutput() {
return {
name: `wrap-output`,
generateBundle(options, bundle, isWrite) {
const bundles = Object.keys(bundle);
if (bundles.length !== 1)
throw new Error(`Expected only one bundle, got ${bundles.length}`);
const outputBundle = bundle[bundles[0]];
outputBundle.code = `let hook;\n\nmodule.exports = () => {\n if (typeof hook === \`undefined\`)\n hook = require('zlib').brotliDecompressSync(Buffer.from('${brotliCompressSync(
outputBundle.code.replace(/\r\n/g, `\n`),
).toString(`base64`)}', 'base64')).toString();\n\n return hook;\n};\n`;
},
};
}
/**
* Before https://github.com/nodejs/node/pull/46904 using a custom global URL class
* wasn't supported by `fileURLToPath` so this plugin ensures that for Node.js < 20
* we always use the builtin URL class.
* TODO: Remove this plugin when dropping support for Node.js < 20
* @returns {import('rollup').Plugin}
*/
function importURL() {
return {
name: `import-url`,
resolveId(id) {
if (id === `virtual:url`) return `\0virtual:url`;
return undefined;
},
load(id) {
if (id === `\0virtual:url`) {
return `
import { URL as nodeURL } from 'url';
export const URL = Number(process.versions.node.split('.', 1)[0]) < 20 ? nodeURL : globalThis.URL;
`;
}
return undefined;
},
transform(code, id) {
if (code.includes(`new URL`) || code.includes(`instanceof URL`))
return `import {URL} from 'virtual:url';\n${code}`;
return undefined;
},
};
}
// eslint-disable-next-line arca/no-default-export
export default defineConfig([
{
input: `./sources/loader/_entryPoint.ts`,
output: {
file: `./sources/hook.js`,
format: `cjs`,
exports: `default`,
strict: false,
generatedCode: `es2015`,
},
plugins: [
resolve({
extensions: [`.mjs`, `.js`, `.ts`, `.tsx`, `.json`],
rootDir: path.join(__dirname, `../../`),
jail: path.join(__dirname, `../../`),
preferBuiltins: true,
}),
esbuild({
tsconfig: false,
target: `node${semver.minVersion(pkg.engines.node).version}`,
define: {
document: `undefined`,
XMLHttpRequest: `undefined`,
crypto: `undefined`,
},
}),
cjs({transformMixedEsModules: true, extensions: [`.js`, `.ts`]}),
importURL(),
wrapOutput(),
],
},
{
input: `./sources/esm-loader/loader.ts`,
output: {
file: `./sources/esm-loader/built-loader.js`,
format: `esm`,
generatedCode: `es2015`,
banner: `/* eslint-disable */\n// @ts-nocheck\n`,
},
external: [
`../.pnp.cjs`,
],
plugins: [
resolve({
extensions: [`.mjs`, `.js`, `.ts`, `.tsx`, `.json`],
rootDir: path.join(__dirname, `../../`),
jail: path.join(__dirname, `../../`),
preferBuiltins: true,
}),
esbuild({
tsconfig: false,
target: `node${semver.minVersion(pkg.engines.node).version}`,
define: {
document: `undefined`,
XMLHttpRequest: `undefined`,
crypto: `undefined`,
},
}),
cjs({requireReturnsDefault: `preferred`}),
importURL(),
wrapOutput(),
],
},
...[`index`, `microkernel`].map(name =>
defineConfig({
input: `./sources/${name}.ts`,
output: {
file: `./lib/${name}.js`,
format: `cjs`,
generatedCode: `es2015`,
},
plugins: [
resolve({
extensions: [`.mjs`, `.js`, `.ts`, `.tsx`, `.json`],
rootDir: path.join(__dirname, `../../`),
jail: path.join(__dirname, `../../`),
preferBuiltins: true,
}),
esbuild({
tsconfig: false,
target: `node${semver.minVersion(pkg.engines.node).version}`,
define: {
document: `undefined`,
XMLHttpRequest: `undefined`,
crypto: `undefined`,
},
}),
cjs({transformMixedEsModules: true, extensions: [`.js`, `.ts`]}),
],
}),
),
]);