Skip to content

Commit

Permalink
Allow bundling from node.js or with new gulp task bundle-to-stdout (p…
Browse files Browse the repository at this point in the history
…rebid#1570)

* allow bundle task to be run programmatically from node

* add bundle-to-stdout task to gulpFile.js

* fixed bug with double-prepending __dirname in module bundling
  • Loading branch information
snapwich authored and dluxemburg committed Jul 17, 2018
1 parent 05aed4c commit db18c40
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
10 changes: 5 additions & 5 deletions gulpHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ module.exports = {
externalModules = externalModules || [];
var internalModules;
try {
internalModules = fs.readdirSync(MODULE_PATH)
var absoluteModulePath = path.join(__dirname, MODULE_PATH);
internalModules = fs.readdirSync(absoluteModulePath)
.filter(file => !(/(^|\/)\.[^\/\.]/g).test(file))
.reduce((memo, file) => {
var moduleName = file.split(new RegExp('[.\\' + path.sep + ']'))[0];
var filePath = path.join(MODULE_PATH, file);
var modulePath = path.join(__dirname, filePath)
if (fs.lstatSync(filePath).isDirectory()) {
modulePath = path.join(__dirname, filePath, "index.js")
var modulePath = path.join(absoluteModulePath, file);
if (fs.lstatSync(modulePath).isDirectory()) {
modulePath = path.join(modulePath, "index.js")
}
memo[modulePath] = moduleName;
return memo;
Expand Down
37 changes: 30 additions & 7 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var optimizejs = require('gulp-optimize-js');
var eslint = require('gulp-eslint');
var gulpif = require('gulp-if');
var sourcemaps = require('gulp-sourcemaps');
var through = require('through2');
var fs = require('fs');

var prebid = require('./package.json');
Expand All @@ -52,8 +53,25 @@ gulp.task('clean', function () {
.pipe(clean());
});

function bundle(dev) {
var modules = helpers.getArgModules(),
function gulpBundle(dev) {
return bundle(dev).pipe(gulp.dest('build/' + (dev ? 'dev' : 'dist')));
}

function nodeBundle(modules) {
return new Promise((resolve, reject) => {
bundle(false, modules)
.on('error', (err) => {
reject(err);
})
.pipe(through.obj(function(file, enc, done) {
resolve(file.contents.toString(enc));
done();
}));
});
}

function bundle(dev, moduleArr) {
var modules = moduleArr || helpers.getArgModules(),
allModules = helpers.getModuleNames(modules);

if(modules.length === 0) {
Expand Down Expand Up @@ -82,8 +100,7 @@ function bundle(dev) {
global: prebid.globalVarName
}
)))
.pipe(gulpif(dev, sourcemaps.write('.')))
.pipe(gulp.dest('build/' + (dev ? 'dev' : 'dist')));
.pipe(gulpif(dev, sourcemaps.write('.')));
}

// Workaround for incompatibility between Karma & gulp callbacks.
Expand All @@ -98,9 +115,13 @@ function newKarmaCallback(done) {
}
}

gulp.task('build-bundle-dev', ['devpack'], bundle.bind(null, true));
gulp.task('build-bundle-prod', ['webpack'], bundle.bind(null, false));
gulp.task('bundle', bundle.bind(null, false)); // used for just concatenating pre-built files with no build step
gulp.task('build-bundle-dev', ['devpack'], gulpBundle.bind(null, true));
gulp.task('build-bundle-prod', ['webpack'], gulpBundle.bind(null, false));
gulp.task('bundle', gulpBundle.bind(null, false)); // used for just concatenating pre-built files with no build step

gulp.task('bundle-to-stdout', function() {
nodeBundle().then(file => console.log(file));
});

gulp.task('devpack', ['clean'], function () {
var cloned = _.cloneDeep(webpackConfig);
Expand Down Expand Up @@ -275,3 +296,5 @@ gulp.task('build-postbid', function() {
.pipe(uglify())
.pipe(gulp.dest('build/dist'));
});

module.exports = nodeBundle;

0 comments on commit db18c40

Please sign in to comment.