Javascript gulp babel,未定义出口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33294705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:54:45  来源:igfitidea点击:

gulp babel, exports is not defined

javascriptgulpecmascript-6babeljsecmascript-5

提问by TheWebs

Consider the following example code (and maybe I am doing it wrong?)

考虑以下示例代码(也许我做错了?)

 var FlareCurrency = {

 };

export {FlareCurrency};

I have the following task:

我有以下任务:

gulp.task("compile:add-new-currency-minified", function(){
  return gulp.src('src/add-new-currency/**/*.js')
             .pipe(babel())
             .pipe(concat('Flare-AddNewCurrency.js'))
             .pipe(uglify({"preserveComments": "all"}))
             .pipe(gulp.dest('dist/minified/'));
});

When I run this I get the following:

当我运行它时,我得到以下信息:

"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var FlareCurrency={};exports.FlareCurrency=FlareCurrency;

For the fun of it, I wanted to run it in the console, yes I know it does nothing but I didn't expect to see this:

为了它的乐趣,我想在控制台中运行它,是的,我知道它什么也不做,但我没想到会看到这个:

Uncaught ReferenceError: exports is not defined(…)

The non minified version:

非缩小版:

"use strict";

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

exports.FlareCurrency = FlareCurrency;

throws the same error. Ideas?

抛出同样的错误。想法?

回答by Tiago Romero Garcia

That is not actually a babel issue, you are just trying to run CommonJS code (transpiled from ES6 export) in the browser without preparation. CommonJS doesn't run on the browser, you need to use a tool to package it for the browser, such as Webpackor Browserify.

这实际上不是 babel 问题,您只是想export在没有准备的情况下在浏览器中运行 CommonJS 代码(从 ES6 转译)。CommonJS 不是在浏览器上运行的,你需要使用一个工具为浏览器打包,比如Webpack或者Browserify

Just by coincidence this week I created a small project on Github that shows a setup of Gulp + ES6 code (using export) + Babel + Webpack: gulp-es6-webpack-example.

恰巧这周我在 Github 上创建了一个小项目,展示了 Gulp + ES6 代码(使用export)+ Babel + Webpack 的设置:gulp-es6-webpack-example

In my example you can load JS code on the browser either synchronously (pre-loaded) or asynchronously (lazy-loaded).

在我的示例中,您可以同步(预加载)或异步(延迟加载)在浏览器上加载 JS 代码。