别忘了,导出没有定义
gulp babel, exports is not defined
考虑以下示例代码(也许我做错了?)
var FlareCurrency = {
};
export {FlareCurrency};
我有以下任务:
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/'));
});
当我运行这个命令时,我得到如下结果:
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var FlareCurrency={};exports.FlareCurrency=FlareCurrency;
为了好玩,我想在控制台中运行它,是的,我知道它什么也不做,但我没想到会看到这个:
Uncaught ReferenceError: exports is not defined(…)
非压缩版本:
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var FlareCurrency = {};
exports.FlareCurrency = FlareCurrency;
抛出相同的错误。想法?
这实际上不是一个通塔问题,您只是试图在没有准备的情况下在浏览器中运行CommonJS代码(从ES6 export
编译)。CommonJS不能在浏览器上运行,你需要使用一个工具来为浏览器打包它,比如Webpack或Browserify。
本周我在Github上创建了一个小项目,展示了Gulp + ES6代码(使用export
) + Babel + Webpack的设置:Gulp - ES6 - Webpack -example。
在我的例子中,你可以同步(预加载)或异步(惰性加载)在浏览器上加载JS代码。
我修复它只是设置se6
为设置gulp-typescript
包:
import gulp from 'gulp';
import ts from 'gulp-typescript';
import uglify from 'gulp-uglify';
import browserSync from 'browser-sync';
export const scripts = () => {
return gulp.src(['app/scripts/*', '!app/scripts/modules/'])
.pipe(ts({
noImplicitAny: true,
target: "es6" // <<< THIS
}))
.pipe(uglify())
.pipe(gulp.dest('dist/scripts/'))
.pipe(browserSync.stream())
}
此外,你必须设置type="module"
连接你的js文件在html。
<script type="module" src="scripts/index.js"></script>
相关文章:
- 对象获胜'如果qml中的其他地方定义了数字动画属性,则t设置动画
- jQuery结合了2个绑定函数(类型错误:a是未定义的)
- 在Ionic中获得了未定义的输入值
- 有了字段的名称,我如何用空白数据初始化对象的未定义字段
- 为什么我得到了一个“;未定义的“;使用“”从Find()返回值;这个“;论点
- .val()给了我未定义的值
- JS方法显示了未定义的简单示例
- grunt contrib jshint-error在定义之前就已经使用过了
- jsLint仍在警告“;X是在定义之前使用的"即使设置了undef
- 怎么了?通过字符串定义 JavaScript 命名空间
- 服务变量分配了数据,但在访问时始终未定义
- 未捕获的类型错误:未定义不是函数,在中为循环创建了对象
- ES6:尽管使用了bind(),但这是未定义的
- Alt flux action:_this.actions未定义,即使调用了this.generateActions
- 为什么我得到“减速器 [..]在初始化期间返回未定义“,尽管为 createStore() 提供了 initialSta
- 如何使用Chrome'查看窗口对象中定义了哪些变量;的开发工具
- 检查是否定义了数组中的值
- template.find().val给了我未定义的值
- 别忘了,导出没有定义
- 为什么是宽度100%的html代码全栏不工作,当我写.忘了那个吧,连复制粘贴都不起作用.为什么