question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for Named Exports

See original GitHub issue

I came across this while implementing Typewriter on an internal project. After doing some manual refactoring a few times I think it’d be great to have this baked in into Typewriter.

Currently typewriter exports wraps all your tracking calls inside a class you can instance to execute and allows you to pass an analytics instance. i.e.

Current Status

Show

const genOptions = (context = {}) => ({
  context: {
    ...context,
    typewriter: {
      name: "gen-js",
      version: "5.1.1"
    }
  }
});
export default class Analytics {
  constructor(analytics) {
    this.analytics = analytics || { track: () => null };
  }
  someThingToTrack(props, context) {
    this.analytics.track("Tracking a Thing", props, genOptions(context));
  }
  (... Many other methods)
}

We where using only a few of those tracking methods in our specific project, so we where shipping a lot of unused code that we weren’t able to treeshake. While it might not look like a a huge code reduction if we add the --runtimeValidation false flag, but it is if we want to keep those 😢

So after talking with @colinking on slack, he mentioned making this issue could be a good first step into the discussion.

Proposal

Final export (after running typewriter gen-js with some flags i.e --runtimeValidation false --export class) would look like this:

Show

let analytics = (window && window.analytics) || { track: () => null } 

export const setaAnalytics = (newAnalytics) => { analytics = newAnalytics }

const genOptions = (context = {}) => ({
  context: {
    ...context,
    typewriter: {
      name: 'gen-js',
      version: '5.0.1'
    }
  }
})


export const someThingToTrack = (props, context, callback = () => {}) => {
  analytics.track('Tracking a Thing', props, genOptions(context), callback)
}

Explanation

  • Implements a new flag --export with parameters class & named-exports (or functions?)
    • We set class as the default value for it to keep backwards compatibility
  • Makes every method now a named export
  • Enables a module variable to be the instance of analytics.js
    • Adds method to be able to override that variable
  • Adds support for the callback parameter

Final Thoughts

  • Easier tree shake / dead code elimination
  • Yei for smaller bundle sizes ❤️

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
fforrescommented, Oct 22, 2019

We are. I’m a big advocate for protocols / define our tracking-plan ( and tw specially 😄 ) It’s taking me more than I hoped for to get someone that can help us with that 😅, but we are getting there 🤘

0reactions
colinkingcommented, Oct 22, 2019

❤️ @fforres

HMU if Brex is interested in trying out Typewriter!

Read more comments on GitHub >

github_iconTop Results From Across the Web

export - JavaScript - MDN Web Docs
This re-exports all named exports from mod as the named exports of the current module, but the default export of mod is not...
Read more >
Node.js now supports named imports from CommonJS ...
First Things First: What are named exports? · The Big Gotcha: Named imports don't work for all CJS modules · A Comedy of...
Read more >
Named export 'Types' not found. The requested module ...
Named export 'Types' not found. The requested module 'mongoose' is a CommonJS module, which may not support all module.exports as named exports.
Read more >
How to write CommonJS exports that can be name-imported ...
This blog post explores how to write CommonJS modules so that their exports can be name-imported from ESM modules on Node.js.
Read more >
Misleading error that module does not provide export #32137
SyntaxError: The requested module './dependency.cjs' is expected to be of type CommonJS, which does not support named exports. CommonJS modules ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found