Skip to content

Commit 4c8b821

Browse files
abdullahcicekliAbdullah Cicekli
andauthored
Build Config: Add ES5 flag support (prebid#13436)
* Enhance Babel configuration to support ES5 output - Added support for an ES5 mode in babelConfig.js, allowing for transpilation to ES5 syntax when the `--ES5` flag is used. - Updated gulpfile.js and webpack.conf.js to pass the ES5 flag and adjust module handling accordingly. - Enhanced README.md to document the new ES5 output feature and its usage. This change improves compatibility with older browsers and environments requiring ES5 syntax. * Update Babel configuration to conditionally use 'usage' for built-ins in ES5 mode --------- Co-authored-by: Abdullah Cicekli <abdullah.cicekli@sahibinden.com>
1 parent d872d6e commit 4c8b821

File tree

4 files changed

+51
-11
lines changed

4 files changed

+51
-11
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,21 @@ gulp build-bundle-dev --modules=bidderA,module1,...
256256

257257
The results will be in build/dev/prebid.js.
258258

259+
## ES5 Output Support
260+
261+
For compatibility with older parsers or environments that require ES5 syntax, you can generate ES5-compatible output using the `--ES5` flag:
262+
263+
```bash
264+
gulp build-bundle-dev --modules=bidderA,module1,... --ES5
265+
```
266+
267+
This will:
268+
- Transpile all code to ES5 syntax using CommonJS modules
269+
- Target browsers: IE11+, Chrome 50+, Firefox 50+, Safari 10+
270+
- Ensure compatibility with older JavaScript parsers
271+
272+
**Note:** Without the `--ES5` flag, the build will use modern ES6+ syntax by default for better performance and smaller bundle sizes.
273+
259274
## Test locally
260275

261276
To lint the code:

babelConfig.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
let path = require('path');
3+
var argv = require('yargs').argv;
34

45
function useLocal(module) {
56
return require.resolve(module, {
@@ -10,15 +11,23 @@ function useLocal(module) {
1011
}
1112

1213
module.exports = function (options = {}) {
14+
15+
const isES5Mode = argv.ES5 || options.ES5;
16+
1317
return {
1418
'presets': [
1519
[
1620
useLocal('@babel/preset-env'),
1721
{
18-
'useBuiltIns': 'entry',
22+
'useBuiltIns': isES5Mode ? 'usage' : 'entry',
1923
'corejs': '3.42.0',
20-
// a lot of tests use sinon.stub & others that stopped working on ES6 modules with webpack 5
21-
'modules': options.test ? 'commonjs' : 'auto',
24+
// Use ES5 mode if requested, otherwise use original logic
25+
'modules': isES5Mode ? 'commonjs' : (options.test ? 'commonjs' : 'auto'),
26+
...(isES5Mode && {
27+
'targets': {
28+
'browsers': ['ie >= 11', 'chrome >= 50', 'firefox >= 50', 'safari >= 10']
29+
}
30+
})
2231
}
2332
]
2433
],

gulpfile.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,12 @@ function makeDevpackPkg(config = webpackConfig) {
165165
mode: 'development'
166166
})
167167

168-
const babelConfig = require('./babelConfig.js')({disableFeatures: helpers.getDisabledFeatures(), prebidDistUrlBase: argv.distUrlBase || '/build/dev/'});
169-
168+
const babelConfig = require('./babelConfig.js')({
169+
disableFeatures: helpers.getDisabledFeatures(),
170+
prebidDistUrlBase: argv.distUrlBase || '/build/dev/',
171+
ES5: argv.ES5 // Pass ES5 flag to babel config
172+
});
173+
170174
// update babel config to set local dist url
171175
cloned.module.rules
172176
.flatMap((rule) => rule.use)

webpack.conf.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ const fs = require('fs');
1010
const babelConfig = require('./babelConfig.js')({disableFeatures: helpers.getDisabledFeatures(), prebidDistUrlBase: argv.distUrlBase});
1111
const {WebpackManifestPlugin} = require('webpack-manifest-plugin')
1212

13+
// Check if ES5 mode is requested
14+
const isES5Mode = argv.ES5;
15+
1316
var plugins = [
1417
new webpack.EnvironmentPlugin({'LiveConnectMode': null}),
1518
new WebpackManifestPlugin({
@@ -41,6 +44,7 @@ if (argv.analyze) {
4144
module.exports = {
4245
mode: 'production',
4346
devtool: 'source-map',
47+
target: isES5Mode ? ['web', 'es5'] : 'web',
4448
cache: {
4549
type: 'filesystem',
4650
cacheDirectory: path.resolve(__dirname, '.cache/webpack')
@@ -79,7 +83,7 @@ module.exports = {
7983
rules: [
8084
{
8185
test: /\.js$/,
82-
exclude: path.resolve('./node_modules'), // required to prevent loader from choking non-Prebid.js node_modules
86+
exclude: isES5Mode ? [] : path.resolve('./node_modules'), // In ES5 mode, process all files
8387
use: [
8488
{
8589
loader: 'babel-loader',
@@ -91,16 +95,16 @@ module.exports = {
9195
}
9296
]
9397
},
94-
{ // This makes sure babel-loader is ran on our intended Prebid.js modules that happen to be in node_modules
95-
test: /\.js$/,
98+
// Only apply the second rule if not in ES5 mode
99+
...(isES5Mode ? [] : [{
96100
include: helpers.getArgModules().map(module => new RegExp('node_modules/' + module + '/')),
97101
use: [
98102
{
99103
loader: 'babel-loader',
100104
options: Object.assign({cacheDirectory: cacheDir, cacheCompression: false}, babelConfig)
101105
}
102106
],
103-
}
107+
}])
104108
]
105109
},
106110
optimization: {
@@ -110,8 +114,16 @@ module.exports = {
110114
new TerserPlugin({
111115
extractComments: false, // do not generate unhelpful LICENSE comment
112116
terserOptions: {
113-
module: true, // do not prepend every module with 'use strict'; allow mangling of top-level locals
114-
}
117+
module: isES5Mode ? false : true, // Force ES5 output if ES5 mode is enabled
118+
...(isES5Mode && {
119+
ecma: 5, // Target ES5
120+
compress: {
121+
ecma: 5 // Ensure compression targets ES5
122+
},
123+
mangle: {
124+
safari10: true // Ensure compatibility with older browsers
125+
}
126+
}) }
115127
})
116128
],
117129
splitChunks: {

0 commit comments

Comments
 (0)