Skip to content

Commit

Permalink
individual dts bundles
Browse files Browse the repository at this point in the history
  • Loading branch information
arshaw committed Nov 20, 2022
1 parent 5073cf6 commit 2742c5d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 48 deletions.
2 changes: 1 addition & 1 deletion scripts/src/pkg/bundle.ts
Expand Up @@ -108,7 +108,7 @@ async function buildRollupOptionObjs(
return [
...buildModuleOptions(pkgBundleStruct, esm, cjs, moduleSourcemap),
...(iife ? await buildIifeOptions(pkgBundleStruct, monorepoStruct, iifeMinify, iifeSourcemap) : []),
...(dts ? [buildDtsOptions(pkgBundleStruct)] : []),
...(dts ? buildDtsOptions(pkgBundleStruct) : []),
]
}

Expand Down
27 changes: 0 additions & 27 deletions scripts/src/pkg/utils/rollup-plugins.ts
Expand Up @@ -161,33 +161,6 @@ async function minifySeparately(path: string): Promise<void> {
})
}

// .d.ts
// -------------------------------------------------------------------------------------------------

/*
Workarounds rollup-plugin-dts
*/
export function massageDtsPlugin(): Plugin {
return {
name: 'massage-dts',
renderChunk(code) {
// force all import statements (especially auto-generated chunks) to have a .js extension
// TODO: file a bug. code splitting w/ es2016 modules
code = code.replace(/(} from ['"])([^'"]*)(['"])/g, (whole, start, importId, end) => {
if (
importId.startsWith('./') && // relative ID
!importId.endsWith('.js')
) {
return start + importId + '.js' + end
}
return whole
})

return code
},
}
}

// Extensions Find & Replace Utils
// -------------------------------------------------------------------------------------------------

Expand Down
67 changes: 47 additions & 20 deletions scripts/src/pkg/utils/rollup-presets.ts
Expand Up @@ -33,7 +33,6 @@ import {
externalizePkgsPlugin,
generatedContentPlugin,
minifySeparatelyPlugin,
massageDtsPlugin,
rerootPlugin,
} from './rollup-plugins.js'

Expand Down Expand Up @@ -65,15 +64,6 @@ export function buildModuleOptions(
return []
}

export function buildDtsOptions(pkgBundleStruct: PkgBundleStruct): RollupOptions {
return {
input: buildDtsInput(pkgBundleStruct),
plugins: buildDtsPlugins(pkgBundleStruct),
output: buildDtsOutputOptions(pkgBundleStruct),
onwarn,
}
}

export async function buildIifeOptions(
pkgBundleStruct: PkgBundleStruct,
monorepoStruct: MonorepoStruct,
Expand Down Expand Up @@ -102,6 +92,38 @@ export async function buildIifeOptions(
return optionsObjs
}

/*
Generate separate .d.ts bundles for each endpoints.
Better than code-splitting because:
- rollup-plugin-dts chokes easily
- when a user package imports a type that lives within an internal-only chunk,
complains about "reference not portable"
Even without code-splitting, there will be code boundaries between endpoints
thanks to externalizePathsPlugin & computeOwnExternalPaths.
However, there will still be repeat types between bundles.
TODO: federate source code better to mitigate this.
*/
export function buildDtsOptions(pkgBundleStruct: PkgBundleStruct): RollupOptions[] {
const { entryStructMap } = pkgBundleStruct
const optionsObjs: RollupOptions[] = []

for (let entryAlias in entryStructMap) {
const entryStruct = entryStructMap[entryAlias]

optionsObjs.push({
input: buildDtsInput(entryStruct),
plugins: buildDtsPlugins(pkgBundleStruct),
output: buildDtsOutputOptions(entryAlias, pkgBundleStruct),
onwarn,
})
}

return optionsObjs
}

// Input
// -------------------------------------------------------------------------------------------------

Expand All @@ -117,10 +139,8 @@ function buildIifeInput(entryStruct: EntryStruct): string {
return entryStruct.entrySrcBase + '.iife' + transpiledExtension
}

function buildDtsInput(pkgBundleStruct: PkgBundleStruct): InputMap {
return mapProps(pkgBundleStruct.entryStructMap, (entryStruct: EntryStruct) => {
return entryStruct.entrySrcBase + '.d.ts'
})
function buildDtsInput(entryStruct: EntryStruct): string {
return entryStruct.entrySrcBase + '.d.ts'
}

// Output
Expand Down Expand Up @@ -178,12 +198,15 @@ function buildIifeOutputOptions(
}
}

function buildDtsOutputOptions(pkgBundleStruct: PkgBundleStruct): OutputOptions {
function buildDtsOutputOptions(
entryAlias: string,
pkgBundleStruct: PkgBundleStruct,
): OutputOptions {
const { pkgDir } = pkgBundleStruct

return {
format: 'esm',
dir: joinPaths(pkgBundleStruct.pkgDir, 'dist'),
entryFileNames: '[name].d.ts',
chunkFileNames: 'internal-[hash].d.ts',
file: joinPaths(pkgDir, 'dist', entryAlias) + '.d.ts',
}
}

Expand All @@ -206,6 +229,9 @@ function buildModulePlugins(pkgBundleStruct: PkgBundleStruct, sourcemap: boolean
]
}

/*
TODO: inefficient to repeatedly generate all this?
*/
function buildIifePlugins(
currentEntryStruct: EntryStruct,
pkgBundleStruct: PkgBundleStruct,
Expand Down Expand Up @@ -233,18 +259,19 @@ function buildIifePlugins(
]
}

/*
TODO: inefficient to repeatedly generate all this?
*/
function buildDtsPlugins(pkgBundleStruct: PkgBundleStruct): Plugin[] {
return [
externalizeAssetsPlugin(),
externalizePkgsPlugin(
computeExternalPkgs(pkgBundleStruct),
),
// rollup-plugin-dts normally gets confused with code splitting. this helps a lot.
externalizePathsPlugin({
paths: computeOwnExternalPaths(pkgBundleStruct),
}),
dtsPlugin(),
massageDtsPlugin(),
nodeResolvePlugin(),
]
}
Expand Down

0 comments on commit 2742c5d

Please sign in to comment.