Skip to content

Commit

Permalink
Hybrid npm package (CommonJS and ESM) (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
frenzzy authored May 25, 2020
1 parent 52c6b14 commit 80acb7e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 52 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 13 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "*"
Expand Down Expand Up @@ -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"
}
}
53 changes: 15 additions & 38 deletions tools/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
]
Expand Down Expand Up @@ -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()

0 comments on commit 80acb7e

Please sign in to comment.