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

Conversation

angeloashmore
Copy link
Contributor

@angeloashmore angeloashmore commented Apr 26, 2020

The following changes are done in preparation for #10.

  • Adds Rollup as a bundler to output CommonJS and ES module. ES module supports tree shaking.
  • Updates Typescript and adds tslib.
  • Adds @ts-ignore in pickTrueObjectKeys (jump to line). Not sure how to fix the type warning. The code is admittedly too complex for me to understand.

src/index.ts Outdated Show resolved Hide resolved
import typescript from 'rollup-plugin-typescript2';
import autoExternal from 'rollup-plugin-auto-external';

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!

@@ -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()],
}

package.json Outdated Show resolved Hide resolved
These upgrades/additions were remnants from using @rollup/typescript
before changing to rollup-plugin-typescript2. rollup-plugin-typescript2
does not require tslib.
When changing the module compiler option to esnext from commonjs, one
must also define the correct module resolution strategy.
@angeloashmore
Copy link
Contributor Author

Trimmed down the PR to just include the Rollup changes.

@OliverJAsh OliverJAsh merged commit 64cef3c into unsplash:master May 4, 2020
@OliverJAsh
Copy link
Member

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants