|
1 | 1 | import { readdir, readFile } from 'node:fs/promises'; |
2 | | -import { resolve } from 'node:path'; |
| 2 | +import { dirname, relative, resolve } from 'node:path'; |
3 | 3 |
|
4 | 4 | const distDir = resolve(import.meta.dirname, '../dist'); |
5 | 5 | const jsFiles = (await readdir(distDir, { recursive: true })).filter(file => file.endsWith('.js')); |
6 | 6 | const sources = await Promise.all(jsFiles.map(file => readFile(resolve(distDir, file), 'utf8'))); |
7 | | -const componentEntries = (await readdir(resolve(distDir, 'components'))) |
8 | | - .filter(file => file.endsWith('.js')) |
9 | | - .map(file => `components/${file}`); |
10 | | - |
11 | | -for (const entryFile of ['index.js', ...componentEntries]) { |
12 | | - const entry = await readFile(resolve(distDir, entryFile), 'utf8'); |
13 | | - if (!entry.startsWith("'use client';") && !entry.startsWith('"use client";')) { |
14 | | - throw new Error(`Published Mosaic JS entry ${entryFile} is missing a top-level 'use client' boundary.`); |
| 7 | +const packageJson = JSON.parse(await readFile(resolve(import.meta.dirname, '../package.json'), 'utf8')); |
| 8 | +const entryFiles = Object.entries(packageJson.exports) |
| 9 | + .filter(([subpath]) => subpath === '.' || /^\.\/(components|primitives)\//.test(subpath)) |
| 10 | + .map(([, target]) => target) |
| 11 | + .map(target => (typeof target === 'string' ? target : target.import)) |
| 12 | + .filter(file => file?.endsWith('.js')) |
| 13 | + .map(file => resolve(import.meta.dirname, '..', file)); |
| 14 | + |
| 15 | +const isClientModule = source => source.startsWith("'use client';") || source.startsWith('"use client";'); |
| 16 | +const importsOf = source => |
| 17 | + [...source.matchAll(/^import\s+(?:[^'"]*?\s+from\s+)?["']([^"']+)["'];?$/gm)].map(m => m[1]); |
| 18 | +const importsReact = source => |
| 19 | + importsOf(source).some(specifier => specifier === 'react' || specifier.startsWith('react/')); |
| 20 | +const code = source => |
| 21 | + source |
| 22 | + .replace(/\/\/.*$/gm, '') |
| 23 | + .split('\n') |
| 24 | + .map(line => line.trim()) |
| 25 | + .filter(Boolean); |
| 26 | + |
| 27 | +const checkNamespaceEntry = async (entryFile, source) => { |
| 28 | + const display = relative(distDir, entryFile); |
| 29 | + const lines = code(source); |
| 30 | + const namespaceImport = /^import \{ (\w+) \} from "(\.[^"]+)";$/; |
| 31 | + const namespaceExport = /^export \{ (\w+ as \w+(?:, \w+ as \w+)*) \};$/; |
| 32 | + if (!lines.every(line => namespaceImport.test(line) || namespaceExport.test(line))) { |
| 33 | + throw new Error( |
| 34 | + `Published Mosaic JS entry ${display} must start with 'use client' or contain only \`export * as\` namespace re-exports.`, |
| 35 | + ); |
| 36 | + } |
| 37 | + for (const line of lines.filter(line => namespaceImport.test(line))) { |
| 38 | + const [, binding, specifier] = line.match(namespaceImport); |
| 39 | + const partsFile = resolve(dirname(entryFile), specifier); |
| 40 | + const parts = await readFile(partsFile, 'utf8'); |
| 41 | + if (isClientModule(parts) || !new RegExp(`^var ${binding} = /\\* @__PURE__ \\*/ __exportAll\\(`, 'm').test(parts)) { |
| 42 | + throw new Error( |
| 43 | + `${display} imports ${binding} from ${relative(distDir, partsFile)}, which must be a namespace object with no directive.`, |
| 44 | + ); |
| 45 | + } |
| 46 | + for (const leafSpecifier of importsOf(parts).filter(specifier => specifier.startsWith('.'))) { |
| 47 | + const leafFile = resolve(dirname(partsFile), leafSpecifier); |
| 48 | + const leaf = await readFile(leafFile, 'utf8'); |
| 49 | + if (importsReact(leaf) && !isClientModule(leaf)) { |
| 50 | + throw new Error( |
| 51 | + `${relative(distDir, leafFile)}, re-exported by the namespace ${relative(distDir, partsFile)}, imports React but is missing a top-level 'use client' boundary.`, |
| 52 | + ); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | +}; |
| 57 | + |
| 58 | +for (const entryFile of entryFiles) { |
| 59 | + const source = await readFile(entryFile, 'utf8'); |
| 60 | + if (!isClientModule(source)) { |
| 61 | + await checkNamespaceEntry(entryFile, source); |
15 | 62 | } |
16 | 63 | } |
17 | 64 |
|
|
0 commit comments