首页 > 解决方案 > 如何从我的 Babel 编译代码中删除 transform typeof 符号代码?

问题描述

我最近将 babel 添加到我的 gulpfile 中,这样我就可以开始在 ES6 中编写代码了。代码按预期编译,但我注意到以下代码行在多个位置添加到已编译的 JS 文件中。我认为我不需要它,因此想删除它:

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };

我试过的

gulpfile.babel.js,里面createBuildTask(),我presets这样设置:'presets': [['env', {'exclude': ['transform-typeof-symbol']}]]

...这导致在 gulp 构建期间返回以下错误:

Invalid Option: The plugins/built-ins 'transform-typeof-symbol' passed to the 'exclude' option are not valid. Please check data/[plugin-features|built-in-features].js in babel-preset-env

这是删除的正确方法,还是我应该以不同的方式处理它?下面我已经包括了我的相关部分package.json,以及gulpfile.babel.js

package.json (包括dev , devDependencies )

"devDependencies": {
    "babel-core": "^6.26.3",
    "babel-eslint": "^7.2.3",
    "babel-preset-env": "^1.7.0",
    "babel-register": "^6.26.0",
    "del": "^2.2.2",
    "eslint": "^6.6.0",
    "gulp": "^4.0.2",
    "gulp-babel": "^7.0.1",
    "gulp-concat": "^2.6.1",
    "gulp-eslint": "^6.0.0",
    "gulp-rename": "^1.2.2",
    "gulp-terser": "^1.2.0"
},
"dependencies": {
    "babel-plugin-transform-remove-strict-mode": "0.0.2",
    "babel-polyfill": "^6.26.0",
    "lodash": "^4.17.15"
}

gulpfile.babel.js

var gulp = require('gulp');
var babel = require('gulp-babel');
var concat = require('gulp-concat');
var del = require('del');
var eslint = require('gulp-eslint');
var rename = require('gulp-rename');
var terser = require('gulp-terser');

function createBuildTask() {
    var sourceArray = [
        'common/*.js',
        'nep/api/*.js',
        'nep/controller/*.js',
        'nep/legacy/*.js',
        'nep/nep-sat.js'
    ];

    return function () {
        return gulp.src(sourceArray, {'allowEmpty': true})
            .pipe(babel({
                'presets': ['env'],
                'plugins': ['transform-remove-strict-mode']
            }))
            .pipe(concat('nep-built-sat.js'))
            .pipe(gulp.dest('nep/dist'))
            .pipe(terser())
            .pipe(rename({
                'extname': '.min.js'
            }))
            .pipe(gulp.dest('nep/dist'));
    };
}

function createLintTask(destination) {
    return function () {
        return gulp.src(destination + '/' + destination + '-sat.js')
            .pipe(eslint())
            .pipe(eslint.format())
            .pipe(eslint.failAfterError());
    };
}

gulp.task('lint-nep', createLintTask('nep'));
gulp.task('build-nep', createBuildTask());

gulp.task('nep', gulp.series('lint-nep', 'build-nep'));
gulp.task('default', gulp.series('lint-nep', 'build-nep'));

gulp.task('clean', (done) => {
    del.sync(['*/dist']);
    done();
});

标签: javascriptgulpbabeljs

解决方案


推荐阅读