-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.base.config.mjs
217 lines (193 loc) · 4.82 KB
/
rollup.base.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
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
import fs from "fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { builtinModules } from "node:module";
import commonjs from "@rollup/plugin-commonjs";
import { nodeResolve } from "@rollup/plugin-node-resolve";
import json from "@rollup/plugin-json"; //native solution coming: https://nodejs.org/docs/latest/api/esm.html#esm_json_modules
import replace from "@rollup/plugin-replace";
import virtual from "@rollup/plugin-virtual";
import esbuild from "rollup-plugin-esbuild";
import { getRuleUrl } from "./src/utils/get-rule-url.mjs";
const rootDir = fileURLToPath(new URL(".", import.meta.url));
const packageJson = JSON.parse(fs.readFileSync(path.join(rootDir, "package.json"), "utf-8"));
const externalDependencies = packageJson.externalDependencies;
const peerDependencies = Object.keys(packageJson.peerDependencies);
/**
* @typedef {import('rollup').RollupOptions} RollupOptions
*/
/** @type {string[]} */
const entrypoints = [
"src/index.ts",
"src/browser.ts",
"src/html5.ts",
"src/cli/html-validate.ts",
"src/jest/jest.ts",
"src/vitest/vitest.ts",
"src/transform/test-utils.ts",
];
/** @type {string[]} */
const external = [
/* nodejs */
...builtinModules,
...builtinModules.map((name) => `node:${name}`), //spec: https://nodejs.org/docs/latest/api/esm.html#esm_node_imports
/* npm dependencies */
...externalDependencies,
...peerDependencies,
];
const jsonConfig = {
preferConst: true,
};
/**
* @param {string} id
* @returns {boolean}
*/
function isNodejsChunk(relativeId) {
const files = ["config/loaders/file-system.ts", "utils/require-uncached.ts"];
return (
files.includes(relativeId) ||
relativeId.startsWith("config/resolver/nodejs/") ||
relativeId.endsWith("src/resolve") ||
relativeId.endsWith(".nodejs.ts")
);
}
/**
* @param {string} id
* @returns {boolean}
*/
function isBrowserChunk(relativeId) {
return relativeId.endsWith(".browser.ts");
}
/**
* @param {string} rel
* @returns {string}
*/
function jestChunks(rel) {
if (rel.startsWith("jest/matchers/")) {
if (rel.includes("codeframe")) {
return "matchers-jestonly";
} else {
return "matchers";
}
}
/* this is a special case where the jest-diff import should only be present
* when importing though this entrypoint, e.g. vitest should not have to rely
* on jest-diff being installed */
if (rel.startsWith("jest/utils/diff")) {
return "jest-diff";
}
if (rel.startsWith("jest/")) {
return "matcher-utils";
}
return "core";
}
/**
* @param {string} id
* @returns {string|undefined}
*/
/* eslint-disable-next-line complexity -- needed to be like this */
function manualChunks(id) {
/** @type {string} */
const base = path.relative(rootDir, id).replace(/\\/g, "/");
if (entrypoints.includes(base)) {
return undefined;
}
/** @type {string} */
const rel = base.startsWith("src/")
? path.relative(path.join(rootDir, "src"), id).replace(/\\/g, "/")
: path.relative(path.join(rootDir, "dist/types"), id).replace(/\\/g, "/");
if (rel.startsWith("cli/")) {
return "cli";
}
if (rel.startsWith("elements/")) {
return "elements";
}
if (rel.startsWith("meta/helper") || rel.startsWith("meta/define-metadata")) {
return "meta-helper";
}
if (isNodejsChunk(rel)) {
return "core-nodejs";
}
if (isBrowserChunk(rel)) {
return "core-browser";
}
if (rel.startsWith("utils")) {
const parsed = path.parse(rel);
const split = ["natural-join"];
if (split.includes(parsed.name)) {
return `utils/${parsed.name}`;
} else {
return "core";
}
}
if (rel.startsWith("jest/")) {
return jestChunks(rel);
}
return "core";
}
/**
* @param {string} format
* @returns {string}
*/
function generateResolved(format) {
if (format === "es") {
return `
import { createRequire } from "node:module";
export const legacyRequire = createRequire(import.meta.url);
`;
} else {
return `
export const legacyRequire = require;
`;
}
}
/**
* @param {string} format
* @returns {RollupOptions[]}
*/
export function build(format) {
return [
{
input: entrypoints,
output: {
dir: `dist/${format}`,
format,
sourcemap: true,
manualChunks,
chunkFileNames: "[name].js",
interop: "auto",
},
treeshake: {
preset: "smallest",
},
preserveEntrySignatures: "strict",
external,
plugins: [
virtual({
"src/resolve": generateResolved(format),
}),
esbuild({
target: "node16",
platform: "node",
}),
json(jsonConfig),
commonjs(),
nodeResolve(),
replace({
preventAssignment: true,
delimiters: ["", ""],
values: {
/**
* Replace __filename global with source filename relative to dist folder
*
* @param {string} filename
*/
"ruleDocumentationUrl(__filename)"(filename) {
return JSON.stringify(getRuleUrl(filename, packageJson.homepage));
},
},
}),
],
},
];
}