forked from Lilly-Protocol/lily-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck-browser-build.mjs
More file actions
43 lines (37 loc) · 1.25 KB
/
Copy pathcheck-browser-build.mjs
File metadata and controls
43 lines (37 loc) · 1.25 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
import { existsSync, readFileSync } from 'node:fs';
import { builtinModules } from 'node:module';
const packageJson = JSON.parse(
readFileSync(new URL('../package.json', import.meta.url), 'utf8'),
);
const exportEntries = Object.entries(packageJson.exports).filter(
([name]) => name !== './package.json',
);
const nodeBuiltinPattern = new RegExp(
`(?:from\\s*|import\\s*\\()(['\"])(?:node:)?(?:${builtinModules
.filter((name) => !name.startsWith('_'))
.map((name) => name.replace('/', '\\/'))
.join('|')})(?:\\/[^'\"]*)?\\1`,
);
for (const [name, conditions] of exportEntries) {
if (
typeof conditions !== 'object' ||
typeof conditions.browser !== 'string'
) {
throw new Error(`${name} is missing a browser export condition`);
}
const browserUrl = new URL(
`..${conditions.browser.slice(1)}`,
import.meta.url,
);
if (!existsSync(browserUrl)) {
throw new Error(
`${name} browser build does not exist at ${conditions.browser}`,
);
}
const source = readFileSync(browserUrl, 'utf8');
if (nodeBuiltinPattern.test(source)) {
throw new Error(`${name} browser build imports a Node.js built-in module`);
}
await import(browserUrl);
}
console.log(`Verified ${exportEntries.length} browser exports.`);