Skip to content

Commit

Permalink
Use memory instead of filesystem for bundle header & footer
Browse files Browse the repository at this point in the history
  • Loading branch information
dgirardi committed Jun 2, 2022
1 parent 0460510 commit 2bb042d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 58 deletions.
113 changes: 57 additions & 56 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var jsEscape = require('gulp-js-escape');
const path = require('path');
const execa = require('execa');
const {minify} = require('terser');
const tmp = require('tmp');
const Vinyl = require('vinyl');

var prebid = require('./package.json');
var port = 9999;
Expand Down Expand Up @@ -172,37 +172,48 @@ function nodeBundle(modules, dev = false) {
});
}

function generateHeaderAndFooter(dev, modules) {
function wrapWithHeaderAndFooter(dev, modules) {
// NOTE: gulp-header, gulp-footer & gulp-wrap do not play nice with source maps.
// gulp-concat does; for that reason we are generating temporary files to pass to it.
const placeholder = '$$PREBID_SOURCE$$';
const tpl = _.template(fs.readFileSync('./bundle-template.txt'))({
prebid,
modules: getModulesListToAddInBanner(modules),
enable: !argv.manualEnable
});
return (dev ? Promise.resolve(tpl) : minify(tpl, {format: {comments: true}}).then((res) => res.code))
.then((tpl) => {
// wrap source placeholder in a closure to make it an expression (so that it works with minify output)
const parts = tpl.replace(placeholder, `(function(){$$${placeholder}$$})()`).split(placeholder);
if (parts.length !== 2) {
throw new Error(`Cannot parse bundle template; it must contain exactly one instance of '${placeholder}'`);
}
const [header, footer] = parts;
const base = tmp.dirSync().name;
const [headerFn, footerFn] = [path.resolve(base, 'prebid-header.js'), path.resolve(base, 'prebid-footer.js')];
fs.writeFileSync(headerFn, header);
fs.writeFileSync(footerFn, footer);
return {
header: headerFn,
footer: footerFn,
cleanup: function () {
fs.unlinkSync(headerFn);
fs.unlinkSync(footerFn);
fs.rmdirSync(base);
}
};
// gulp-concat does; for that reason we are prepending and appending the source stream with "fake" header & footer files.
function memoryVinyl(name, contents) {
return new Vinyl({
cwd: '',
base: 'generated',
path: name,
contents: Buffer.from(contents, 'utf-8')
});
}
return function wrap(stream) {
const wrapped = through.obj();
const placeholder = '$$PREBID_SOURCE$$';
const tpl = _.template(fs.readFileSync('./bundle-template.txt'))({
prebid,
modules: getModulesListToAddInBanner(modules),
enable: !argv.manualEnable
});
(dev ? Promise.resolve(tpl) : minify(tpl, {format: {comments: true}}).then((res) => res.code))
.then((tpl) => {
// wrap source placeholder in an IIFE to make it an expression (so that it works with minify output)
const parts = tpl.replace(placeholder, `(function(){$$${placeholder}$$})()`).split(placeholder);
if (parts.length !== 2) {
throw new Error(`Cannot parse bundle template; it must contain exactly one instance of '${placeholder}'`);
}
const [header, footer] = parts;
wrapped.push(memoryVinyl('prebid-header.js', header));
stream.pipe(wrapped, {end: false});
stream.on('end', () => {
wrapped.push(memoryVinyl('prebid-footer.js', footer));
wrapped.push(null);
});
stream.on('error', (err) => {
wrapped.destroy(err);
})
})
.catch((err) => {
wrapped.destroy(err);
});
return wrapped;
}
}

function bundle(dev, moduleArr) {
Expand All @@ -221,35 +232,25 @@ function bundle(dev, moduleArr) {
});
}
}
const stream = through.obj();
generateHeaderAndFooter(dev, modules).then(({header, footer, cleanup}) => {
try {
var entries = [header, helpers.getBuiltPrebidCoreFile(dev)].concat(helpers.getBuiltModules(dev, modules)).concat([footer]);

var outputFileName = argv.bundleName ? argv.bundleName : 'prebid.js';
var entries = [helpers.getBuiltPrebidCoreFile(dev)].concat(helpers.getBuiltModules(dev, modules));

// change output filename if argument --tag given
if (argv.tag && argv.tag.length) {
outputFileName = outputFileName.replace(/\.js$/, `.${argv.tag}.js`);
}
var outputFileName = argv.bundleName ? argv.bundleName : 'prebid.js';

gutil.log('Concatenating files:\n', entries);
gutil.log('Appending ' + prebid.globalVarName + '.processQueue();');
gutil.log('Generating bundle:', outputFileName);

gulp.src(entries)
.pipe(gulpif(sm, sourcemaps.init({ loadMaps: true })))
.pipe(concat(outputFileName))
.pipe(gulpif(sm, sourcemaps.write('.')))
.pipe(stream)
.on('error', cleanup)
.on('end', cleanup);
} catch (e) {
cleanup();
throw e;
}
});
return stream;
// change output filename if argument --tag given
if (argv.tag && argv.tag.length) {
outputFileName = outputFileName.replace(/\.js$/, `.${argv.tag}.js`);
}

gutil.log('Concatenating files:\n', entries);
gutil.log('Appending ' + prebid.globalVarName + '.processQueue();');
gutil.log('Generating bundle:', outputFileName);

const wrap = wrapWithHeaderAndFooter(dev, modules);
return wrap(gulp.src(entries))
.pipe(gulpif(sm, sourcemaps.init({ loadMaps: true })))
.pipe(concat(outputFileName))
.pipe(gulpif(sm, sourcemaps.write('.')));
}

// Run the unit tests.
Expand Down
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@
"resolve-from": "^5.0.0",
"sinon": "^4.1.3",
"through2": "^4.0.2",
"tmp": "^0.2.1",
"url": "^0.11.0",
"url-parse": "^1.0.5",
"webdriverio": "^7.6.1",
Expand Down

0 comments on commit 2bb042d

Please sign in to comment.