Gulp任务从写入Index.html文件中排除特定的js文件

时间:2018-05-29 14:34:38

标签: javascript gulp

您好我是Gulp的新手,我的要求是在build-dist任务中将特定的js文件排除在写入index.html之外。以下是要写入index.html的文件。           

下面是gulpfile.js:对于我的要求,我猜gulp.task('full-build-dist',['build-dist']应该修改为指定前缀为(!)的js文件,但不知道实际的代码,所以你的帮助将不胜感激。

'use strict';

/*** DO NOT MODIFY THIS FILE ***/

let gulp = require('gulp');
let fs = require('fs-extra');
let inject = require('gulp-inject');
let sequence = require('run-sequence');
/**
 * Load the default configuration from the ng1-template-gulp package.
 */
let ng1TemplateGulp = require('ng1-template-gulp');
let config = ng1TemplateGulp.config;
let utils = ng1TemplateGulp.utils;


/**
 * Once the config is loaded from the ng1-template-gulp package, call the local gulp.config module
 * to customize the config as per the project requirements.
 */
utils.compileTsFile('./gulp.config.ts', config.folders.build);
require(`${config.folders.build}gulp.config`)(config);


config.options.bowerJson = require(`${config.folders.root}bower.json`);

gulp.task('load_rev_manifest', done => {
    config.revManifest = require(`${config.folders.distBuild}rev-manifest.json`);
    done();
});


gulp.task('full-build-dist', ['build-dist'], () => {
    utils.log('Creating a new json file for cms to use');
    var fileContent = fs.readFileSync(`${config.folders.distBuild}index.html`, "utf8");
    let array = fileContent.split('<script');
    let result = [];
    for (let i = 0; i < array.length; i++) {
        if (i == 0 || i == array.length - 1 || i == array.length - 2) {
            continue;
        }
        let elem = array[i];
        let tempResult = elem.substring(elem.indexOf('js/') + 3, elem.indexOf('</script>') - 2);
        result.push(tempResult);
    }
    utils.log(`Element :${config.folders.distBuild}` + JSON.stringify(result));
    fs.writeJson(`${config.folders.distBuild}library.json`, result, function (err) {
        // console.log(err)
    })
});
var bless = require('gulp-bless');
gulp.task('split', () => {
    utils.log('css only' + `${config.folders.distBuild}css`);
    return gulp.src(`${config.folders.distBuild}css/*.css`)
        .pipe(bless({
            imports: false,
            suffix: '-part'
        }))
        .pipe(gulp.dest(`${config.folders.distBuild}css`));
});



gulp.task('complete-build', function () {

    sequence('clean', 'build-dist', 'copy-files-upload', 'copy-files-upload1', 'copy-files-upload2', 'copy-files-upload3', 'copy-files-add', 'split', 'add-css');

});

gulp.task('add-css', function () {
    let cssFiles = `${config.folders.distBuild}css/*part1.css`;
    let cssSrc = gulp.src(cssFiles, { read: false });
    let configOptions = {
        starttag: '<!-- inject:css:def -->',
        relative: true,
        removeTags: true
    };
    let injectTask = utils.src(`${config.folders.distBuild}index.html`, 'local-inject')
        .pipe(inject(cssSrc, configOptions));
    injectTask = injectTask.pipe(gulp.dest(`${config.folders.distBuild}`));
    return injectTask;
});

gulp.task('copy-files-upload', function () {
    let jsFiles = [`${config.folders.bower}ng-file-upload-shim/*`];
    return gulp.src(jsFiles).pipe(gulp.dest(`${config.folders.distBuild}js/ng-file-upload-shim`));

});
gulp.task('copy-files-upload1', function () {
    let jsFiles1 = [`${config.folders.bower}ng-file-upload/*`];
    return gulp.src(jsFiles1).pipe(gulp.dest(`${config.folders.distBuild}js/ng-file-upload`));

});
gulp.task('copy-files-upload2', function () {
    let jsFiles2 = [`${config.folders.client}/customjs/fileuploader.js`];
    return gulp.src(jsFiles2).pipe(gulp.dest(`${config.folders.distBuild}js`));

});
gulp.task('copy-files-upload3', function () {
    let jsFiles2 = [`${config.folders.client}/customjs/tealeaf*.js`];
    console.log('jsFiles2', jsFiles2);
    return gulp.src(jsFiles2).pipe(gulp.dest(`${config.folders.distBuild}js`));

});
gulp.task('copy-files-add', function () {
    let jsFiles = [`${config.folders.distBuild}js/ng-file-upload/ng-file-upload-shim.js`, `${config.folders.distBuild}js/ng-file-upload/ng-file-upload.js`, `${config.folders.distBuild}js/fileuploader.js`, `${config.folders.distBuild}js/tealeaf*.js`];
    let jsSrc = gulp.src(jsFiles, { read: false });
    let configOptions = {
        starttag: '<!-- inject:js:upload -->',
        relative: true,
        removeTags: true
    };
    let injectTask = utils.src(`${config.folders.distBuild}index.html`, 'local-inject')
        .pipe(inject(jsSrc, configOptions));
    injectTask = injectTask.pipe(gulp.dest(`${config.folders.distBuild}`));
    return injectTask;
});

1 个答案:

答案 0 :(得分:0)

这是我的朋友为gulp制作的,只将两个脚本捆绑在一起

git difftool

这个方便的代码将uglify和sourcemap一切。不要结束,因为这正在成为网络开发的反模式。我可能错过了所需的包,但如果我这样做,错误很容易阅读。 :d