Javascript 6to5 (now Babel) export module usage

2020-02-23 07:59发布

I'm still a beginner, i try to to export and import one class into a main file, the other class in the others class file and use them.

And then gulp ES5 code with 6to5 (now Babel).

// file a.js
import B from 'b.js';

class A {
  constructor() {
    B.methodB();
  }
}

export default A;

// file b.js
class B {
  methodB() {
    console.log('hi from b');
  }
}

export default B;

// file main.js
import A from 'a.js';
new A();

My gulpfile:

var gulp = require('gulp');
var to5 = require('gulp-6to5');

gulp.task('default', function () {
  return gulp.src('main.js')
    .pipe(to5())
    .pipe(gulp.dest('dist'));
 });

And this is my dist/main.js file:

"use strict";

var _interopRequire = function (obj) {
  return obj && (obj["default"] || obj);
};

var A = _interopRequire(require("a.js"));

new A();

The error in console: ReferenceError: require is not defined

Which of course does not work ... what am I doing wrong or what lack I yet? I do not get it exactly.

2条回答
Lonely孤独者°
2楼-- · 2020-02-23 08:31

I was having the exact same problem before myself... As Qantas mentioned in the comments, Babel (formerly 6to5) will convert syntax, but it won't do module loading or polyfills.

I've found the easiest workflow is using browserify with gulp. This takes care of transpiling, adding polyfills, bundling, minification, and source map generation in one hit. This question has a pretty nice example: Gulp + browserify + 6to5 + source maps.

This version adds minification and polyfills. An example for your case would look like this:

let gulp = require('gulp');
let browserify = require('browserify');
let babelify = require('babelify');
let util = require('gulp-util');
let buffer = require('vinyl-buffer');
let source = require('vinyl-source-stream');
let uglify = require('gulp-uglify');
let sourcemaps = require('gulp-sourcemaps');

gulp.task('build:demo', () => {
    browserify('./demo/app.js', { debug: true })
    .add(require.resolve('babel-polyfill/dist/polyfill.min.js'))
    .transform(babelify.configure({ presets: ['es2015', 'es2016', 'stage-0', 'stage-3'] }))
    .bundle()
    .on('error', util.log.bind(util, 'Browserify Error'))
    .pipe(source('demo.js'))
    .pipe(buffer())
    .pipe(sourcemaps.init({ loadMaps: true }))
    .pipe(uglify({ mangle: false }))
    .pipe(sourcemaps.write('./'))
    .pipe(gulp.dest('./demo'));
});

gulp.task('default', ['build:demo']);

It's important that uglify has mangle set to false; it really doesn't seem to play nice with the transformed code.

If you don't have all the dependencies installed, you may want to create a package.json file, and ensure that following packages are defined in the dependencies object:

"devDependencies": {
    "babel-polyfill": "^6.13.0",
    "babel-preset-es2015": "^6.13.0",
    "babel-preset-es2016": "^6.11.0",
    "babel-preset-stage-0": "^6.5.0",
    "babel-preset-stage-3": "^6.11.0",
    "babelify": "^7.3.0",
    "browserify": "^13.1.0",
    "gulp": "^3.9.0",
    "gulp-sourcemaps": "^1.6.0",
    "gulp-uglify": "^2.0.0",
    "gulp-util": "^3.0.0",
    "vinyl-buffer": "^1.0.0",
    "vinyl-source-stream": "^1.1.0"
}

Most of these won't work if installed with -g, so consider yourself warned :P

Then, just run npm install to install all the dependencies, and gulp to run the default task and transpile all of your code.

Your other files look good, you have the right idea with importing at the beginning of each file and exporting your defaults :) If you want some examples of babelified ES6 in the wild, I have a couple of projects on GitHub that might help.

查看更多
Fickle 薄情
3楼-- · 2020-02-23 08:43

It's seems you need to import the requirejs fond in your HTML like that:

<script src="//cdnjs.cloudflare.com/ajax/libs/require.js/2.1.15/require.min.js"></script>

And your files need to be something like that:

// file a.js
import B from './b';

class A {
  constructor() {
    B.methodB = function() {

    };
  }
}

export default A;


// file b.js
class B {
  methodB() {
    console.log('hi from b');
  }
}

export default B;

// main.js
import A from './a';

new A();

Note that you need to put the module's directory ./a and ./b on the import.

And your gulpfile need to be:

var gulp = require('gulp');
var to5 = require('gulp-6to5');

gulp.task('default', function () {
  return gulp.src('src/*.js')
    .pipe(to5())
    .pipe(gulp.dest('dist'));
 });

Note that you need to transform all of your file with src/*.js on the gulp.src

查看更多
登录 后发表回答