Skip to content

Commit 277af6e

Browse files
committed
Also distribute CommonJS version of library
1 parent d5a283f commit 277af6e

7 files changed

Lines changed: 31 additions & 4 deletions

File tree

.gitignore

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

9+
cjs
910
dist

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ 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)
2122
- [Benchmarks](#benchmarks)
2223
- [API](#api)
2324

@@ -106,6 +107,9 @@ With NPM:
106107
npm install --save transducist
107108
```
108109

110+
If running in a Node environemnt, make sure to check the note [ES Modules and
111+
Node](#es-modules-and-node) below.
112+
109113
This library, with the exception of the functions which relate to `Set` and
110114
`Map`, works fine on ES5 without any polyfills or transpilation, but its
111115
TypeScript definitions depend on ES6 definitions for the `Iterable` type. If you
@@ -272,6 +276,19 @@ For details, [see the tree shaking
272276
API](https://github.com/dphilipson/transducist/blob/master/docs/api.md#tree-shakeable-api)
273277
section of the API docs.
274278

279+
## ES Modules and Node
280+
281+
Transducist is distributed with ES module syntax (i.e. `import`/`export`, not
282+
`require()`) because doing so is required to take advantage of tree shaking. If
283+
you're running in a Node environment or your bundler is so old that it doesn't
284+
understand this syntax, you may encounter runtime errors complaining about
285+
unexpected tokens in `import` statements. To resolve this, use a CommonJS
286+
version of this library by importing from `transducist/cjs` instead:
287+
288+
```ts
289+
const { chainFrom } = require("transducist/cjs");
290+
```
291+
275292
## Benchmarks
276293

277294
[View the

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"main": "dist/index.js",
66
"types": "dist/index",
77
"files": [
8+
"cjs/",
89
"dist/"
910
],
1011
"sideEffects": false,
@@ -28,7 +29,7 @@
2829
"author": "David Philipson <david.philipson@gmail.com> (http://dphil.me)",
2930
"license": "MIT",
3031
"scripts": {
31-
"build": "yarn run clean && tsc -p tsconfig.build.json && ./scripts/mark-classes-pure.sh",
32+
"build": "yarn run clean && tsc -p tsconfig.build-es.json && tsc -p tsconfig.build-cjs.json && ./scripts/mark-classes-pure.sh",
3233
"clean": "rm -rf dist/*",
3334
"format-file": "prettier --write",
3435
"format": "git ls-files | egrep '\\.(js(on)?|md|scss|tsx?)?$' | xargs yarn run format-file",

scripts/mark-classes-pure.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
# Intentionally removes an extra space to keep the same length, to maintain
99
# accuracy of sourcemaps.
1010

11-
find dist | grep \.js$ | xargs perl -p -i -e 's~/\*\* \@class \*/ ~/\*\*\@__PURE__\*/~g'
11+
find {src,dist} | grep \.js$ | xargs perl -p -i -e 's~/\*\* \@class \*/ ~/\*\*\@__PURE__\*/~g'
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"declaration": true,
44
"inlineSources": true,
55
"noEmit": false,
6-
"outDir": "dist",
6+
"outDir": "cjs",
77
"sourceMap": true
88
},
99
"exclude": ["__tests__/**/*"],

tsconfig.build-es.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"compilerOptions": {
3+
"module": "es2015",
4+
"outDir": "dist"
5+
},
6+
"exclude": ["__tests__/**/*"],
7+
"extends": "./tsconfig.build-cjs.json"
8+
}

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"compilerOptions": {
33
"lib": ["es5", "es2015.iterable", "es2015.collection"],
4-
"module": "es2015",
4+
"module": "commonjs",
55
"moduleResolution": "node",
66
"noEmit": true,
77
"noFallthroughCasesInSwitch": true,

0 commit comments

Comments
 (0)