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.

Global object (window) confusion

See original GitHub issue

Describe the bug

Readme states that MatomoTracker instance may be initialized like so

import MatomoTracker from '@datapunt/matomo-tracker-js'
const MatomoInstance = new window.MatomoTracker({})

and later uses syntax without window:

import MatomoTracker from '@datapunt/matomo-tracker-js'
const MatomoInstance = new MatomoTracker({})

however latter doesn’t work (at least in rollup builds) as it holds es-module wrapper {__esModule: true, default: ƒ}.

~Same object is also leaking via window.MatomoTracker_1.~

IMHO it’s not necessary to set class on window object, as it’s already exported via IIFE in rollup config.

To Reproduce As in description

Expected behavior When importing MatomoTracker it’s possible to initialize it via new MatomoTracker({}). The window.MatomoTracker property is available when using ES5 prebuilt bundle.min.js.

Screenshots N/A

Desktop (please complete the following information): N/A

Smartphone (please complete the following information): N/A

Additional context Proposed solution:

  • remove code block responsible for setting global object
  • to preserve semver backward compatibility release in v1.x
  • fix readme so examples are consistent

When running yarn run build, rollup shows warnings related to this issue:

src/index.ts → bundle.min.js...
(!) Mixing named and default exports
https://rollupjs.org/guide/en/#output-exports
The following entry modules are using named and default exports together:
src\index.ts

Consumers of your bundle will have to use chunk['default'] to access their default export, which may not be what you want. Use `output.exports: 'named'` to disable this warning

This is caused by exporting both default and named exports together in index.ts:

export default MatomoTracker

export { types }

I wonder do we need to export types (here)?

I’d recommend removing types from index.ts and defining in package.json

{
  "main": "lib/index.js",
  "types": "lib/types.ts"
}

Related commit: https://github.com/Amsterdam/matomo-tracker/commit/c6c2324627a4a75dee35d3538233ac93a5e2bf67

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
jonkoopscommented, Jul 30, 2020

Correct, and in the next version we will get rid of window.MatomoTracker entirely.

0reactions
piotr-czcommented, Aug 4, 2020

Probably fixed by #196 and #197

Read more comments on GitHub >

github_iconTop Results From Across the Web

The confusion about Global object in JavaScript - Stack Overflow
I am beginner at JavaScript and I am trying to understand the Global window object in JavaScript. So, is it ok if I...
Read more >
Global object (window) confusion · Issue #186 - GitHub
When importing MatomoTracker it's possible to initialize it via new MatomoTracker({}) . The window.MatomoTracker property is available when ...
Read more >
JavaScript's Global Object - LearnHowToProgram.com
For JavaScript that's run in the browser, its global object is set to the window object. We'll start with a review of global...
Read more >
Understanding the JavaScript Window Object - SitePoint
In a browser environment the global object is the window object, which represents the browser window that contains a web page.
Read more >
A Subtle Difference Between Window Properties and Global ...
In the case that _gaq is a property of the window object - maybe you initialized it like this: window._gaq = ['the', 'existing' ......
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