-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bundling): generate matching d.ts files for rollup
- Loading branch information
Showing
9 changed files
with
134 additions
and
102 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
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,60 @@ | ||
import type { OutputBundle } from 'rollup'; | ||
import { relative } from 'path'; | ||
import { stripIndents } from '@nx/devkit'; | ||
|
||
//NOTE: This is here so we can share between `@nx/rollup` and `@nx/vite`. | ||
|
||
/* | ||
* This plugin takes all entry-points from the generated bundle and creates a | ||
* bundled version of corresponding d.ts files. | ||
* | ||
* For example, `src/index.ts` generates two corresponding files: | ||
* - `dist/xyz/index.js` | ||
* - `dist/xyz/src/index.d.ts` | ||
* | ||
* We want a third file: `dist/index.d.ts` that re-exports from `src/index.d.ts`. | ||
* That way, when TSC or IDEs look for types, it will find them in the right place. | ||
*/ | ||
export function typeDefinitions(options: { | ||
projectRoot: string; | ||
main: string; | ||
}) { | ||
return { | ||
name: 'dts-bundle', | ||
async generateBundle(_opts: unknown, bundle: OutputBundle): Promise<void> { | ||
for (const [name, file] of Object.entries(bundle)) { | ||
if ( | ||
file.type === 'asset' || | ||
!file.isEntry || | ||
file.facadeModuleId == null | ||
) { | ||
continue; | ||
} | ||
|
||
const hasDefaultExport = file.exports.includes('default'); | ||
const entrySourceFileName = relative( | ||
options.projectRoot, | ||
file.facadeModuleId | ||
); | ||
const entrySourceDtsName = entrySourceFileName.replace( | ||
/\.[cm]?[jt]sx?$/, | ||
'' | ||
); | ||
const dtsFileName = file.fileName.replace(/\.[cm]?js$/, '.d.ts'); | ||
const relativeSourceDtsName = JSON.stringify('./' + entrySourceDtsName); | ||
const dtsFileSource = hasDefaultExport | ||
? stripIndents` | ||
export * from ${relativeSourceDtsName}; | ||
export { default } from ${relativeSourceDtsName}; | ||
` | ||
: `export * from ${relativeSourceDtsName};\n`; | ||
|
||
this.emitFile({ | ||
type: 'asset', | ||
fileName: dtsFileName, | ||
source: dtsFileSource, | ||
}); | ||
} | ||
}, | ||
}; | ||
} |
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
20 changes: 0 additions & 20 deletions
20
packages/rollup/src/executors/rollup/lib/validate-types.ts
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.