From 80acb7e6e575657ca3cbedcf47e726fd1ba8ebfa Mon Sep 17 00:00:00 2001 From: Vladimir Kutepov Date: Mon, 25 May 2020 20:20:51 +0300 Subject: [PATCH] Hybrid npm package (CommonJS and ESM) (#20) --- CHANGELOG.md | 5 +++++ package.json | 27 +++++++++++++------------ tools/build.js | 53 ++++++++++++++------------------------------------ 3 files changed, 33 insertions(+), 52 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6392b89..6d18d0f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## [Unreleased](unreleased) + +- Hybrid npm package with both CommonJS and ESM versions + ([#20](https://github.com/kriasoft/hyperapp-render/pull/20)). + ## [3.3.0] - 2020-05-20 - Add `unpkg`, `jsdelivr` and `exports` fields to package.json diff --git a/package.json b/package.json index 0cff6f2..0253a6a 100644 --- a/package.json +++ b/package.json @@ -15,27 +15,26 @@ "html", "ssr" ], - "main": "node/index.js", - "module": "node/module.js", + "type": "commonjs", + "main": "commonjs/node.js", + "module": "esm/node.js", "types": "src/node.d.ts", "esnext": "src/node.js", "unpkg": "hyperapp-render.min.js", "jsdelivr": "hyperapp-render.min.js", "browser": { - "node/index.js": "./browser/index.js", - "node/module.js": "./browser/module.js", + "commonjs/node.js": "./commonjs/browser.js", + "esm/node.js": "./esm/browser.js", "src/node.d.ts": "./src/browser.d.ts", "src/node.js": "./src/browser.js" }, "exports": { "node": { - "import": "./node/module.js", - "require": "./node/index.js" + "require": "./commonjs/node.js", + "default": "./esm/node.js" }, - "default": { - "import": "./browser/module.js", - "require": "./browser/index.js" - } + "require": "./commonjs/browser.js", + "default": "./esm/browser.js" }, "dependencies": { "@types/node": "*" @@ -66,10 +65,10 @@ "typescript": "^3.3.3333" }, "scripts": { - "lint": "node tools/lint", - "test": "node tools/test", - "build": "node tools/build", + "lint": "node tools/lint.js", + "test": "node tools/test.js", + "build": "node tools/build.js", "benchmark": "benchr benchmark/benchmark.js --require @babel/register", - "pre-commit": "node tools/pre-commit" + "pre-commit": "node tools/pre-commit.js" } } diff --git a/tools/build.js b/tools/build.js index e7db7bb..1749424 100644 --- a/tools/build.js +++ b/tools/build.js @@ -20,22 +20,22 @@ const files = [ }, { input: 'dist/src/browser.js', - output: 'dist/browser/index.js', + output: 'dist/commonjs/browser.js', format: 'cjs', }, - { - input: 'dist/src/browser.js', - output: 'dist/browser/module.js', - format: 'es', - }, { input: 'dist/src/node.js', - output: 'dist/node/index.js', + output: 'dist/commonjs/node.js', format: 'cjs', }, + { + input: 'dist/src/browser.js', + output: 'dist/esm/browser.js', + format: 'es', + }, { input: 'dist/src/node.js', - output: 'dist/node/module.js', + output: 'dist/esm/node.js', format: 'es', }, ] @@ -88,41 +88,18 @@ async function build() { ) // Create package.json for npm publishing - const libPkg = Object.assign({}, pkg) - delete libPkg.private - delete libPkg.devDependencies - delete libPkg.scripts - await fs.outputJson('dist/package.json', libPkg, { spaces: 2 }) - - // Create browser/package.json for convenient import - await fs.outputJson( - 'dist/browser/package.json', - { - private: true, - name: 'browser', - version: pkg.version, - main: 'index.js', - module: 'module.js', - types: '../src/browser.d.ts', - esnext: '../src/browser.js', - }, - { spaces: 2 }, - ) - - // Create node/package.json for convenient import await fs.outputJson( - 'dist/node/package.json', + 'dist/package.json', { - private: true, - name: 'node', - version: pkg.version, - main: 'index.js', - module: 'module.js', - types: '../src/node.d.ts', - esnext: '../src/node.js', + ...pkg, + private: undefined, + devDependencies: undefined, + scripts: undefined, }, { spaces: 2 }, ) + await fs.outputJson('dist/commonjs/package.json', { type: 'commonjs' }, { spaces: 2 }) + await fs.outputJson('dist/esm/package.json', { type: 'module' }, { spaces: 2 }) } module.exports = build()