I use gulp and babel to translate ES2015 code to ES5. We use template literals and a tag function to tear out any leading spaces.
It seems that during forwarding, that template literals are added to the global scope with the same variable names. This causes Javascript templates to be overwritten in certain views.
Gulp Challenge
// Transpile ES2015 JS to ES5 JS gulp.task('babel', function () { return gulp.src('precompiled/Views/**/*.js') // Gets all files ending with .js in precompiled/Views and children dirs .pipe(babel({ presets: ['es2015'] })) .pipe(header('/* THIS IS A GENERATED FILE. MAKE CHANGES TO THE SOURCE FILE. CHANGES HERE ARE OVERWRITTEN. */\n\n')) .pipe(gulp.dest('./Views')) // Outputs it in the Views folder .pipe(browserSync.reload({ // Reloading with Browser Sync stream: true })); });
Example
const singleLineString = function (strings) { const values = Array.prototype.slice.call(arguments, 1); // Interweave the strings with the // substitution vars first. let output = ""; for (var i = 0; i < values.length; i++) { output += strings[i] + values[i]; } output += strings[values.length]; // Split on newlines. const lines = output.split(/(?:\r\n|\n|\r)/); // Rip out the leading whitespace. return lines.map(function (line) { return line.replace(/^\s+/gm, ''); }).join(' ').trim(); }; const firstFilesJSFunction = (name) => { return singleLineString `My name is ${name} and I am the best.`; }; const secondJSFilesFunction = (candy) => { return singleLineString `I love ${candy} and ice cream.`; };
My Javascript uses the module design template, so the scope is not becoming a problem. Although the sample code is in the module, the transferred tagged object is still placed in the global scope. Here's what the passed tagged object looks like:
First file
var _templateObject = _taggedTemplateLiteral(["My name is ","and I am the best."])
Second file
var _templateObject = _taggedTemplateLiteral(["I love ","and ice cream."])
Since the variables are placed in the global scope, the first variable will be replaced by the second variable.
Is there a way to cover these template objects? Any help is much appreciated.


Thanks!
source share