Skip to content

Commit c86b331

Browse files
committed
Rework CJS output, add "module" to package.json
The build now produces `dist/esm` and `dist/cjs`, rather than `dist` and `cjs`, and it specifies separate "module" and "main" fields in `package.json` so that the correct type is chosen automatically.
1 parent 26eebe5 commit c86b331

7 files changed

Lines changed: 21 additions & 40 deletions

File tree

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@ node_modules
66
yarn-debug.log
77
yarn-error.log
88

9-
cjs
109
dist

README.md

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ Status](https://travis-ci.org/dphilipson/transducist.svg?branch=master)](https:/
1818
- [Using custom reductions](#using-custom-reductions)
1919
- [Creating a standalone transducer](#creating-a-standalone-transducer)
2020
- [Bundle Size and Tree Shaking](#bundle-size-and-tree-shaking)
21-
- [ES Modules and Node](#es-modules-and-node)
2221
- [Benchmarks](#benchmarks)
2322
- [API](#api)
2423

@@ -98,9 +97,6 @@ With NPM:
9897
npm install transducist
9998
```
10099

101-
If running in a Node environment, make sure to check the note [ES Modules and
102-
Node](#es-modules-and-node) below.
103-
104100
This library, with the exception of the functions which relate to `Set` and
105101
`Map`, works fine on ES5 without any polyfills or transpilation, but its
106102
TypeScript definitions depend on ES6 definitions for the `Iterable` type. If you
@@ -267,20 +263,6 @@ For details, [see the tree shaking
267263
API](https://github.com/dphilipson/transducist/blob/master/docs/api.md#tree-shakeable-api)
268264
section of the API docs.
269265

270-
## ES Modules and Node
271-
272-
Transducist is distributed with ES module syntax (i.e. `import`/`export`,
273-
instead of `require()`/`module.exports`) because doing so is required to take
274-
advantage of tree shaking. If you're running in a Node environment or your
275-
bundler is so old that it doesn't understand this syntax, you may encounter
276-
runtime errors complaining about unexpected tokens in `import` statements. To
277-
resolve this, use the CommonJS version of this library instead by importing from
278-
`transducist/cjs`:
279-
280-
```ts
281-
const { chainFrom } = require("transducist/cjs");
282-
```
283-
284266
## Benchmarks
285267

286268
[View the

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
"name": "transducist",
33
"version": "1.0.2",
44
"description": "Ergonomic JavaScript/TypeScript transducers for beginners and experts.",
5-
"main": "dist/index.js",
6-
"types": "dist/index",
5+
"module": "dist/esm/index.js",
6+
"main": "dist/cjs/index.js",
7+
"types": "dist/esm/index.d.ts",
78
"files": [
8-
"cjs/",
99
"dist/"
1010
],
1111
"sideEffects": false,
@@ -29,8 +29,8 @@
2929
"author": "David Philipson <david.philipson@gmail.com> (http://dphil.me)",
3030
"license": "MIT",
3131
"scripts": {
32-
"build": "yarn run clean && tsc -p tsconfig.build-es.json && tsc -p tsconfig.build-cjs.json && ./scripts/mark-classes-pure.sh",
33-
"clean": "rm -rf cjs/* dist/*",
32+
"build": "yarn run clean && tsc -p tsconfig.build-esm.json && tsc -p tsconfig.build-cjs.json && ./scripts/mark-classes-pure.sh",
33+
"clean": "rm -rf dist/*",
3434
"format-file": "prettier --write",
3535
"format": "git ls-files | egrep '\\.(js(on)?|md|scss|tsx?)?$' | xargs yarn run format-file",
3636
"generate-toc": "git ls-files | egrep '\\.md$' | xargs scripts/markdown-toc-all.sh",

scripts/mark-classes-pure.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#!/bin/sh
22

33
# This replaces "/** @class */" with "/**@__PURE__*/" in all .js files in the
4-
# dist and cjs directories. This is useful because when TypeScript compiles
5-
# classes to ES5, it marks them with /** @class */, but UglifyJS looks for
6-
# /**@__PURE__*/ when performing dead code removal, such as during tree shaking.
4+
# dist directory. This is useful because when TypeScript compiles classes to
5+
# ES5, it marks them with /** @class */, but UglifyJS looks for /**@__PURE__*/
6+
# when performing dead code removal, such as during tree shaking.
77
#
88
# Intentionally removes an extra space to keep the same length, to maintain
99
# accuracy of sourcemaps.
1010

11-
find {cjs,dist} | grep '\.js$' | xargs perl -p -i -e 's~/\*\* \@class \*/ ~/\*\*\@__PURE__\*/~g'
11+
find dist | grep '\.js$' | xargs perl -p -i -e 's~/\*\* \@class \*/ ~/\*\*\@__PURE__\*/~g'

tsconfig.build-cjs.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
{
22
"compilerOptions": {
3-
"declaration": true,
4-
"inlineSources": true,
53
"noEmit": false,
6-
"outDir": "cjs",
7-
"sourceMap": true
4+
"outDir": "dist/cjs"
85
},
96
"exclude": ["__tests__/**/*"],
107
"extends": "./tsconfig.json"

tsconfig.build-es.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

tsconfig.build-esm.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"compilerOptions": {
3+
"declaration": true,
4+
"inlineSources": true,
5+
"noEmit": false,
6+
"outDir": "dist/esm",
7+
"sourceMap": true
8+
},
9+
"exclude": ["__tests__/**/*"],
10+
"extends": "./tsconfig.json"
11+
}

0 commit comments

Comments
 (0)