Created
July 1, 2017 22:37
-
-
Save MovingGifts/26247429cb79ee903a80c3897740f33e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var RX = require('reactxp'); // I tried import, but it threw an error when on `gulp run`, so var seemed to work | |
var gulp = require('gulp'); | |
var config: { // I get "SyntaxError: Unexpected token :" on `gulp run` | |
aliasify: { | |
src: './.temp/' + RX.Platform.getType(), | |
dest: getBuildPath() + 'js/', | |
aliases: (RX.Platform.getType() === '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')) | |
.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(callback) { | |
runSequence('clean', 'build', 'apply-aliases', 'watch', 'lint'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment