Skip to content

Commit

Permalink
add rollup config to generate dist
Browse files Browse the repository at this point in the history
  • Loading branch information
fvsch committed Jul 19, 2024
1 parent 55555f7 commit 62f7a24
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 188 deletions.
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.nvmrc
.nvmrc
dist/**/_internal/*.d.ts
22 changes: 13 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"exports": {
".": {
"import": {
"types": "./dist/index.d.mts",
"types": "./dist/index.d.ts",
"default": "./dist/index.mjs"
},
"require": {
Expand All @@ -48,7 +48,7 @@
},
"./array": {
"import": {
"types": "./dist/array/index.d.mts",
"types": "./dist/array/index.d.ts",
"default": "./dist/array/index.mjs"
},
"require": {
Expand All @@ -58,7 +58,7 @@
},
"./function": {
"import": {
"types": "./dist/function/index.d.mts",
"types": "./dist/function/index.d.ts",
"default": "./dist/function/index.mjs"
},
"require": {
Expand All @@ -68,7 +68,7 @@
},
"./math": {
"import": {
"types": "./dist/math/index.d.mts",
"types": "./dist/math/index.d.ts",
"default": "./dist/math/index.mjs"
},
"require": {
Expand All @@ -78,7 +78,7 @@
},
"./object": {
"import": {
"types": "./dist/object/index.d.mts",
"types": "./dist/object/index.d.ts",
"default": "./dist/object/index.mjs"
},
"require": {
Expand All @@ -88,7 +88,7 @@
},
"./predicate": {
"import": {
"types": "./dist/predicate/index.d.mts",
"types": "./dist/predicate/index.d.ts",
"default": "./dist/predicate/index.mjs"
},
"require": {
Expand All @@ -98,7 +98,7 @@
},
"./promise": {
"import": {
"types": "./dist/promise/index.d.mts",
"types": "./dist/promise/index.d.ts",
"default": "./dist/promise/index.mjs"
},
"require": {
Expand All @@ -108,7 +108,7 @@
},
"./string": {
"import": {
"types": "./dist/string/index.d.mts",
"types": "./dist/string/index.d.ts",
"default": "./dist/string/index.mjs"
},
"require": {
Expand All @@ -118,7 +118,7 @@
},
"./compat": {
"import": {
"types": "./dist/compat/index.d.mts",
"types": "./dist/compat/index.d.ts",
"default": "./dist/compat/index.mjs"
},
"require": {
Expand All @@ -135,6 +135,7 @@
"@babel/preset-typescript": "^7.24.1",
"@changesets/changelog-github": "^0.5.0",
"@changesets/cli": "^2.27.1",
"@rollup/plugin-typescript": "^11.1.6",
"@types/babel__core": "^7",
"@types/babel__preset-env": "^7",
"@types/broken-link-checker": "^0",
Expand All @@ -146,7 +147,10 @@
"eslint": "^8.56.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-jsdoc": "^48.5.0",
"glob": "^11.0.0",
"prettier": "^3.2.5",
"rollup": "^4.18.1",
"tslib": "^2.6.3",
"tsup": "^8.2.0",
"typescript": "^5.4.5",
"vitest": "^1.5.2"
Expand Down
77 changes: 77 additions & 0 deletions rollup.config.mjs
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')];
5 changes: 3 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"lib": ["ESNext"],
"target": "es2016",
"target": "ES2018",
"module": "ESNext",
"noEmit": true,
"allowImportingTsExtensions": true,
Expand All @@ -11,5 +11,6 @@
"strict": true,
"skipLibCheck": true
},
"include": ["src"]
"include": ["src/**/*.ts"],
"exclude": ["**/*.spec.ts"]
}
Loading

0 comments on commit 62f7a24

Please sign in to comment.