Skip to content

Commit

Permalink
Add CJS/ESM module exports (#81)
Browse files Browse the repository at this point in the history
* add: rollup, esbuild and rollup plugins

- rollup
- @rollup/plugin-typescript
- rollup-plugin-dts
- rollup-plugin-esbuild
- esbuild

* update: tsconfig.json

Following changes:

- update lib and target to es2018
- add esModuleInterop and set to true
- add moduleResolution and set to node

* add: rollup config file

The configuration is designe to build:

- declarations
- cjs module
- esm module

* update: package.json exports and scripts

Set the correct entrypoint for cjs and esm modules and update build script to perform a typecheck followed by a rollup build

* update: typescript 4.4.2 -> 4.8.3

Update typescript to latest version due to an issue that version 4.4.2 has with rollup.
For reference here is the issue: [Builds hang with typescript 4.4.2](rollup/plugins#983)

* add: sideEffects to package.json

Add 'sideEffects: false' to package.json to allow webpack (and possible other bundlers) to threeshake the code

* fix: tsconfig target set to 'esnext'

Update tsconfig to target 'esnext' instead of 'es2018'

* fix: set rollup to preserve module structure

Set option 'preserveModules' to 'true' to keep module structure intact instead of combining everything into a single file.
This way it should be easier for bundlers to three shake unused code

* fix: tsconfig target and lib set to 'es2019'

Following the guidelines provided here: https://github.com/microsoft/TypeScript/wiki/Node-Target-Mapping, I set tsconfig target and lib to 'es2019' to support node 12

* fix: set package.json exports and use .cjs and .mjs extensions

Add exports and specific .cjs and .mjs extensions to make sure node uses the correct module loader
Following documentaion on the official nodejs website: https://nodejs.org/api/packages.html#dual-commonjses-module-packages

* build: bump rollup to 2.79.1

* build: regenerate yarn.lock
  • Loading branch information
matteosacchetto authored Oct 14, 2022
1 parent e4a8f57 commit cc22b12
Show file tree
Hide file tree
Showing 4 changed files with 1,310 additions and 933 deletions.
18 changes: 15 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@
"name": "radash",
"version": "7.1.0",
"description": "Functional utility library - modern, simple, typed, powerful",
"main": "dist/index.js",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
"types": "dist/index.d.ts",
"exports": {
"import": "./dist/esm/index.mjs",
"require": "./dist/cjs/index.cjs",
"types": "./dist/index.d.ts"
},
"sideEffects": false,
"files": [
"dist/**/*"
],
Expand All @@ -16,18 +23,23 @@
"scripts": {
"test": "jest --coverage",
"check": "yarn lint && yarn test && yarn build",
"build": "tsc",
"build": "yarn tsc --noEmit && yarn rollup -c",
"lint": "tslint -p tsconfig.json"
},
"devDependencies": {
"@rollup/plugin-typescript": "^8.5.0",
"@types/chai": "^4.2.21",
"@types/jest": "^27.0.1",
"chai": "^4.3.4",
"esbuild": "^0.15.7",
"jest": "^27.1.0",
"prettier": "^2.7.1",
"prettier-plugin-organize-imports": "^3.0.3",
"rollup": "^2.79.1",
"rollup-plugin-dts": "^4.2.2",
"rollup-plugin-esbuild": "^4.10.1",
"ts-jest": "^27.0.5",
"tslint": "^6.0.0",
"typescript": "^4.4.2"
"typescript": "^4.8.3"
}
}
85 changes: 85 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// rollup.config.js
import typescript from '@rollup/plugin-typescript';
import dts from 'rollup-plugin-dts';
import esbuild from 'rollup-plugin-esbuild';

// Import dependencies
import { builtinModules } from 'module';
import { dependencies } from './package.json';

const usePreferConst = true; // Use "const" instead of "var"
const usePreserveModules = true; // `true` -> keep modules structure, `false` -> combine everything into a single file
const useStrict = true; // Use "strict"
const useThrowOnError = true; // On error throw and exception
const useSourceMap = true; // Generate source map files
const useEsbuild = true; // `true` -> use esbuild, `false` use tsc

// Node dependencies are treated as external dependencies and are not bundled
const nodeDependencies = builtinModules
.filter((el) => !el.startsWith('_'))
.flatMap((el) => [el, `node:${el}`]);

export default [
{
// .d.ts build
external: dependencies
? [...Object.keys(dependencies), ...nodeDependencies]
: nodeDependencies,
input: 'src/index.ts',
output: {
file: 'dist/index.d.ts',
format: 'es',
},
plugins: [
dts(),
],
},
{
// CJS build
external: dependencies
? [...Object.keys(dependencies), ...nodeDependencies]
: nodeDependencies,
input: 'src/index.ts',
output: {
dir: 'dist/cjs',
format: 'cjs',
preferConst: usePreferConst,
preserveModules: usePreserveModules,
strict: useStrict,
entryFileNames: '[name].cjs',
sourcemap: useSourceMap,
},
plugins: [
useEsbuild
? esbuild()
: typescript({
noEmitOnError: useThrowOnError,
outDir: 'dist/cjs'
}),
],
},
{
// ESM build
external: dependencies
? [...Object.keys(dependencies), ...nodeDependencies]
: nodeDependencies,
input: 'src/index.ts',
output: {
dir: 'dist/esm',
format: 'es',
preferConst: usePreferConst,
preserveModules: usePreserveModules,
strict: useStrict,
entryFileNames: '[name].mjs',
sourcemap: useSourceMap,
},
plugins: [
useEsbuild
? esbuild()
: typescript({
noEmitOnError: useThrowOnError,
outDir: 'dist/esm'
}),
],
},
];
7 changes: 4 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"compilerOptions": {
"outDir": "dist",
"lib": ["es2016"],
"declaration": true,
"sourceMap": true
"moduleResolution": "node",
"target": "es2019",
"lib": ["es2019"],
"esModuleInterop": true,
},
"include": [
"src/**/*.ts"
Expand Down
Loading

0 comments on commit cc22b12

Please sign in to comment.