Skip to content

Commit

Permalink
fix: use local pbjsGlobals.js
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelpeixe committed Jan 26, 2022
1 parent 5bb1e3d commit 2e42f5a
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
config/pbjsGlobals.js
92 changes: 92 additions & 0 deletions config/pbjsGlobals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@

let t = require('@babel/core').types;
let prebid = require('../package.json');
const path = require('path');

module.exports = function(api, options) {
const pbGlobal = options.globalVarName || prebid.globalVarName;
let replace = {
'$prebid.version$': prebid.version,
'$$PREBID_GLOBAL$$': pbGlobal,
'$$REPO_AND_VERSION$$': `${prebid.repository.url.split('/')[3]}_prebid_${prebid.version}`
};

let identifierToStringLiteral = [
'$$REPO_AND_VERSION$$'
];

const PREBID_ROOT = path.resolve(__dirname, '..');

function getModuleName(filename) {
const modPath = path.parse(path.relative(PREBID_ROOT, filename));
if (modPath.ext.toLowerCase() !== '.js') {
return null;
}
if (modPath.dir === 'modules') {
// modules/moduleName.js -> moduleName
return modPath.name;
}
if (modPath.name.toLowerCase() === 'index' && path.dirname(modPath.dir) === 'modules') {
// modules/moduleName/index.js -> moduleName
return path.basename(modPath.dir);
}
return null;
}

return {
visitor: {
Program(path, state) {
const modName = getModuleName(state.filename);
if (modName != null) {
// append "registration" of module file to $$PREBID_GLOBAL$$.installedModules
path.node.body.push(...api.parse(`window.${pbGlobal}.installedModules.push('${modName}');`, {filename: modName}).program.body);
}
},
StringLiteral(path) {
Object.keys(replace).forEach(name => {
if (path.node.value.includes(name)) {
path.node.value = path.node.value.replace(
new RegExp(escapeRegExp(name), 'g'),
replace[name]
);
}
});
},
TemplateLiteral(path) {
path.traverse({
TemplateElement(path) {
Object.keys(replace).forEach(name => {
['raw', 'cooked'].forEach(type => {
if (path.node.value[type].includes(name)) {
path.node.value[type] = path.node.value[type].replace(
new RegExp(escapeRegExp(name), 'g'),
replace[name]
);
}
});
});
}
});
},
Identifier(path) {
Object.keys(replace).forEach(name => {
if (path.node.name === name) {
if (identifierToStringLiteral.includes(name)) {
path.replaceWith(
t.StringLiteral(replace[name])
);
} else {
path.replaceWith(
t.Identifier(replace[name])
);
}
}
});
}
}
};
};

function escapeRegExp(str) {
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
3 changes: 2 additions & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,14 @@ const webpackConfig = getBaseWebpackConfig(

webpackConfig.module.rules.push( {
test: /.js$/,
include: new RegExp( `\\${ path.sep }prebid\.js` ),
include: new RegExp( `\\${ path.sep }prebid\\.js` ),
use: {
loader: 'babel-loader',
// presets and plugins for Prebid.js must be manually specified separate from your other babel rule.
// this can be accomplished by requiring prebid's .babelrc.js file (requires Babel 7 and Node v8.9.0+)
options: {
...prebidConfig,
configFile: false,
},
},
} );
Expand Down

0 comments on commit 2e42f5a

Please sign in to comment.