javascript - 使用 Gulp 导入 ES6 模块

标签 javascript gulp gulp-babel

我正在尝试将我的 ES6 模块导入到一个文件中,并运行 Gulp 来连接和缩小该文件。我遇到了 ReferenceError: require is not defined at all.js(transpiled) line no 3。 我已经使用 gulp-babel 转译了代码。

我的 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(转译):

'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;

最佳答案

您需要像 Webpack 这样的 bundler 或 Browserify为了使用 ES6 导入。 Babel 只能将 ES6 代码编译为 ES5(原生 JS)。

Webpack 和 Browserify 都为 gulp 制作了配方:

希望这对您有所帮助。

关于javascript - 使用 Gulp 导入 ES6 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47632435/

相关文章:

javascript - 如何使用 jquery clone 在点击按钮上隐藏和显示删除按钮

javascript - native JavaScript 语法中的千位分隔符

javascript - 无法让 Gulp 查看两个文件

webpack - 无法使用 Babel loader 和 Webpack 解析 Babel 插件

javascript - 防止 gulp uglify 剥离 es6

javascript - 有条件地在另一个 promise 中调用 promise

javascript - 如何合并两个json对象?

javascript - gulp-uglify 缩小不应该的脚本

css - 图标不显示语义 UI