javascript - Object.assign 带有外部 npm dep 的 Babel

标签 javascript gulp babeljs

我有一个使用 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/

相关文章:

javascript - Gulp BrowserSync 不刷新

javascript - 浅拷贝对象遗漏了 ES6/ES7 中的一个或多个属性?

javascript - 使用 javascript 在任务栏中创建气球弹出窗口

gulp - 将浏览器同步放在 polymer 自定义构建的 gulpfile 中的正确位置在哪里?

javascript - Rails Assets 在生产中不可用

javascript - 引用错误: require is not defined at gulpfile. js

javascript - 无法在 React 组件的 let/var 中存储箭头函数

javascript - Babel 7 : Function. prototype.toString: 'this' 不是函数对象

javascript - 使用 express 时在 EJS View 中查找当前 url

javascript - 如何在 HTML Canvas 上旋转矩形?