我有一个使用 Object.assign 的外部 NPM 依赖项。我想使用 Babel,以确保与旧浏览器的兼容性,通过 helper 转换 Object.assign 或使用其他方法替换它,但它不起作用。
PS:我可以在代码顶部使用 polyfill,我想避免这种情况,因为它是一个库 (https://babeljs.io/docs/plugins/transform-object-assign/)
依赖是:https://github.com/krux/postscribe
通过 var postscribe = require('postscribe');
我的 .babelrc 配置文件:
{
"presets": [
["env", {
"targets": {
"browsers": ["Android 4", "iOS 7"]
}
}]
],
"plugins": ["transform-object-assign"]
}
Gulp 配置文件:
gulp.task('adtag', function () {
// set up the browserify instance on a task basis
var b = browserify({
entries: './src/adtag/main.js',
debug: true
}).transform(babelify)
return b.bundle()
.pipe(source('smaatoAdTag.min.js'))
.pipe(buffer())
.pipe(sourcemaps.init({
loadMaps: true
}))
// Add transformation tasks to the pipeline here.
.pipe(uglify())
.on('error', gutil.log)
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('./dist/adtag/'));
});
如果 Object.assign 在依赖项中而不在主代码中,是否有任何方法可以替代它?
最佳答案
查看 babelify 的文档
你可以使用:
browserify().transform("babelify", {
global: true,
ignore: /\/node_modules\/(?!postscribe\/)/
});
关于javascript - Object.assign 带有外部 npm dep 的 Babel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45167375/