2017-12-04 9 views
3

ES6 모듈을 파일로 가져오고 Gulp를 실행하여 연결하고 파일을 축소하려고합니다. ReferenceError : require는 all.js (transpiled) 행 번호 3에 정의되어 있지 않습니다. gulp-babel을 사용하여 코드를 추출했습니다.Gulp가있는 ES6 가져 오기 모듈

내 JS 파일은 다음과 같습니다

cart.js :

class Cart{ 
    constructor(){ 
    this.cart = []; 
    this.items = items = [{ 
     id: 1, 
     name: 'Dove Soap', 
     price: 39.99 
    },{ 
     id: 2, 
     name: 'Axe Deo', 
     price: 99.99 
    }]; 
    } 


    getItems(){ 
    return this.items; 
    } 


} 

export {Cart}; 

app.js :

import {Cart} from 'cart.js'; 

let cart = new Cart(); 

console.log(cart.getItems()); 

gulpfile.js :

"use strict"; 

let gulp = require('gulp'); 
let jshint = require('gulp-jshint'); 
let concat = require('gulp-concat'); 
let uglify = require('gulp-uglify'); 
let rename = require('gulp-rename'); 
let babel = require('gulp-babel'); 


// Lint Task 
gulp.task('lint', function() { 
    return gulp.src('js/*.js') 
     .pipe(jshint()) 
     .pipe(jshint.reporter('default')); 
}); 


// Concatenate & Minify JS 
gulp.task('scripts', function() { 
    return gulp.src('js/*.js') 
     .pipe(babel({ 
      presets: ['env'] 
     })) 
     .pipe(concat('all.js')) 
     .pipe(gulp.dest('dist')) 
     .pipe(rename('all.min.js')) 
     .pipe(uglify().on('error', function(e){ 
      console.dir(e); 
     })) 
     .pipe(gulp.dest('dist/js')); 
}); 

// Default Task 
gulp.task('default', ['lint','scripts']); 

app.js (transpiled) : 나는 스크린 캐스트이 최대

링크를 삭제 코드 학교에서 카를로스 수자에 의해 스크린 캐스트를 통해 온

'use strict'; 

var _cart = require('cart.js'); //Uncaught ReferenceError: require is not defined 

var cart = new _cart.Cart(); 

console.log(cart.getItems()); 
'use strict'; 

Object.defineProperty(exports, "__esModule", { 
    value: true 
}); 

var _createClass = function() { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); 

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } 

var Cart = function() { 
    function Cart() { 
    _classCallCheck(this, Cart); 

    this.cart = []; 
    this.items = items = [{ 
     id: 1, 
     name: 'Dove Soap', 
     price: 39.99 
    }, { 
     id: 2, 
     name: 'Axe Deo', 
     price: 99.99 
    }]; 
    } 

    _createClass(Cart, [{ 
    key: 'getItems', 
    value: function getItems() { 
     return this.items; 
    } 
    }]); 

    return Cart; 
}();exports.Cart = Cart; 
+0

브라우저에서 app.js를 실행하려고하는데, 올바른 경우 ('something') 작동하지 않습니다. 브라우저를 사용해야합니까? – Neil

+0

babel 다음에 webpack-stream을 실행해야합니다. 설치하는 것은 그리 재미있는 일이 아니며 예제를 만들려고 포기했습니다. gulp-webpack은 또 다른 패키지이지만 더 이상 유지 관리되지 않는다고 생각합니다. – MikaS

+0

FYI,'browserify'는 제대로 감싸 인 후에 만'gulp'와 함께 작동합니다. 기본 사용 예는 [recipe] (https://github.com/gulpjs/gulp/blob/master/docs/recipes/browserify-transforms.md)입니다. –

답변