Skip to content

Commit af15395

Browse files
authored
Build sytem: allow customization of some build options (such as global name) to NPM consumers (#13685)
* Build system: translate exports (and not just imports) from .ts files * add global options * contain use of 16010PREBID_GLOBAL16010 macro * allow custom global / defineGlobal * allow distUrlBase customization * allow extensionless import * update README * fix eslint config * consistent quotes * remove useless comment
1 parent 8071258 commit af15395

File tree

74 files changed

+631
-432
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+631
-432
lines changed

README.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Prebid.js is open source software that is offered for free as a convenience. Whi
2424
## Usage (as a npm dependency)
2525

2626
**Note**: versions prior to v10 required some Babel plugins to be configured when used as an NPM dependency -
27-
refer to [v9 README](https://github.com/prebid/Prebid.js/blob/9.43.0/README.md).
27+
refer to [v9 README](https://github.com/prebid/Prebid.js/blob/9.43.0/README.md). See also [customize build options](#customize-options)
2828

2929
```javascript
3030
import pbjs from 'prebid.js';
@@ -56,6 +56,37 @@ declare global {
5656
}
5757
```
5858

59+
<a id="customize-options"></a>
60+
61+
### Customize build options
62+
63+
If you're using Webpack, you can use the `prebid.js/customize/webpackLoader` loader to set the following options:
64+
65+
| Name | Type | Description | Default |
66+
| ---- | ---- | ----------- | ------- |
67+
| globalVarName | String | Prebid global variable name | `"pbjs"` |
68+
| defineGlobal | Boolean | If false, do not set a global variable | `true` |
69+
| distUrlBase | String | Base URL to use for dynamically loaded modules (e.g. debugging-standalone.js) | `"https://cdn.jsdelivr.net/npm/prebid.js/dist/chunks/"` |
70+
71+
For example, to set a custom global variable name:
72+
73+
```javascript
74+
// webpack.conf.js
75+
module.exports = {
76+
module: {
77+
rules: [
78+
{
79+
loader: 'prebid.js/customize/webpackLoader',
80+
options: {
81+
globalVarName: 'myCustomGlobal'
82+
}
83+
},
84+
]
85+
}
86+
}
87+
```
88+
89+
5990
<a name="Install"></a>
6091

6192
## Install

customize/buildOptions.mjs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import path from 'path'
2+
import validate from 'schema-utils'
3+
4+
const boModule = path.resolve(import.meta.dirname, '../dist/src/buildOptions.mjs')
5+
6+
export function getBuildOptionsModule () {
7+
return boModule
8+
}
9+
10+
const schema = {
11+
type: 'object',
12+
properties: {
13+
globalVarName: {
14+
type: 'string',
15+
description: 'Prebid global variable name. Default is "pbjs"',
16+
},
17+
defineGlobal: {
18+
type: 'boolean',
19+
description: 'If false, do not set a global variable. Default is true.'
20+
},
21+
distUrlBase: {
22+
type: 'string',
23+
description: 'Base URL to use for dynamically loaded modules (e.g. debugging-standalone.js)'
24+
}
25+
}
26+
}
27+
28+
export function getBuildOptions (options = {}) {
29+
validate(schema, options, {
30+
name: 'Prebid build options',
31+
})
32+
const overrides = {}
33+
if (options.globalVarName != null) {
34+
overrides.pbGlobal = options.globalVarName
35+
}
36+
['defineGlobal', 'distUrlBase'].forEach((option) => {
37+
if (options[option] != null) {
38+
overrides[option] = options[option]
39+
}
40+
})
41+
return import(getBuildOptionsModule())
42+
.then(({ default: defaultOptions }) => {
43+
return Object.assign(
44+
{},
45+
defaultOptions,
46+
overrides
47+
)
48+
})
49+
}

customize/webpackLoader.mjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { getBuildOptions, getBuildOptionsModule } from './buildOptions.mjs'
2+
3+
export default async function (source) {
4+
if (this.resourcePath !== getBuildOptionsModule()) {
5+
return source
6+
}
7+
return `export default ${JSON.stringify(await getBuildOptions(this.getOptions()), null, 2)};`
8+
}

eslint.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ module.exports = [
6060
// do not lint build-related stuff
6161
'*.js',
6262
'metadata/**/*',
63+
'customize/**/*',
6364
...jsPattern('plugins'),
6465
...jsPattern('.github'),
6566
],
@@ -99,7 +100,7 @@ module.exports = [
99100
'comma-dangle': 'off',
100101
semi: 'off',
101102
'no-undef': 2,
102-
'no-console': 'error',
103+
'no-console': 'error',
103104
'space-before-function-paren': 'off',
104105
'import/extensions': ['error', 'ignorePackages'],
105106
'no-restricted-syntax': [

gulp.precompilation.js

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,21 @@ const {buildOptions} = require('./plugins/buildOptions.js');
1515
// do not generate more than one task for a given build config - so that `gulp.lastRun` can work properly
1616
const PRECOMP_TASKS = new Map();
1717

18-
function babelPrecomp({distUrlBase = null, disableFeatures = null, dev = false} = {}) {
18+
function getDefaults({distUrlBase = null, disableFeatures = null, dev = false}) {
1919
if (dev && distUrlBase == null) {
2020
distUrlBase = argv.distUrlBase || '/build/dev/'
2121
}
22+
return {
23+
disableFeatures: disableFeatures ?? helpers.getDisabledFeatures(),
24+
distUrlBase: distUrlBase ?? argv.distUrlBase,
25+
ES5: argv.ES5
26+
}
27+
}
28+
29+
function babelPrecomp({distUrlBase = null, disableFeatures = null, dev = false} = {}) {
2230
const key = `${distUrlBase}::${disableFeatures}`;
2331
if (!PRECOMP_TASKS.has(key)) {
24-
const babelConfig = require('./babelConfig.js')({
25-
disableFeatures: disableFeatures ?? helpers.getDisabledFeatures(),
26-
prebidDistUrlBase: distUrlBase ?? argv.distUrlBase,
27-
ES5: argv.ES5
28-
});
32+
const babelConfig = require('./babelConfig.js')(getDefaults({distUrlBase, disableFeatures, dev}));
2933
const precompile = function () {
3034
// `since: gulp.lastRun(task)` selects files that have been modified since the last time this gulp process ran `task`
3135
return gulp.src(helpers.getSourcePatterns(), {base: '.', since: gulp.lastRun(precompile)})
@@ -173,9 +177,23 @@ function generateGlobalDef(options) {
173177
}
174178
}
175179

180+
function generateBuildOptions(options = {}) {
181+
return function (done) {
182+
options = buildOptions(getDefaults(options));
183+
import('./customize/buildOptions.mjs').then(({getBuildOptionsModule}) => {
184+
const dest = getBuildOptionsModule();
185+
if (!fs.existsSync(path.dirname(dest))) {
186+
fs.mkdirSync(path.dirname(dest), {recursive: true});
187+
}
188+
fs.writeFile(dest, `export default ${JSON.stringify(options, null, 2)}`, done);
189+
})
190+
}
191+
192+
}
193+
176194
function precompile(options = {}) {
177195
return gulp.series([
178-
gulp.parallel(['ts', generateMetadataModules]),
196+
gulp.parallel(['ts', generateMetadataModules, generateBuildOptions(options)]),
179197
gulp.parallel([copyVerbatim, babelPrecomp(options)]),
180198
gulp.parallel([publicModules, generateCoreSummary, generateModuleSummary, generateGlobalDef(options)])
181199
]);

libraries/riseUtils/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import {BANNER, NATIVE, VIDEO} from '../../src/mediaTypes.js';
1414
import {config} from '../../src/config.js';
1515
import {ADAPTER_VERSION, DEFAULT_CURRENCY, DEFAULT_TTL, SUPPORTED_AD_TYPES} from './constants.js';
1616

17+
import {getGlobalVarName} from '../../src/buildOptions.js';
18+
1719
export const makeBaseSpec = (baseUrl, modes) => {
1820
return {
1921
version: ADAPTER_VERSION,
@@ -367,7 +369,7 @@ export function generateGeneralParams(generalObject, bidderRequest, adapterVersi
367369

368370
const generalParams = {
369371
wrapper_type: 'prebidjs',
370-
wrapper_vendor: '$$PREBID_GLOBAL$$',
372+
wrapper_vendor: getGlobalVarName(),
371373
wrapper_version: '$prebid.version$',
372374
adapter_version: adapVer,
373375
auction_start: bidderRequest.auctionStart,

modules/adagioRtdProvider.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import { _ADAGIO, getBestWindowForAdagio } from '../libraries/adagioUtils/adagio
3131
import { getGptSlotInfoForAdUnitCode } from '../libraries/gptUtils/gptUtils.js';
3232
import { getBoundingClientRect } from '../libraries/boundingClientRect/boundingClientRect.js';
3333

34+
import {getGlobalVarName} from '../src/buildOptions.js';
35+
3436
/**
3537
* @typedef {import('../modules/rtdModule/index.js').RtdSubmodule} RtdSubmodule
3638
* @typedef {import('../modules/rtdModule/index.js').adUnit} adUnit
@@ -446,7 +448,7 @@ function storeRequestInAdagioNS(bid, config) {
446448
bidderRequestsCount,
447449
ortb2: ortb2Data,
448450
ortb2Imp: ortb2ImpData,
449-
localPbjs: '$$PREBID_GLOBAL$$',
451+
localPbjs: getGlobalVarName(),
450452
localPbjsRef: getGlobal(),
451453
organizationId,
452454
site

modules/amxBidAdapter.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import { getStorageManager } from '../src/storageManager.js';
1717
import { fetch } from '../src/ajax.js';
1818
import { getGlobal } from '../src/prebidGlobal.js';
1919

20+
import {getGlobalVarName} from '../src/buildOptions.js';
21+
2022
const BIDDER_CODE = 'amx';
2123
const storage = getStorageManager({ bidderCode: BIDDER_CODE });
2224
const SIMPLE_TLD_TEST = /\.com?\.\w{2,4}$/;
@@ -343,7 +345,7 @@ export const spec = {
343345
trc: fbid.bidRequestsCount || 0,
344346
tm: isTrue(testMode),
345347
V: '$prebid.version$',
346-
vg: '$$PREBID_GLOBAL$$',
348+
vg: getGlobalVarName(),
347349
i: testMode && tagId != null ? tagId : getID(loc),
348350
l: {},
349351
f: 0.01,
@@ -551,7 +553,7 @@ export const spec = {
551553
U: getUIDSafe(),
552554
re: ref,
553555
V: '$prebid.version$',
554-
vg: '$$PREBID_GLOBAL$$',
556+
vg: getGlobalVarName(),
555557
};
556558
}
557559

modules/amxIdSystem.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import {getStorageManager} from '../src/storageManager.js';
1414
import {MODULE_TYPE_UID} from '../src/activities/modules.js';
1515
import {domainOverrideToRootDomain} from '../libraries/domainOverrideToRootDomain/index.js';
1616

17+
import {getGlobalVarName} from '../src/buildOptions.js';
18+
1719
const NAME = 'amxId';
1820
const GVL_ID = 737;
1921
const ID_KEY = NAME;
@@ -117,7 +119,7 @@ export const amxIdSubmodule = {
117119

118120
v: '$prebid.version$',
119121
av: version,
120-
vg: '$$PREBID_GLOBAL$$',
122+
vg: getGlobalVarName(),
121123
us_privacy: usp,
122124
am: getBidAdapterID(),
123125
gdpr: consent.gdprApplies ? 1 : 0,

modules/gamAdServerVideo.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import {DEFAULT_GAM_PARAMS, GAM_ENDPOINT, gdprParams} from '../libraries/gamUtil
2626
import { vastLocalCache } from '../src/videoCache.js';
2727
import { fetch } from '../src/ajax.js';
2828
import XMLUtil from '../libraries/xmlUtils/xmlUtils.js';
29+
30+
import {getGlobalVarName} from '../src/buildOptions.js';
2931
/**
3032
* @typedef {Object} DfpVideoParams
3133
*
@@ -74,7 +76,7 @@ export const VAST_TAG_URI_TAGNAME = 'VASTAdTagURI';
7476
*/
7577
export function buildGamVideoUrl(options) {
7678
if (!options.params && !options.url) {
77-
logError(`A params object or a url is required to use $$PREBID_GLOBAL$$.adServers.gam.buildVideoUrl`);
79+
logError(`A params object or a url is required to use ${getGlobalVarName()}.adServers.gam.buildVideoUrl`);
7880
return;
7981
}
8082

0 commit comments

Comments
 (0)