gulp-typescript
gulp-typescript copied to clipboard
Error: gulp-typescript: A project cannot be used in two compilations at the same time. Create multiple projects with createProject instead
Expected behavior: No error When I run watch, I got the following error
Actual behavior:
[22:39:13] Error: gulp-typescript: A project cannot be used in two compilations at the same time. Create multiple projects with createProject instead. at project (/Users/zhutian/Code/billofchen/node_modules/gulp-typescript/release/project.js:36:19) at componentTS (/Users/zhutian/Code/billofchen/gulpfile.js:50:15) at bound (domain.js:415:14) at runBound (domain.js:428:12) at asyncRunner (/Users/zhutian/Code/billofchen/node_modules/async-done/index.js:55:18) at processTicksAndRejections (internal/process/task_queues.js:75:11)
Your gulpfile:
const tsProject = tsCompiler.createProject(path.join(__dirname, 'tsconfig-build.json'))
async function componentTS() {
return tsProject.src()
.pipe(sourcemaps.init())
.pipe(tsProject())
.pipe(sourcemaps.write())
.pipe(gulp.dest(distPath))
}
function parallelWatch() {
gulp.watch(
[path.join(srcPath, '**/*.ts'), '!' + path.join(srcPath, '**/__test__/**')],
{ ignoreInitial: false },
componentTS
)
}
const watch = gulp.series(cleanDist, parallelWatch)
tsconfig.json
Include your tsconfig, if related to this issue.
{
"compilerOptions": {
"target": "ES6",
"lib": ["ES6", "DOM"],
"strictNullChecks": true,
"noImplicitAny": false,
"noUnusedLocals": false,
"noUnusedParameters": false,
"module": "CommonJS",
"moduleResolution": "node",
"allowJs": false,
"experimentalDecorators": true,
"noImplicitThis": true,
"noImplicitReturns": true,
"alwaysStrict": true,
"inlineSourceMap": true,
"inlineSources": true,
"noFallthroughCasesInSwitch": true,
"incremental": true,
"strict": true,
"removeComments": true,
"pretty": true,
"esModuleInterop": true,
"skipLibCheck": true,
"allowSyntheticDefaultImports": true,
"strictPropertyInitialization": true,
},
"include": ["./src/**/*.ts", "node_modules/miniprogram-api-typings/index.d.ts"]
}
I got around this by creating the tsProject inside the Gulp task, which the docs say not to do... but it was the only thing I could find that stopped this error from happening. I only have the one build task that uses the tsProject and watch just calls that.
Note my Gulpfile is also in TypeScript, unclear if that makes a difference.
function build(): NodeJS.ReadWriteStream {
const tsProject = typescript.createProject(path.join(taskName, 'tsconfig.json'));
return tsProject
.src()
.pipe(eslint.default())
.pipe(eslint.format())
.pipe(eslint.failAfterError())
.pipe(tsProject(typescript.reporter.longReporter()))
.js
.pipe(gulp.dest((file) => {
return file.base ?? process.cwd();
}));
}
function watch(): FSWatcher {
return gulp.watch(path.join('src', '**', '*.ts'), build);
}
exports.watch = gulp.series(build, watch);