Skip to content

Commit

Permalink
fix: expand * files as modules (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
ignatiusmb committed Jan 12, 2024
1 parent 2166c59 commit e3e5808
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
8 changes: 5 additions & 3 deletions workspace/marqua/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@
"types": "./index.d.ts",
"default": "./src/transform/index.js"
},
"./styles/*.css": "./styles/*.css",
"./styles/*.css": {
"types": "./styles/index.d.ts",
"default": "./styles/*.css"
},
"./package.json": "./package.json"
},
"files": [
"artisan/*.d.ts",
"src/**/*.js",
"styles/",
"styles/*.css",
"index.d.ts",
"index.d.ts.map"
],
Expand Down
11 changes: 11 additions & 0 deletions workspace/marqua/scripts/compile-types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as fs from 'node:fs';
import { createBundle } from 'dts-buddy';
import { exports } from '../package.json';

Expand All @@ -8,3 +9,13 @@ await createBundle({
return { ...acc, ['marqua' + key.slice(1)]: exports[key].default };
}, {}),
});

for (const subpath of Object.keys(exports).filter((k) => k.includes('*'))) {
const parent = subpath.slice(0, subpath.lastIndexOf('/'));
const ext = subpath.slice(subpath.lastIndexOf('*') + 1);
const files = fs.readdirSync(parent).filter((f) => f.endsWith(ext));
fs.writeFileSync(
`${parent}/index.d.ts`,
files.map((f) => `declare module 'marqua/${parent.slice(2)}/${f}' {}`).join('\n'),
);
}
4 changes: 2 additions & 2 deletions workspace/marqua/src/fs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ export function traverse(
const tree = fs.readdirSync(entry).map((name) => {
const path = join(entry, name);
return {
/** @type {any} - discriminated union without multiple returns */
type: fs.lstatSync(path).isDirectory() ? 'directory' : 'file',
/** @type {any} - trick TS to enable discriminated union */
type: fs.statSync(path).isDirectory() ? 'directory' : 'file',
breadcrumb: path.split(/[/\\]/).reverse(),
get buffer() {
return this.type === 'file' ? fs.readFileSync(path) : void 0;
Expand Down

1 comment on commit e3e5808

@vercel
Copy link

@vercel vercel bot commented on e3e5808 Jan 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.