Skip to content

Commit

Permalink
packaging: distribute @cockroachlabs/icons with ESM and CommonJS vers…
Browse files Browse the repository at this point in the history
…ions

Distributing an ES Modules-formatted package makes tree-shaking of
unused exports significantly simpler for packages that consume /icons.
Node's support for ESM is still experimental though, and Jest by default
won't transform anything in node_modules. Using that ESM build wouldn't
work, and would require the previous CommonJS format. This left us with
a dilemma:

Either distribute something that can be bundled efficiently and
effectively (ESM), or ship something that's easy to use in tests (CJS).

Luckily that's actually a false dilemma: we can just ship both. Bundlers
like webpack will read the 'module' field in package.json and receive
the easily-bundled ES Modules format. Node (and by extension, Jest) will
read the 'main' field in package.json and receive the no-transformation-
necessary CommonJS format. Everyone wins!
  • Loading branch information
sjbarag committed Sep 20, 2023
1 parent 239711d commit 0cff2a2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
9 changes: 6 additions & 3 deletions packages/icons/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
"files": [
"dist/"
],
"main": "dist/index.js",
"types": "dist/index.d.ts",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"types": "dist/cjs/index.d.ts",
"scripts": {
"build": "run-s build:generate:stripfill build:generate:preserve build:typescript",
"build:typescript": "tsc",
"build:typescript": "run-p build:typescript:*",
"build:typescript:cjs": "tsc -p tsconfig.json",
"build:typescript:esm": "tsc -p tsconfig.esm.json",
"build:generate:stripfill": "svgr --typescript --icon -d src/components svg/customFill",
"build:generate:preserve": "svgr --typescript --icon --no-runtime-config -d src/components svg/preserveFill",
"build:watch": "tsc --watch --preserveWatchOutput",
Expand Down
7 changes: 7 additions & 0 deletions packages/icons/tsconfig.esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "dist/esm/",
"module": "es2020"
}
}
4 changes: 2 additions & 2 deletions packages/icons/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
"noImplicitAny": true,
"module": "commonjs",
"moduleResolution": "Node",
"target": "es6",
"target": "es2020",
"jsx": "react",
"esModuleInterop": true,
"declaration": true,
"outDir": "./dist",
"outDir": "./dist/cjs/",
"skipLibCheck": true,
},
"exclude": [
Expand Down

0 comments on commit 0cff2a2

Please sign in to comment.