ES6模块不兼容babel 6和gulp

时间:2016-03-15 19:40:41

标签: javascript gulp ecmascript-6 browserify

我试图了解如何使用babel 6和gulp模块。我创建了一些测试文件

档案a.js

export function test () {
    console.log(11111);
}

文件app.js

import {test} from 'a.js';
console.log(test());

file gulpfile.babel.js

import gulp from 'gulp';
import babel from 'gulp-babel';
import fs from 'fs';
import browserify from 'browserify'
import babelify from 'babelify';
import buffer from 'vinyl-buffer';
import source from 'vinyl-source-stream';
import uglify from 'gulp-uglify';


gulp.task('default', () => {
    var bundler = browserify('src/app.js');
    bundler.transform(babelify);

    bundler.bundle()
        .on('error', function (err) { console.error(err); })
        .pipe(source('app.js'))
        .pipe(buffer())
        .pipe(uglify()) 
        .pipe(gulp.dest('dist'));
});

的package.json

{
  "name": "babel",
  "version": "1.0.0",
  "description": "",
  "main": "index.html",
  "dependencies": {
    "babel": "^6.5.2",
    "babel-preset-es2015": "^6.6.0",
    "babelify": "^7.2.0",
    "browserify": "^13.0.0",
    "fs": "^0.0.2",
    "gulp": "^3.9.1",
    "gulp-uglify": "^1.5.3",
    "gulp-babel": "^6.1.2",
    "vinyl-buffer": "^1.0.0",
    "vinyl-source-stream": "^1.1.0"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

.babelrc

{
  "presets": ["es2015"]
}

所以,似乎一切都是正确的,但是当我运行gulp命令时,我看到了这个错误 - [错误:无法找到模块' a.js'来自' E:\ JS \ babel \ src'] 我无法理解为什么会发生这种错误以及如何解决它

这里是github上的回购 - https://github.com/zheleznov/ecma-modules.git

1 个答案:

答案 0 :(得分:0)

如果您引用某个文件,则需要在模块文件中提供路径

... from './a.js';

否则,Node将在node_modules/中查找名为“a.js”的模块。

这与ES6 btw无关。这就是Node / browserify的工作原理。请参阅Node documentation

相关问题