Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions scripts/exports/build-exports-list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import {readdirSync, readFileSync, writeFileSync} from 'node:fs';
import pathlib from 'node:path';
import {fileURLToPath} from 'node:url';
import {COMPONENT_CUSTOM_ELEMENTS} from '../component-custom-elements.js';

const __filename = fileURLToPath(import.meta.url);
const __dirname = pathlib.dirname(__filename);

type ExportEntry = {import: string; types: string};

let exports: {[path: string]: ExportEntry} = {
'./all.js': {
import: './all.js',
types: './all.d.ts',
},
};

Object.keys(COMPONENT_CUSTOM_ELEMENTS).forEach((component) => {
const paths: string[] = [];
paths.push(
...COMPONENT_CUSTOM_ELEMENTS[
component as keyof typeof COMPONENT_CUSTOM_ELEMENTS
],
);

// add internals to the list of paths.
const componentName = component.toLocaleLowerCase();
const internalDir = pathlib.resolve(
`${__dirname}/../../${componentName}/internal/`,
);
try {
const internals = readdirSync(internalDir, {withFileTypes: true})
.filter(
(f) =>
f.isFile() &&
f.name.endsWith('.ts') &&
!f.name.endsWith('.d.ts') &&
!f.name.includes('_test') &&
!f.name.includes('-styles'),
)
.map((f) => `${componentName}/internal/${f.name}`);

paths.push(...internals);
} catch {
// no internal directory → ignore
}

paths.forEach((filepath) => {
const path = filepath.replace(/\.ts$/, '');
exports[`./${path}.js`] = {
import: `./${path}.js`,
types: `./${path}.d.ts`,
};
});
});

const packageLocation = pathlib.resolve(
`${__dirname}/../../package.json`,
);

const packageJson = JSON.parse(
readFileSync(packageLocation).toString(),
);

packageJson.exports = exports;

writeFileSync(
packageLocation,
JSON.stringify(packageJson, null, 2) + '\n',
);