0

目标是将 ES6 脚本转换为 ES5 并使它们在浏览器中可读。这最适用于我的 node.js gulp 任务,但一些脚本使用“导入”,如

import EstaticoModule from '../../assets/js/helpers/module';

我想跳过这个“导入”,更多地从结果中删除这一行。

“gulp-babel”中是否有一个参数来实现这一点,或者有另一个想法以更好的方式实现这一点?

这是我的吞咽任务:

'use strict';

/**
 * @function `gulp js:lint1`
 * @desc Lint JavaScript files (using `ESLint`).
 */

var gulp = require('gulp'),
    helpers = require('require-dir')('../../helpers'),
    webpack = require('webpack'),
    babel = require("gulp-babel");
    //babel = require("babelify");
    //babel = require("babel-core");

    //require("babel-core");
var taskName = 'js:lint1',
    taskConfig = {
        src: [
            'source/assets/js/**/*.js',
            'source/modules/**/*.js',
            'source/pages/**/*.js',
            'source/demo/modules/**/*.js',
            'source/demo/pages/**/*.js',
            '!source/modules/**/*.data.js',
            '!source/pages/**/*.data.js',
            '!source/demo/modules/**/*.data.js',
            '!source/demo/pages/**/*.data.js',
            '!source/modules/.scaffold/scaffold.js',
            '!source/assets/js/libs/**/*.js',
            '!source/assets/js/libs/wtscript.js'
        ],
        watch: [
            './source/assets/js/**/*.js',
            './source/modules/**/*.js',
            './source/pages/**/*.js',
            './source/demo/modules/**/*.js',
            './source/demo/pages/**/*.js',
            '!./source/modules/.scaffold/scaffold.js'
        ],
        dest: './RSE/',
        srcBase: './source/assets/js/'
    }

gulp.task( taskName, function() {

    var helpers = require('require-dir')('../../helpers'),
        tap = require('gulp-tap'),
        path = require('path'),
        cached = require('gulp-cached'),
        eslint = require('gulp-eslint');

    return gulp.src(taskConfig.src, {
        dot: true
    })
        .pipe(cached('linting'))
        .pipe(eslint())
        .pipe(eslint.formatEach())
        .pipe(tap(function(file) {
            if (file.eslint && file.eslint.errorCount > 0) {
                helpers.errors({
                    task: taskName,
                    message: 'Linting error in file "' + path.relative('./source/', file.path) + '" (details above)'
                });
            }else{
                console.log(file);
            }
        }))
        .pipe(babel({
            presets: [
                'es2015',
                'react'
            ],
            plugins: [
                // Work around some issues in IE
                'transform-class-properties',
                'transform-proto-to-assign',
                ['transform-es2015-classes', {
                    loose: true
                }]
            ]
        }))
        .pipe(gulp.dest(taskConfig.dest))
        ;
});

module.exports = {
    taskName: taskName,
    taskConfig: taskConfig
};
4

1 回答 1

0

我找到了一种方法:

//  EXCLUDE IMPORTS FROM STREAM
var content = file.contents.toString();
content = content.replace(/import/g, "//$&");
//  RETURN STREAM INTO PIPE
file.contents = Buffer.from(content);

这将导致

//import EstaticoModule from '../../assets/js/helpers/module';

以下是代码摘要:

'use strict';

/**
 * @function `gulp js:create:js:files`
 * @desc Lint JavaScript files (using `ESLint`), EXCLUDE IMPORTS FROM STREAM and create separate js files in modules/%module% folder.
 */

var gulp = require('gulp'),
    helpers = require('require-dir')('../../helpers'),
    webpack = require('webpack'),
    babel = require("gulp-babel");

var taskName = 'js:create:js:files',
    taskConfig = {
        src: [
            'source/assets/js/**/*.js',
            'source/modules/**/*.js',
            'source/pages/**/*.js',
            'source/demo/modules/**/*.js',
            'source/demo/pages/**/*.js',
            '!source/modules/**/*.data.js',
            '!source/pages/**/*.data.js',
            '!source/demo/modules/**/*.data.js',
            '!source/demo/pages/**/*.data.js',
            '!source/modules/.scaffold/scaffold.js',
            '!source/assets/js/libs/**/*.js',
            '!source/assets/js/libs/wtscript.js'
        ],
        watch: [
            './source/assets/js/**/*.js',
            './source/modules/**/*.js',
            './source/pages/**/*.js',
            './source/demo/modules/**/*.js',
            './source/demo/pages/**/*.js',
            '!./source/modules/.scaffold/scaffold.js'
        ],
        dest: './build/',
        srcBase: './source/assets/js/'
    }

gulp.task( taskName, function() {

    var helpers = require('require-dir')('../../helpers'),
        tap = require('gulp-tap'),
        path = require('path'),
        cached = require('gulp-cached'),
        eslint = require('gulp-eslint');

    return gulp.src(taskConfig.src, {
        dot: true
    })
        .pipe(cached('linting'))
        .pipe(eslint())
        .pipe(eslint.formatEach())
        .pipe(tap(function(file) {
            if (file.eslint && file.eslint.errorCount > 0) {
                helpers.errors({
                    task: taskName,
                    message: 'Linting error in file "' + path.relative('./source/', file.path) + '" (details above)'
                });
            }else{
                //  EXCLUDE IMPORTS FROM STREAM
                var content = file.contents.toString();
                content = content.replace(/import/g, "//$&");
                //  RETURN STREAM INTO PIPE
                file.contents = Buffer.from(content);
            }
        }))
        .pipe(babel({
            presets: [
                'es2015'
                ,'react'
            ],
            plugins: [
                // Work around some issues in IE
                'transform-class-properties',
                'transform-proto-to-assign',
                ['transform-es2015-classes', {
                    loose: true
                }]
            ]
        }))
        .pipe(gulp.dest(taskConfig.dest))
        ;
});

module.exports = {
    taskName: taskName,
    taskConfig: taskConfig
};
于 2018-07-10T11:18:57.753 回答