Skip to content

Commit

Permalink
change/API! ~ add ESM module packaging (adds 'exports' to package)
Browse files Browse the repository at this point in the history
- adds an ESM wrapper to provide simple ESM support (as suggested in "Node Modules at War"

* ref: [Node Modules at War](https://redfin.engineering/node-modules-at-war-why-commonjs-and-es-modules-cant-get-along-9617135eeca1) @@ <https://archive.is/zl0qg>
* ref: [yargs ~ 'Road to ESM/Deno'](yargs/yargs#1706)
  • Loading branch information
rivy committed Jan 1, 2021
1 parent c34dd25 commit 1e0bda5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
12 changes: 12 additions & 0 deletions package.json
Expand Up @@ -23,6 +23,17 @@
"main": "dist/cjs/index.js",
"module": "dist/cjs/esm-wrapper/index.js",
"types": "dist/types/index.d.ts",
"exports": {
"./package.json": "./package.json",
".": [
{
"import": "./dist/cjs/esm-wrapper/index.js",
"require": "./dist/cjs/index.js",
"types": "./dist/types/index.d.ts"
},
"./dist/cjs/index.js"
]
},
"keywords": [
"common",
"cross-platform",
Expand All @@ -44,6 +55,7 @@
"# build # build/compile package": "",
"build": "run-s _:regen:build",
"build:cjs": "tsc -p tsconfig/tsconfig.cjs.json",
"build:cjs/esm": "shx mkdir -p build/cjs && shx cp -r src/esm-wrapper build/cjs",
"## build:esm * [2020-12-22; rivy] TS compiles to ESMs are broken due to extension mishandling (use `rollup`)": "tsc -p tsconfig/tsconfig.esm.json",
"build:umd": "tsc -p tsconfig/tsconfig.umd.json",
"build:tests": "tsc -p tsconfig/tsconfig.tests.json",
Expand Down
9 changes: 9 additions & 0 deletions src/esm-wrapper/index.js
@@ -0,0 +1,9 @@
/* eslint-env node */
// # spell-checker:ignore Deno

import _ from '../index.js';
// note: not usable by `deno`;
// ...`deno` is unable to load (the CJS module) '../index.js' via import => `'../index.js' does not provide an export named 'default'`

const default_ = _;
export default default_;
1 change: 1 addition & 0 deletions src/esm-wrapper/package.json
@@ -0,0 +1 @@
{"type": "module"}
10 changes: 9 additions & 1 deletion src/index.ts
Expand Up @@ -9,7 +9,15 @@ const haveModuleExports_ = typeof module === 'object' && module.exports;
// ## maint ~ [2020-12-23; rivy] although tested, `nyc` is unable to instrument ESM/.mjs correctly in order show coverage for testing the *else* clause
/* istanbul ignore else */
if (haveModuleExports_) {
// enables direct require from CJS (eg, `const m = require('...');`); skipped for ESM
// enables direct require from CJS (eg, `const module = require('...');`), but generally disables any other exports
// * skipped for ESM (missing `module.exports`)
// * added non-enumerable '_esm!' property (as a hack) to allow full access to all exports (for testing, ...)
// eslint-disable-next-line functional/immutable-data
module.exports = default_;
Object.defineProperty(module.exports, '_esm!', {
get() {
return exports;
},
enumerable: false,
});
}

0 comments on commit 1e0bda5

Please sign in to comment.