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

Prebid core: move generation of 'installedModules' to babel #7739

Merged
merged 1 commit into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 || [];
Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh, I like that change! ❤️


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