Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CORE: allow to disable setting the pbjs global variable #9568

Merged
merged 12 commits into from
Mar 29, 2023

Conversation

olafbuitelaar
Copy link
Contributor

Type of change

  • Bugfix

  • Feature

  • New bidder adapter

  • Code style update (formatting, local variables)

  • Refactoring (no functional changes, no api changes)

  • Build related changes

  • CI related changes

  • Does this change affect user-facing APIs or examples documented on http://prebid.org?

  • Other

Description of change

for embedding prebid as a pure module in other projects, it always has the side effect of settings the prebid global variable on the window object.
This PR, gives the user the option, to disable this behaviour, either by setting; defineGlobal to false in the package.json or via the options in the babel-loaded module.
This PR sets the prebid global everywhere to a localscope, and uses the function getGlobal to resolve the general reference.
when the defineGlobal is kept true, behaviour should be unaffected.

path.node.body.push(...api.parse(`window.${pbGlobal}.installedModules.push('${modName}');`, {filename: state.filename}).program.body);
} else {
if (!hasGetGlobalsImport(path.node.body)) {
path.node.body.unshift(...api.parse(`import {getGlobal} from '../src/prebidGlobal.js';const ${pbGlobal} = getGlobal();`, {filename: state.filename}).program.body);
Copy link
Collaborator

@dgirardi dgirardi Feb 21, 2023

Choose a reason for hiding this comment

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

I am uneasy about this putting names in the module's scope without checking for conflicts, and the fact that - if I understand this right - this has the "side effect" of removing references to the global from module code; by that I mean that it's not clear from this code that that's the intent; it appears to be dealing with installedModules only. I think its better to be explicit.

What do you think about this strategy:

  • refactor all uses of the $$PREBID_GLOBAL$$ macro, with the exception of prebidGlobal.js, to be simple and explicit uses of getGlobal(). const pbjs = getGlobal() seems to me much better than const $$PREBID_GLOBAL$$ = getGlobal() or just direct use of $$PREBID_GLOBAL$$ that are covertly translated to the first form through this black magic.
  • remove the linter exception for $$PREBID_GLOBAL$$ to stop future circumvention of getGlobal(). Also, do not add one for $$USE_PREBID_GLOBAL$$, both can be single-file comment exceptions.
  • for dealing with installedModules, I think this should try to stay invisible to the code's namespace. that means generating code like import {getGlobal as __r} from 'src/prebidGlobal.js; __r().installedModules.push(...), with some logic to use __r2 instead of __r if the latter is taken and so on (through scope.hasBinding, there's an example in here when dealing with FEATURES).

Copy link
Contributor Author

@olafbuitelaar olafbuitelaar Feb 22, 2023

Choose a reason for hiding this comment

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

  • I've factored out all all references to $$PREBID_GLOBAL$$, except in the prebidGlobal.js. There was 1 module (rivrAnalyticsAdapter.js) which requires the global variable name.
  • a common pattern i see, is that many modules just reference the global to get the version, e.g. $$PREBID_GLOBAL$$.version, maybe an utility function should be introduced for this, or maybe encourage the usage of 'v$prebid.version$'?
  • i've removed the global eslint rules
  • i've updated the babel plugin, to not introduce the extra variable, call the getGlobal() directly to set the modules, also i removed the different paths for using a global variable, or only use the internal instance.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

forget my comment about rivrAnalyticsAdapter.js, it uses the reference after all

Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe an utility function should be introduced for this, or maybe encourage the usage of 'v$prebid.version$'?

I like the first idea because it would work better with the minifier, but I don't think it has to be part of this PR. (I don't like using v$prebid.version$ because these macros are already more of a hack than I'd like).

The approach you have here is still in my opinion technically incorrect (the best kind of incorrect) because a module could theoretically use the name getGlobal for its own unrelated purposes, which makes the autogenerated import intrusive. It's not a huge deal but I think solving the problem also makes the code overall simpler: i pushed this update do demonstrate, which should also convince circleCI to run the tests.

Copy link
Contributor Author

@olafbuitelaar olafbuitelaar Feb 22, 2023

Choose a reason for hiding this comment

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

fair point, only right now it's possible to import getGlobal twice (once by the module, and once auto-generated), not sure how well this would finally get minimized. otherwise maybe the 2 solutions could get merged, where this new variable is only introduced in this if 1a3e72e#diff-d38260cead55420ead0e3bc806c91343b780d84c61577742edb484a4add1dc22L96 but i'm fine with it as it right now, as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i would also prefer the utility function to resolve the version, but agree this shouldn't be part of this PR

Copy link
Collaborator

Choose a reason for hiding this comment

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

I stole the same utility function idea for registerModule, so even if the minifier could not deal with the double import (I don't know either) it should not be an issue now. In fact there is an improvement as until now it could not touch the .installedModules property accessor, but it can rename the utility function.

olafbuitelaar and others added 4 commits February 22, 2023 11:17
…tead

updated the babel module, to directly call the getGlobal function
removed eslint global exception, and added them as local exceptions
@jsnellbaker jsnellbaker added the needs 2nd review Core module updates require two approvals from the core team label Feb 27, 2023
@robertrmartinez
Copy link
Collaborator

I think we have the issue where the circle ci won't run since it is not the prebid org instance.

What exactly do we need to do to get it to run @dgirardi @ChrisHuie ?

@olafbuitelaar
Copy link
Contributor Author

olafbuitelaar commented Mar 2, 2023

@dgirardi are you sure the src/prebidGlobal.js (1a3e72e#diff-d38260cead55420ead0e3bc806c91343b780d84c61577742edb484a4add1dc22R73) change will always work? As i'm having issues with it when using dynamic module importing e.g. import('prebid.js/modules/appnexusBidAdapter') and only the implementation (1a3e72e#diff-d38260cead55420ead0e3bc806c91343b780d84c61577742edb484a4add1dc22L96) with absolute relative path's (../) works for me in this case.

@olafbuitelaar
Copy link
Contributor Author

@dgirardi it looks like your commit introduced a merge conflict again, since the rubicon adapter was updated, that change shouldn't be needed anymore, since that code is gone.

@dgirardi
Copy link
Collaborator

dgirardi commented Mar 2, 2023

@olafbuitelaar thank you for calling out the relative path issue; it should be (finally) fixed.

@ChrisHuie ChrisHuie removed needs 2nd review Core module updates require two approvals from the core team needs docs labels Mar 29, 2023
@ChrisHuie ChrisHuie merged commit 9a180db into prebid:master Mar 29, 2023
mscottnelson added a commit to 33Across/Prebid.js that referenced this pull request Mar 31, 2023
* upstream/master:
  Prebid Server adapter: fledge support (prebid#9342)
  Taboola Bid Adapter: resolve AUCTION_PRICE macro (prebid#9735)
  Revert "IntentIQ Analytics Module : initial release (prebid#9322)" (prebid#9734)
  IntentIQ Analytics Module : initial release (prebid#9322)
  Increment version to 7.44.0-pre
  Prebid 7.43.0 release
  Core: allow restriction of cookies / localStorage through `bidderSettings.*.storageAllowed` (prebid#9660)
  Add 9 Dots Media alias (prebid#9699)
  Smaato: Adapters that accept geolocation data from bid parameters should also accept it from ortb2.(device|user).geo (prebid#9676) (prebid#9725)
  Adloox AdServer Video : lengthen test timeouts (prebid#9728)
  RTBHouse Bid Adapter: change `source.tid` to contain `auctionId` and populate imp-level `ext.tid` (prebid#9726)
  chore: update `getAudiencesAsBidderOrtb2` implementation and test (prebid#9720)
  CORE: allow to disable setting the pbjs global variable (prebid#9568)
  Display.io Bid Adapter: ad request parameters renaming, user session saving (prebid#9553)
  CORE:  add bid to winningBids when marking as used (prebid#9612)
  Concert Bid Adapter: enable support for GPP consent and remove user sync (prebid#9700)
  Sonobi Bid Adapter: add IntentIq Id (prebid#9649)
  added fix and support for multibid module (prebid#9602)
  Update Permutive RTD documentation (prebid#9697)
  Adkernel Bid Adapter: add adlive.io alias (prebid#9714)
  FPD Enrichment: use low entropy method by default to fetch user agent data (prebid#9711)
  GumGum Bid Adapter : send gpp data in bidrequest (prebid#9707)
  Adagio Bid Adapter : add new params `splitKeyword` and `dl` to bidRequest payload (prebid#9694)
  fix lint and test failures
  Increment version to 7.43.0-pre
  Prebid 7.42.0 release
  Fluct Bid Adapter: add user sync support (prebid#9651)
  VIS.X fix onTimeout data (prebid#9657)
  ShowHeroes Bid Adapter: added support for USP (prebid#9681)
  Core: improve FPD enrichment (prebid#9659)
  changed the URL (prebid#9698)
  Permutive RTD Module: migrate appnexus to ortb2 (prebid#9630)
  Disable describe.only and it.only (prebid#9693)
  DistroScale bidder enhancement (prebid#9641)
  minutemediaplus Bid Adapter - pass gpp, sua and bid data to server. (prebid#9637)
  LiveIntent UserId module: update LiveConnect dependency (prebid#9672)
  Criteo Bid Adapter: reinforce adomain type in case of missmatch (prebid#9687)
  SmartyTech Bid Adapter : add size parameters (prebid#9692)
  Revert "Nativo Bid Adapter: adding UserId support (prebid#9583)" (prebid#9691)
  GDPR consent management: accept static config without `getTCData` (prebid#9664)
  Kulturemedia Bid Adapter: New Adapter (prebid#9613)
  Nativo Bid Adapter: adding UserId support (prebid#9583)
  Underdog Media Bid Adapter: Switch request to method to POST (prebid#9547)
  ZetaGlobalSsp: provide schain (prebid#9678)
  Next Millennium Bid Adapter : added `imp[].id` required parameter for openrtb 2.5 request. (prebid#9675)
  Increment version to 7.42.0-pre
  Prebid 7.41.0 release
  DFP video adserver module: set `description_url` to pub's URL by default; do not skip setting it if `cache.url` is set (prebid#9665)
  support the timeout parameter in the conversant adapter (prebid#9673)
  Browsi Bid Adapter: initial release (prebid#9562)
  feat(permutiveRtd): add `ix` and custom cohort `ortb2.user.data` (prebid#9631)
  Freewheel SSP Adapter: add prebid version in ad request (prebid#9667)
  Yandex Bid Adapter: (prebid#9604)
  Bump webpack from 5.74.0 to 5.76.0 (prebid#9668)
  Deleted the default empty string from  userConsent argument in the module's  init-function. (prebid#9663)
  AdUp Technology bid adapter: avoid modification of bid request (prebid#9656)
  NoBid Bid Adapter: support for Floors (prebid#9635)
  Freewheel SSP Bid Adapter : support userIdAsEids (prebid#9655)
  Missena: add userEids, add network and cpm to tracking (prebid#9645)
  IX Bid Adapter: update additional consent field (prebid#9650)
  Aduptech Bid Adapter : add GVLID (prebid#9658)
  Core: make video cache timeout configurable (prebid#9578)
  Delete flocIdSystem.js (prebid#9652)
  TheMediaGrid Bid Adapter : support gpp (prebid#9629)
  Stv Bid Adapter : initial adapter release (prebid#9533)
  CleanMediaNet Bid Adapter : add userid support and update testing (prebid#9608)
  Update creative.html (prebid#9648)
  PubWise Bid Adapter: support video and improve tests (prebid#9576)
  Increment version to 7.41.0-pre
  Prebid 7.40.0 release
  Rubicon Bid Adapter: add size 1x2 (prebid#9644)
  main>modules\neuwoRtdProvider.js > apiUrl format handling improved, removed unnecessary parameter integrationExamples\gpt\neuwoRtdProvider_example.html > fixed render-step handling on warning (prebid#9646)
  Core: fix native render when adUnits defines `mediaTypes.native.ortb` but adapter replies with "legacy" native bid (prebid#9638)
  kueezRtb Bid Adapter: pass sua data to server. (prebid#9643)
  update Mediago & Discovery BidAdapter:remove size filter (prebid#9585)
  Permutive RTD Module: migrate magnite to ortb2 (prebid#9555)
  Vidazoo Bid Adapter: pass sua params. (prebid#9636)
  ADJS-1271-send-envelope-param-for-lexicon (prebid#9634)
  Nexx360 Bid Adapter: native support added and ortbConverter usage (prebid#9626)
  PBjs Core: do not rely on an extendable `window.Promise` (prebid#9558)
  TheMediaGrid Bid Adapters : do not use jwp segments from bid.rtd field (prebid#9627)
  read video size from playerSize (prebid#9625)
  Lemma Digital Bid Adapter : initial adapter release (prebid#9532)
  smallfix on response validation (prebid#9623)
  Updated adf adapter to support native with type; use ortb request for natives (prebid#9616)
  kargo adapter - adding prebid version to requests (prebid#9620)
  Rubicon bid adapter: remove pchain support (prebid#9621)
  trigger build
  revert
  tag out markWinningBidAsUsed entirely
  remove unnecessary feature tag
  tag out ORTB video conversion utils
  tag out video-related code in native.js
  tag out video-related code in renderAd
  replace callPrebidCache with getHook result
  tag out adpod-related logic
  Define "VIDEO" compile time feature flag
jorgeluisrocha pushed a commit to jwplayer/Prebid.js that referenced this pull request May 23, 2023
* allow to disable settings the pbjs global variable

* factored out all references to $$PREBID_GLOBAL$$ to use getGlobal instead
updated the babel module, to directly call the getGlobal function
removed eslint global exception, and added them as local exceptions

* fix comments

* make module use getGlobal

* Isolate `installedModules` management from module namespaces

* Use relative import paths in autogenerated code for  `installedModules`

* Remove $$PREBID_GLOBAL$$ macro ref from rubicon adapter

* Revert "Remove $$PREBID_GLOBAL$$ macro ref from rubicon adapter"

This reverts commit 16e25dd.

---------

Co-authored-by: Demetrio Girardi <dgirardi@prebid.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants