Skip to content

Instantly share code, notes, and snippets.

@MovingGifts
Created July 3, 2017 15:21
Show Gist options
  • Save MovingGifts/69e6641d5ab065d6dbcc5fea6c72e85c to your computer and use it in GitHub Desktop.
Save MovingGifts/69e6641d5ab065d6dbcc5fea6c72e85c to your computer and use it in GitHub Desktop.
v2
var gulp = require('gulp');
var argv = require('yargs').argv; // This was needed
var runSequence = require('run-sequence'); // This was needed
var config = {
aliasify: {
src: './.temp/' + argv.platform, // Is this the correct directory for ReactXP
dest: './dist/js/', // Is this the correct directory for ReactXP
aliases: (argv.platform === 'web') ?
// Web Aliases
{
'AppAssets': './src/modules/AppAssetsWeb'
} :
// Native Aliases
{
'AppAssets': './src/modules/AppAssetsNative'
}
}
}
function aliasify(aliases) {
var reqPattern = new RegExp(/require\(['"]([^'"]+)['"]\)/g);
// For all files in the stream, apply the replacement.
return eventStream.map(function(file, done) {
if (!file.isNull()) {
var fileContent = file.contents.toString();
if (reqPattern.test(fileContent)) {
file.contents = new Buffer(fileContent.replace(reqPattern, function(req, oldPath) {
if (!aliases[oldPath]) {
return req;
}
return "require('" + aliases[oldPath] + "')";
}));
}
}
done(null, file);
});
}
gulp.task('apply-aliases', function() {
return gulp.src(path.join(config.aliasify.src, '**/*.js')) // What should the "path" variable be here?
.pipe(aliasify(config.aliasify.aliases))
.pipe(gulp.dest(config.aliasify.dest))
.on('error', handleError);
});
// Here's our full build task pipeline. I haven't provided the task
// definitions for all of these stages, but you can see where the
// 'apply-aliases' task fits into the pipeline.
gulp.task('run', function(runSequence) {
runSequence('apply-aliases');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment