From 4d34d1cbd8c6f9b8990012ced5a6126ee4221cc9 Mon Sep 17 00:00:00 2001 From: Demetrio Girardi Date: Thu, 18 Nov 2021 15:14:28 -0800 Subject: [PATCH] Prebid core: move generation of 'installedModules' to babel `pbjs.installedModules` does not work correctly when using prebid as an npm dependency (https://github.com/prebid/Prebid.js/issues/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. --- gulpfile.js | 8 -------- plugins/pbjsGlobals.js | 29 ++++++++++++++++++++++++++++- src/prebid.js | 3 +-- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/gulpfile.js b/gulpfile.js index 98caf1d7312..c86357372f1 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -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'; @@ -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()); } @@ -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')); } diff --git a/plugins/pbjsGlobals.js b/plugins/pbjsGlobals.js index bf3c9033ee6..73912d8126e 100644 --- a/plugins/pbjsGlobals.js +++ b/plugins/pbjsGlobals.js @@ -1,11 +1,13 @@ 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}` }; @@ -13,8 +15,33 @@ module.exports = function(api, options) { '$$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)) { diff --git a/src/prebid.js b/src/prebid.js index 855d53d7de0..ee20754bb62 100644 --- a/src/prebid.js +++ b/src/prebid.js @@ -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 || [];