typescript 解决方案“错误:gulp-typescript:项目不能同时用于两个编译*。使用createP创建多个项目

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了typescript 解决方案“错误:gulp-typescript:项目不能同时用于两个编译*。使用createP创建多个项目相关的知识,希望对你有一定的参考价值。

gulp.task('build', () => {
    let project = tsc.createProject('path/to/tsconfig.json');
    return project.src()
        .pipe(sourcemaps.init())
        .pipe(tsCompileQStream(project)) // wrap the project with the function call
        .js // you don't need this as the wrapper exposes the js stream by default
        .pipe(sourcemaps.write())
        .pipe(gulp.dest(project.options.outDir));
});
import {Project, CompileStream} from 'gulp-typescript';
import {Duplex, PassThrough, Readable} from 'stream';
import {Reporter} from 'gulp-typescript/release/reporter';

type Callback = () => void;

/**
 * This is used to ensure that each project object is not busy when it is to be used
 * This prevents the annoying:
 * "Error: gulp-typescript: A project cannot be used in two compilations
 * at the same time. Create multiple projects with createProject instead."
 * @param project The project
 * @param reporter The reporter for the project
 * @returns {CompileStream} compiled project stream
 */
export function tsCompileQStream(project: Project, reporter?: Reporter): CompileStream {

    return new class extends PassThrough implements CompileStream {
        public readonly js: Duplex = this;
        public readonly dts: Duplex = new PassThrough();
        private transformStream: CompileStream;
        private signal: Callback;

        constructor() {
            super({ objectMode: true });
            this.on('pipe', this.checkExistingFlow);
        }

        private checkExistingFlow(src: Readable) {
            this.removeListener('pipe', this.checkExistingFlow);
            src.unpipe(this);

            this.signal = CompileScheduler.scheduleCompilation(project, () => {
                this.transformStream = project(reporter).on('finish', () => this.signal());

                let compileStream = src.pipe(this.transformStream);
                compileStream.js.pipe(this.js);
                compileStream.dts.pipe(this.dts);
            });
        }
    };
}

class CompileScheduler {
    private static compileGateKeeper: Map<Project, Callback[]> = new Map();

    public static scheduleCompilation(project: Project, beginCompilation: Callback): Callback {
        let projectQueue = CompileScheduler.compileGateKeeper.get(project);
        if (!projectQueue) {
            projectQueue = [];
            CompileScheduler.compileGateKeeper.set(project, projectQueue);
        }
        let ret = CompileScheduler.startNext(project);
        if (projectQueue.length) {
            projectQueue.push(beginCompilation);
        } else {
            projectQueue.push(ret);
            beginCompilation();
        }
        return ret;
    }

    private static startNext(project: Project): Callback {
        return () => {
            let projectQueue = CompileScheduler.compileGateKeeper.get(project);
            if (projectQueue.length) {
                let nextCompilation = projectQueue.shift();
                nextCompilation();
            }
        };
    }
    private constructor() {}
}

以上是关于typescript 解决方案“错误:gulp-typescript:项目不能同时用于两个编译*。使用createP创建多个项目的主要内容,如果未能解决你的问题,请参考以下文章

按字母顺序对 TypeScript 界面键进行排序

typescript 错误元素获得焦点的可能解决方案。

优雅解决 TypeScript 生成接口文档的问题

TypeScript 命名空间

用Typescript编写AngularJS应用是怎样一种感受

TypeScript tsc 引发错误“无效句柄”80070006(解决方案)