设想:
我有两个设置完全不同的项目:
- 常规网站、遗留代码和简单的 gulp 设置
-
小宠物项目。在 ES6 类的帮助下编写的 JS 滑块插件(用 babel 转译)。 JS 吞咽任务:
gulp.task('js', function() {
return gulp.src('src/scripts/*.js')
.pipe($.plumber())
.pipe(through2.obj(function (file, enc, next) {
browserify(file.path, { debug: true })
.transform(require('babelify'))
.transform(require('debowerify'))
.bundle(function (err, res) {
if (err) { return next(err); }
file.contents = res;
next(null, file);
});
}))
.on('error', function (error) {
console.log(error.stack);
this.emit('end')
})
.pipe( $.rename('alder.js'))
.pipe( gulp.dest('dist/scripts/'));
});
我想实现什么目标?
我希望能够在这个常规网站中使用 browserify/babelify 输出的文件(无需设置整个 browserify/babelify 的东西)。
问题
当然 browserify / babelify 做了一些神奇的事情,最后将变量包装到函数作用域中,隐藏了这个变量/构造函数。所以问题是导出可在其他项目中使用的全局变量/构造函数的正确方法是什么?此时,我想到的唯一一件事是将我的函数附加到窗口对象,例如:
class Alder { // constructor and then methods }
export default Alder;
window['Alder'] = Alder
还有其他想法吗?
Set the standalone
option:
When opts.standalone
是一个非空字符串,使用该名称和一个独立模块创建umd https://github.com/forbeslindesay/umd包装纸。您可以使用独立全局导出中的命名空间.
在字符串名称中作为分隔符,例如'A.B.C'
。全球出口将已消毒并装在骆驼箱中 https://github.com/ForbesLindesay/umd#name-casing-and-characters.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)