-
Notifications
You must be signed in to change notification settings - Fork 343
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
204 additions
and
188 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 |
---|---|---|
@@ -1 +1,2 @@ | ||
.nvmrc | ||
.nvmrc | ||
dist/**/_internal/*.d.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// @ts-check | ||
|
||
import tsPlugin from '@rollup/plugin-typescript'; | ||
import { globSync } from 'glob'; | ||
import path from 'node:path'; | ||
|
||
/** | ||
* @param {'es' | 'cjs'} format Rollup output format | ||
* @returns {import('rollup').RollupOptions} Rollup config for a ESM or CJS build | ||
*/ | ||
function buildConfig(format) { | ||
const isESM = format === 'es'; | ||
const jsExtension = format === 'es' ? 'mjs' : 'js'; | ||
|
||
return { | ||
// For ESM, we transpile all the original modules like tsc would do. | ||
// For CJS, only treat the package.json#exports paths as entrypoints, | ||
// resulting in one bundle per category (array, object, etc.), | ||
// plus one index requiring those bundles. | ||
input: getInputs( | ||
isESM | ||
? ['src/**/*.ts'] | ||
: ['src/index.ts', 'src/{array,compat,function,math,object,predicate,promise,string}/index.ts'] | ||
), | ||
plugins: [ | ||
tsPlugin({ | ||
sourceMap: true, | ||
inlineSources: true, | ||
declaration: isESM, | ||
declarationDir: isESM ? 'dist' : undefined, | ||
}), | ||
], | ||
output: { | ||
format, | ||
dir: 'dist', | ||
chunkFileNames: `_chunk/[name]-[hash:6].${jsExtension}`, | ||
entryFileNames: `[name].${jsExtension}`, | ||
// Using preserveModules virtually disables extracting common chunks, | ||
// leading to a result that is a mirror of the TypeScript source. | ||
preserveModules: isESM, | ||
manualChunks: isESM | ||
? undefined | ||
: id => { | ||
if (id.includes('/_internal/')) { | ||
const fragments = id.split('/'); | ||
const parent = fragments.at(fragments.indexOf('_internal') - 1); | ||
return parent ? `${parent}-internal` : 'internal'; | ||
} | ||
}, | ||
sourcemap: true, | ||
generatedCode: 'es2015', | ||
// Hoisting transitive imports adds bare imports in modules, | ||
// which can make imports by JS runtimes slightly faster, | ||
// but makes the generated code harder to follow. | ||
hoistTransitiveImports: false, | ||
}, | ||
}; | ||
} | ||
|
||
/** | ||
* @type {(pattern: Parameters<typeof globSync>[0]) => Record<string, string>} | ||
*/ | ||
function getInputs(pattern = '') { | ||
const files = globSync(pattern, { | ||
ignore: ['**/_internal/*', '**/*.types.ts', '**/*.{bench,spec,test}.ts'], | ||
}); | ||
|
||
return Object.fromEntries( | ||
files.map(file => [ | ||
// chunk name is path without src/ or extension | ||
file.replace(/^src\//, '').replace(/\.[cm]?(js|ts)$/, ''), | ||
path.join(import.meta.dirname, file), | ||
]) | ||
); | ||
} | ||
|
||
export default [buildConfig('es'), buildConfig('cjs')]; |
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.