Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

feat: add ES module support #11

Merged
merged 3 commits into from
May 4, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
{
"name": "ts-imgix",
"main": "./target/index.js",
"main": "./target/index.cjs.js",
"module": "./target/index.esm.js",
"typings": "./target/index.d.ts",
"sideEffects": false,
"repository": {
"type": "git",
"url": "https://github.com/unsplash/ts-imgix.git"
},
"version": "0.0.11",
"scripts": {
"lint": "tslint --project ./tsconfig.json",
"compile": "rm -rf ./target/ && tsc",
"compile": "rm -rf ./target/ && rollup -c",
"test": "npm run compile && node --require source-map-support/register ./target/tests.js",
"format": "prettier --write './**/*.{ts,tsx,js,json,md}' '.prettierrc'",
"prepublishOnly": "npm run test && npm run lint && npm run compile"
Expand All @@ -22,10 +24,14 @@
},
"devDependencies": {
"prettier": "^1.15.3",
"rollup": "^2.7.2",
"rollup-plugin-auto-external": "^2.0.0",
"rollup-plugin-typescript2": "^0.27.0",
"source-map-support": "^0.5.9",
"tslib": "^1.11.1",
"tslint": "^5.11.0",
"tslint-language-service": "^0.9.9",
"tslint-no-unused": "^0.2.0-alpha.1",
"typescript": "^3.2.2"
"typescript": "^3.8.3"
}
}
20 changes: 20 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import typescript from 'rollup-plugin-typescript2';
import autoExternal from 'rollup-plugin-auto-external';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this do in our case? I'm new to Rollup

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • typescript runs the code through tsc
  • autoExternal sets dependencies and peerDependencies as external packages so the bundled code leaves them as imports. Non-external packages will be bundled in the compiled output.

rollup-plugin-auto-external could be replaced with an inlined version. It effectively does the following:

import pkg from './package.json'

// Returns a function that returns true if the provided package name is in `externalArr`.
const makeExternalPredicate = externalArr => {
  if (externalArr.length === 0) {
    return () => false
  }
  const pattern = new RegExp(`^(${externalArr.join('|')})($|/)`)
  return id => pattern.test(id)
}

export default {
  input: 'src/index.ts',
  output: [
    { file: pkg.main, format: 'cjs' },
    { file: pkg.module, format: 'es' },
  ],
  external: makeExternalPredicate([
    ...Object.keys(pkg.dependencies || {}),
    ...Object.keys(pkg.peerDependencies || {}),
  ]),
  plugins: [typescript()],
}


import pkg from './package.json';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!


export default [
{
input: 'src/index.ts',
output: [
{ file: pkg.main, format: 'cjs', sourcemap: true },
{ file: pkg.module, format: 'es', sourcemap: true },
],
plugins: [autoExternal(), typescript()],
},
{
input: 'src/tests.ts',
output: { file: 'target/tests.js', format: 'cjs' },
plugins: [autoExternal(), typescript()],
},
];
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export type ImgixUrlQueryParams = {
const pickTrueInObject = <K extends string>(obj: Record<K, boolean>): Partial<Record<K, true>> =>
pickBy(obj, (_key, value): value is true => value);
const pickTrueObjectKeys = pipe(
// @ts-ignore
pickTrueInObject,
// tslint:disable-next-line no-unbound-method
Object.keys,
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"module": "commonjs",
"module": "esnext",
// Assumption: most consumers of this lib will be using UglifyJS which does not support
// ES2015+ syntax, so we target ES5 instead.
"target": "es5",
Expand Down
Loading