Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Global object (window) confusion #186

Closed
piotr-cz opened this issue Jul 30, 2020 · 5 comments · Fixed by #196
Closed

Global object (window) confusion #186

piotr-cz opened this issue Jul 30, 2020 · 5 comments · Fixed by #196
Labels
documentation Improvements or additions to documentation enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Milestone

Comments

@piotr-cz
Copy link
Contributor

piotr-cz commented Jul 30, 2020

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: c6c2324

@jonkoops jonkoops added documentation Improvements or additions to documentation enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed labels Jul 30, 2020
@jonkoops
Copy link
Owner

Thanks for submitting this issue @piotr-cz. I agree the documentation is quite confusing on this matter. I am adding this to the milestone for the next version where we intend to remove the global variable and only use the module version.

@jonkoops jonkoops added this to the 0.2.0 milestone Jul 30, 2020
@piotr-cz
Copy link
Contributor Author

Thanks,
so for now all examples in readme except bundleshould use new window.MatomoTracker instantiation style

@jonkoops
Copy link
Owner

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

@piotr-cz
Copy link
Contributor Author

piotr-cz commented Jul 30, 2020

There are more problems with window.MatomoTracker:

  • when using rollup, it treats properties on window object as global dependency and doesn't include in builds
  • when using terser, it tends to shorten property name to for example window.jt.default

@piotr-cz
Copy link
Contributor Author

piotr-cz commented Aug 4, 2020

Probably fixed by #196 and #197

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
documentation Improvements or additions to documentation enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants