A
A
Andrey Dyrkov2016-02-17 19:39:37
gulp.js
Andrey Dyrkov, 2016-02-17 19:39:37

How to build with babel.js?

I'm building a project with gulp. Decided to use es2016 for work.
But ran into a problem.
Here's the challenge

var gulp = require('gulp'),

    rename      = require('gulp-rename'),
    jsMin       = require('gulp-uglify'),
    sass        = require('gulp-sass'),
    babel       = require('gulp-babel'),
    concat      = require('gulp-concat'),
    _if         = require('gulp-if'),
    _plumber    = require('gulp-plumber'),
    _debug      = require('gulp-debug'),
    sourcemaps  = require('gulp-sourcemaps'),
    cssMin      = require('gulp-minify-css'),
    cssPrefixer = require('gulp-autoprefixer'),
    notify      = require('gulp-notify'),

    rimraf      = require('rimraf'),
    browserSync = require('browser-sync'),
    reload      = browserSync.reload;



var path = {
    dist:{
        html:'./dist/',
        css:'./dist/css/',
        js:'./dist/js/',
        libs:'./dist/libs/',
        img:'./dist/img/',
        font:'./dist/font/',
    },

    src:{
        html:'./src/index.html',
        css:'./src/sass/index.scss',
        js:'./src/js/app.js',
        img:'./src/img/**/*.*',
        font:'./src/**/*.*',


        // stack sync libs (lib which have dependencies)
        sLibs:[
            './src/libs/modernizer/modernizr.js',
            './src/libs/underscore/underscore-min.js',
            './src/libs/jquery/jquery.min.js',
            './src/libs/backbone/backbone-min.js',
        ],

        //stack async libs (libs which we lazy load)
        aLibs:[
            //some path
        ],
    },

    watch:{
        html:'./src/*.html',
        css:'src/sass/**/*.*',
        js:'src/js/**/*.*',
    },

    clean:'./dist/'
};

// #javascript
gulp.task('js', function () {
    return gulp.src(path.src.js)
            .pipe(_plumber({
                errorHandler: notify.onError(function(err){
                    return {
                        title:'JAVASCRIPT',
                        message:err.message
                    };
                })
            }))
            .pipe(_if(isDev,sourcemaps.init()))
            .pipe(_if(!isDev,jsMin()))
            .pipe(babel({ presets: ['es2015'], plugins: [ "transform-es2015-modules-commonjs" ]}))
            .pipe(_debug({title:'babel'}))
            //.pipe(rename('app.js'))
            .pipe(_if(isDev,sourcemaps.write()))
            .pipe(gulp.dest(path.dist.js))
            .pipe(reload({stream: true}));
});

isDev is just a flag that determines how to build.
I have an app.js file, which in theory connects all other modules (files). I tried to write in es2016, but I'm not going to. The files are being translated from babel.js to es5 but the imports don't work. Error require is not defined in console.
Who did such assembly, share your experience :)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question