Skip to content

Commit

Permalink
feat: conditional exports
Browse files Browse the repository at this point in the history
Co-authored-by: ljharb <ljharb@gmail.com>

BREAKING CHANGE: This package now includes [conditional package exports][docs]. Most consumers should be able to upgrade without any issues, however, package exports limits the allowed entry points inside of Node.js and also affects consumption in other bundlers.

- `main` targets been updated to support down to node 4 (ES5)
- `module` targets has update to support down to node 6 (ES2015)
- `exports` targets have been set to node 12 (ES2019)
- `unpkg` targets have been set to browserslist `defaults`

[docs]: https://nodejs.org/api/packages.html#conditional-exports
  • Loading branch information
ricokahler committed Feb 14, 2022
1 parent 5cb8d66 commit 31b79a1
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 27 deletions.
6 changes: 3 additions & 3 deletions .gitignore
@@ -1,5 +1,5 @@
/dist
/build
/coverage
node_modules
dist
build
coverage
.DS_Store
4 changes: 3 additions & 1 deletion .npmignore
@@ -1,4 +1,5 @@
.github
.vscode
build
coverage
node_modules
Expand All @@ -9,8 +10,9 @@ website
.gitignore
.nvmrc
.prettierrc
.releaserc.json
babel.config.json
jest.config.js
renovate.json
rollup.config.js
tsconfig.json
.releaserc.json
13 changes: 8 additions & 5 deletions README.md
@@ -1,4 +1,6 @@
# [color2k](https://color2k.com) <br /> [![bundlephobia](https://badgen.net/bundlephobia/minzip/color2k)](https://bundlephobia.com/result?p=color2k) [![github status checks](https://badgen.net/github/checks/ricokahler/color2k)](https://github.com/ricokahler/color2k/actions) [![codecov](https://codecov.io/gh/ricokahler/color2k/branch/master/graph/badge.svg)](https://codecov.io/gh/ricokahler/color2k) ![weekly downloads](https://badgen.net/npm/dw/color2k) [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
# [color2k](https://color2k.com)

[![bundlephobia](https://badgen.net/bundlephobia/minzip/color2k)](https://bundlephobia.com/result?p=color2k) [![github status checks](https://badgen.net/github/checks/ricokahler/color2k)](https://github.com/ricokahler/color2k/actions) [![codecov](https://codecov.io/gh/ricokahler/color2k/branch/master/graph/badge.svg)](https://codecov.io/gh/ricokahler/color2k) ![weekly downloads](https://badgen.net/npm/dw/color2k) [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)

> a color parsing and manipulation lib served in roughly 2kB or less (2.8kB to be more precise)
Expand All @@ -18,17 +20,18 @@ The full bundle size is [currently 2.8kB](https://bundlephobia.com/result?p=colo

👋 Compare tree-shaken bundle outputs on [bundle.js.org](https://bundle.js.org/?share=PTAEFcDsGMHsFt4FNIBdQENIBNRIB4AOsATuvLNuADZIDOoqsoc8hGJSjAFkgJYlQdPgC96oAFASQeIqXQAqUADMSCUACJo3NfAwBaAFZ0NAbmlgCxMqCWr1G4tT51e2MxdnXFK3ZrjUpB4yVvK2vg6ofJAAngGkAEweoTYA3tgcANYoAL4R8P6wgSQJmR5AA)


## Installation

Install with a package manager such as `npm`

```
npm i color2k
```

or
**OR** use with [skypack.dev](https://www.skypack.dev/) in supported environments (e.g. [deno](https://deno.land/manual/linking_to_external_code), [modern browsers](https://docs.skypack.dev/#whats-old-is-new-again)).

```
yarn add color2k
```js
import { darken, transparentize } from 'https://cdn.skypack.dev/color2k?min';
```

## Usage
Expand Down
8 changes: 3 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 16 additions & 8 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "color2k",
"version": "1.1.1",
"version": "2.0.0",
"description": "a color parsing and manipulation lib served in roughly 2kB",
"repository": {
"type": "git",
Expand All @@ -22,14 +22,23 @@
"chroma-js"
],
"sideEffects": false,
"main": "./dist/index.js",
"unpkg": "./dist/index.umd.js",
"module": "./dist/index.module.js",
"source": "./src/index.ts",
"main": "./dist/index.main.cjs.js",
"unpkg": "./dist/index.unpkg.umd.js",
"jsdelivr": "./dist/index.unpkg.umd.js",
"module": "./dist/index.module.es.js",
"types": "./dist/index.d.ts",
"exports": {
".": [
{
"import": "./dist/index.exports.import.es.mjs",
"default": "./dist/index.exports.require.cjs.js"
},
"./dist/index.exports.require.cjs.js"
],
"./package.json": "./package.json"
},
"scripts": {
"build": "microbundle",
"dev": "microbundle watch",
"build": "./scripts/build",
"lint": "eslint src --ext .ts,.tsx,.js,.jsx",
"prepare": "npm run build",
"test": "jest",
Expand Down Expand Up @@ -58,7 +67,6 @@
"eslint": "8.9.0",
"eslint-config-react-app": "7.0.0",
"jest": "27.4.2",
"microbundle": "0.14.2",
"postcss": "8.4.5",
"preact": "10.6.5",
"preact-render-to-string": "5.1.19",
Expand Down
89 changes: 89 additions & 0 deletions rollup.config.js
@@ -0,0 +1,89 @@
import babel from '@rollup/plugin-babel';
import resolve from '@rollup/plugin-node-resolve';

const input = './src/index.ts';

// please update the package.json to reflect any changes here
const bundles = createBundles({
// the `unpkg` UMD bundle (for usage without a bundler)
unpkg: {
format: 'umd',
targets: 'defaults',
output: { name: 'color2k' },
},
// legacy node bundle (note: this is currently used in jest-resolve)
main: {
format: 'cjs',
targets: 'node 4',
},
// the `module` bundle that should support older bundlers that used this entry
// point. (e.g. webpack 2)
module: {
format: 'es',
// `node 6` is a good target because it's closest to es2015 specified in the
// rollup spec: https://github.com/rollup/rollup/wiki/pkg.module
targets: 'node 6',
},
exports: {
// `node 12` is a good target because:
//
// this module entry point is used by several bundlers.
// namely: webpack, @rollup/plugin-node-resolve, wmr (via rollup)
//
// the lowest common denominator is **acorn 7** with ecmaVersion `2019`
// - @rollup/plugin-node-resolve supports as low as rollup 1.2
// - rollup 1.2 uses acorn 7 with ecmaVersion `2019` https://github.com/rollup/rollup/blob/v1.2.0/src/Module.ts#L105
// - webpack uses acorn 8 with ecmaVersion latest https://github.com/webpack/webpack/blob/v5.0.0/lib/javascript/JavascriptParser.js#L128
// - node 12 is the first version to completely support ES2019 https://node.green/#ES2019
//
// this module entry point is used by esbuild too.
// https://esbuild.github.io/api/#how-conditions-work
import: {
format: 'es',
targets: 'node 12',
extension: 'mjs',
},
require: {
format: 'cjs',
targets: 'node 12',
},
},
});

function createBundles(config, parents = []) {
if ('targets' in config) {
const { format, extension = 'js', output, targets } = config;

return [
{
input,
output: {
file: `./dist/index.${parents.join('.')}.${format}.${extension}`,
format,
sourcemap: true,
...output,
},
plugins: [
resolve({ extensions: ['.ts'], modulesOnly: true }),
babel({
babelrc: false,
configFile: false,
babelHelpers: 'bundled',
extensions: ['.ts'],
targets,
presets: [
['@babel/preset-env', { targets }],
'@babel/preset-typescript',
],
}),
],
},
];
}

return Object.entries(config).flatMap(([key, subConfig]) =>
createBundles(subConfig, [...parents, key])
);
}

export default bundles;
16 changes: 16 additions & 0 deletions scripts/build
@@ -0,0 +1,16 @@
#!/bin/bash

# use node_modules/.bin first
PATH=./node_modules/.bin:$PATH

# exit on any error
set -e

# clean
rm -rf dist

# build js
rollup -c

# generate types
tsc
11 changes: 6 additions & 5 deletions tsconfig.json
@@ -1,18 +1,19 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "Node",
"target": "esnext",
"module": "esnext",
"moduleResolution": "node",
"jsx": "react",
"strict": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"isolatedModules": true,
"declaration": true,
"emitDeclarationOnly": true,
"outDir": "./dist",
"noUnusedLocals": true,
"noUnusedParameters": true,
"baseUrl": ".",
"baseUrl": "."
},
"include": ["src/**/*"],
"exclude": ["src/**/*.test.ts"]
Expand Down

0 comments on commit 31b79a1

Please sign in to comment.