-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* normalize entry source files * infer source and output options for loader * fix tests * add changeset * filter internal components
- Loading branch information
1 parent
c57b51f
commit f05b552
Showing
16 changed files
with
201 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
--- | ||
'mdxts': minor | ||
--- | ||
|
||
Normalizes the internal `getEntrySourceFiles` utility that is responsible for determining what TypeScript data sources are public based on `package.json` exports, index files, and top-level directories. | ||
|
||
To determine what source files should be considered public when dealing with package exports, `createSource` gets two new options used to remap `package.json` exports to their original source files: | ||
|
||
```ts | ||
import { createSource } from 'mdxts' | ||
|
||
const allPackages = createSource('../packages/mdxts/src/**/*.{ts,tsx}', { | ||
sourceDirectory: 'src', | ||
outputDirectory: 'dist', | ||
}) | ||
``` | ||
|
||
Using a subset of the `mdxts` `package.json` exports as an example: | ||
|
||
```json | ||
"exports": { | ||
".": { | ||
"types": "./dist/src/index.d.ts", | ||
"import": "./dist/src/index.js", | ||
"require": "./dist/cjs/index.js" | ||
}, | ||
"./components": { | ||
"types": "./dist/src/components/index.d.ts", | ||
"import": "./dist/src/components/index.js", | ||
"require": "./dist/cjs/components/index.js" | ||
}, | ||
}, | ||
``` | ||
|
||
These would be remapped to their original source files, filtering out any paths gathered from the `createSource` pattern not explicitly exported: | ||
|
||
```json | ||
["../packages/mdxts/src/index.ts", "../packages/mdxts/src/components/index.ts"] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { join, resolve } from 'path' | ||
import { Project, SourceFile } from 'ts-morph' | ||
|
||
import { getPackageMetadata } from './get-package-metadata' | ||
import { getSharedDirectoryPath } from './get-shared-directory-path' | ||
|
||
const extensionPatterns = [ | ||
'.{js,jsx,ts,tsx}', | ||
'.{examples,test}.{js,jsx,ts,tsx}', | ||
] | ||
|
||
/** | ||
* Filters paths and returns TypeScript source files based on the following entry points: | ||
* - Package.json exports | ||
* - Root index file | ||
* - Top-level directory files | ||
*/ | ||
export function getEntrySourceFiles( | ||
project: Project, | ||
allPaths: string[], | ||
sourceDirectory: string = 'src', | ||
outputDirectory: string | string[] = 'dist' | ||
): SourceFile[] { | ||
if (typeof outputDirectory === 'string') { | ||
outputDirectory = [outputDirectory] | ||
} | ||
|
||
const sharedDirectoryPath = getSharedDirectoryPath(...allPaths) | ||
const packageMetadata = getPackageMetadata(...allPaths) | ||
let entrySourceFiles: SourceFile[] = [] | ||
|
||
// Use package.json exports for calculating public paths if they exist. | ||
if (packageMetadata?.exports) { | ||
for (const exportKey in packageMetadata.exports) { | ||
const exportValue = packageMetadata.exports[exportKey] | ||
let exportPath = exportValue | ||
|
||
if (typeof exportValue === 'object') { | ||
exportPath = exportValue.import | ||
} | ||
|
||
const sourceFilePaths = extensionPatterns | ||
.flatMap((pattern, index) => | ||
(outputDirectory as string[]).map((directory) => { | ||
if (!exportPath.includes(directory)) { | ||
return | ||
} | ||
const exportPattern = exportPath | ||
.replace(directory, sourceDirectory) | ||
.replace(/\.js$/, pattern) | ||
const sourcePathPattern = resolve( | ||
packageMetadata.directory, | ||
exportPattern | ||
) | ||
// Include the first pattern and exclude examples and tests. | ||
return index === 0 ? sourcePathPattern : `!${sourcePathPattern}` | ||
}) | ||
) | ||
.filter(Boolean) as string[] | ||
|
||
entrySourceFiles.push(...project.addSourceFilesAtPaths(sourceFilePaths)) | ||
} | ||
} else { | ||
// Otherwise default to a common root index file. | ||
const defaultSourcePath = join(sharedDirectoryPath, 'index.{js,jsx,ts,tsx}') | ||
|
||
entrySourceFiles.push(...project.addSourceFilesAtPaths(defaultSourcePath)) | ||
|
||
// If no root index files exist, assume the top-level directory files are all public exports. | ||
if (entrySourceFiles.length === 0) { | ||
entrySourceFiles = project.addSourceFilesAtPaths( | ||
extensionPatterns.map((pattern, index) => { | ||
const sourcePathPattern = join(sharedDirectoryPath, `*${pattern}`) | ||
return index === 0 ? sourcePathPattern : `!${sourcePathPattern}` | ||
}) | ||
) | ||
} | ||
} | ||
|
||
return entrySourceFiles | ||
} |
Oops, something went wrong.