Skip to content

Commit

Permalink
Prebid core: move generation of 'installedModules' to babel (prebid#7739
Browse files Browse the repository at this point in the history
)

`pbjs.installedModules` does not work correctly when using prebid as an npm dependency (prebid#7287), because it's generated by a gulp task.

This moves generation of `installedModules` to the `pbjsGlobals` babel plugin that we ask npm consumers to use for prebid.
  • Loading branch information
dgirardi authored and Chris Pabst committed Jan 10, 2022
1 parent ce024e7 commit eab906d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
8 changes: 0 additions & 8 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,6 @@ function watch(done) {
done();
};

function makeModuleList(modules) {
return modules.map(module => {
return '"' + module + '"'
});
}

function makeDevpackPkg() {
var cloned = _.cloneDeep(webpackConfig);
cloned.devtool = 'source-map';
Expand All @@ -158,7 +152,6 @@ function makeDevpackPkg() {
return gulp.src([].concat(moduleSources, analyticsSources, 'src/prebid.js'))
.pipe(helpers.nameModules(externalModules))
.pipe(webpackStream(cloned, webpack))
.pipe(replace(/('|")v\$prebid\.modulesList\$('|")/g, makeModuleList(externalModules)))
.pipe(gulp.dest('build/dev'))
.pipe(connect.reload());
}
Expand All @@ -176,7 +169,6 @@ function makeWebpackPkg() {
.pipe(helpers.nameModules(externalModules))
.pipe(webpackStream(cloned, webpack))
.pipe(terser())
.pipe(replace(/('|")v\$prebid\.modulesList\$('|")/g, makeModuleList(externalModules)))
.pipe(gulpif(file => file.basename === 'prebid-core.js', header(banner, { prebid: prebid })))
.pipe(gulp.dest('build/dist'));
}
Expand Down
29 changes: 28 additions & 1 deletion plugins/pbjsGlobals.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,47 @@

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$$': options.globalVarName || prebid.globalVarName,
'$$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}');`).program.body);
}
},
StringLiteral(path) {
Object.keys(replace).forEach(name => {
if (path.node.value.includes(name)) {
Expand Down
3 changes: 1 addition & 2 deletions src/prebid.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ $$PREBID_GLOBAL$$.libLoaded = true;
$$PREBID_GLOBAL$$.version = 'v$prebid.version$';
logInfo('Prebid.js v$prebid.version$ loaded');

// modules list generated from build
$$PREBID_GLOBAL$$.installedModules = ['v$prebid.modulesList$'];
$$PREBID_GLOBAL$$.installedModules = $$PREBID_GLOBAL$$.installedModules || [];

// create adUnit array
$$PREBID_GLOBAL$$.adUnits = $$PREBID_GLOBAL$$.adUnits || [];
Expand Down

0 comments on commit eab906d

Please sign in to comment.