0

I have a gulp task to compile ES6 code into ES5 using babel and browserify. but when compiling it compiles all the other things except functions().

my gulp task code.

gulp.task('convert', function() {
        return browserify({ entries: './src/app.js', debug: true })
            .transform("babelify", { "presets": ["es2015"] })
            .bundle()
            .pipe(plumber({
                errorHandler: function (err) {
                    notify.onError({
                        title: "Gulp error in " + err.plugin,
                        message: err.toString()
                    })(err);
                }
            }))
            .pipe(source( 'app.js'))
            .pipe(buffer())
            .pipe(sourcemaps.init())
            .pipe(uglify())
            .pipe(sourcemaps.write('./maps'))
            .pipe(gulp.dest('./tmp'))
            .pipe(browserSync.reload({stream: true}));

});

my js file:

let title = 'this is my app 3';
let test = 'test';

document.getElementById("box").innerHTML = title;
document.getElementById("box2").innerHTML = test;

var tests = ()=>{
  alert('hello');
}

converted file :

!function e(r,t,n){function o(u,f){if(!t[u]){if(!r[u]){var c="function"==typeof require&&require;if(!f&&c)return c(u,!0);if(i)return i(u,!0);var s=new Error("Cannot find module '"+u+"'");throw s.code="MODULE_NOT_FOUND",s}var a=t[u]={exports:{}};r[u][0].call(a.exports,function(e){var t=r[u][1][e];return o(t||e)},a,a.exports,e,r,t,n)}return t[u].exports}for(var i="function"==typeof require&&require,u=0;u<n.length;u++)o(n[u]);return o}({1:[function(e,r,t){"use strict";document.getElementById("box").innerHTML="this is my app 3",document.getElementById("box2").innerHTML="test"},{}]},{},[1]);
//# sourceMappingURL=maps/app3.js.map

when I use the function it says tests is not defined. really need help:/

  • What is `myFunction`? There is nothing with that name in the code. – loganfsmyth Oct 10 '17 at 04:14
  • sorry I mean tests( ) in my js file. in my html I have used like this – Dilakshan Sooriyanathan Oct 10 '17 at 04:21
  • "when I use the function" Use it where? This JS file just creates a function and never uses it. Are you saying you're trying to use it as a global? – loganfsmyth Oct 10 '17 at 04:27
  • I have a HTML file using this JS file. it has a method tests(). first it was written in ES6 as mentioned in my js file above. wen I compile it exclude the functions. for example my **tests()** function. when I try to use the function is says tests is not defined because my converted js file doesn't have a **tests()** function.(converted mean ES6 to ES5 using gulp+babel+browserify) – Dilakshan Sooriyanathan Oct 10 '17 at 04:32
  • I have found a solution. https://stackoverflow.com/questions/35781579/basic-webpack-not-working-for-button-click-function-uncaught-reference-error – Dilakshan Sooriyanathan Oct 10 '17 at 09:37

0 Answers0