Skip to content

Commit

Permalink
Isolate installedModules management from module namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
dgirardi committed Feb 22, 2023
1 parent 7a21b57 commit 1a3e72e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 37 deletions.
41 changes: 8 additions & 33 deletions plugins/pbjsGlobals.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,44 +59,19 @@ module.exports = function(api, options) {
return null;
}

function hasGetGlobalsImport(body) {
for (let i = 0; i < body.length; i++) {
if (body[i].type === 'ImportDeclaration' && body[i].source.value.match(/prebidGlobal(\.js)?$/)) {
for (let j = 0; j < body[i].specifiers.length; j++) {
if (body[i].specifiers[j].imported.name === 'getGlobal') {
return body[i].specifiers[j].local.name;
}
}
}
}
return false;
}
function getPathDepth(path) {
path = path.replace(/\\/g, '/');
let pathSegs = path.split('/');
let depth = 0;
for (let i = pathSegs.length - 2; i >= 0; i--) {
if (pathSegs[i] === 'modules') {
break;
}
depth++;
}
return depth;
}

return {
visitor: {
Program(path, state) {
const modName = getModuleName(state.filename);
if (modName != null) {
// append "registration" of module file to $$PREBID_GLOBAL$$.installedModules

let getGlobalName = hasGetGlobalsImport(path.node.body);
if (getGlobalName === false) {
path.node.body.unshift(...api.parse(`import {getGlobal} from '${'../'.repeat(getPathDepth(state.filename) + 1)}src/prebidGlobal.js';`, {filename: state.filename}).program.body);
getGlobalName = 'getGlobal';
}
path.node.body.push(...api.parse(`${getGlobalName}().installedModules.push('${modName}');`, {filename: state.filename}).program.body);
// append "registration" of module file to getGlobal().installedModules
let i = 0;
let registerName;
do {
registerName = `__r${i++}`
} while (path.scope.hasBinding(registerName))
path.node.body.unshift(...api.parse(`import {registerModule as ${registerName}} from 'src/prebidGlobal.js';`, {filename: state.filename}).program.body);
path.node.body.push(...api.parse(`${registerName}('${modName}');`, {filename: state.filename}).program.body);
}
},
StringLiteral(path) {
Expand Down
12 changes: 8 additions & 4 deletions src/prebidGlobal.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
// global defination should happen BEFORE imports to avoid global undefined errors.
/* global $$DEFINE_PREBID_GLOBAL$$ */
const scope = !$$DEFINE_PREBID_GLOBAL$$ ? {} : window;
scope.$$PREBID_GLOBAL$$ = (scope.$$PREBID_GLOBAL$$ || {});
scope.$$PREBID_GLOBAL$$.cmd = scope.$$PREBID_GLOBAL$$.cmd || [];
scope.$$PREBID_GLOBAL$$.que = scope.$$PREBID_GLOBAL$$.que || [];
const global = scope.$$PREBID_GLOBAL$$ = scope.$$PREBID_GLOBAL$$ || {};
global.cmd = global.cmd || [];
global.que = global.que || [];

// create a pbjs global pointer
if (scope === window) {
Expand All @@ -13,5 +13,9 @@ if (scope === window) {
}

export function getGlobal() {
return scope.$$PREBID_GLOBAL$$;
return global;
}

export function registerModule(name) {
global.installedModules.push(name);
}

0 comments on commit 1a3e72e

Please sign in to comment.