Как я могу удалить код символа преобразования typeof из моего скомпилированного кода Babel? - PullRequest
0 голосов
/ 05 ноября 2019

Я недавно добавил 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();
});
...