【发布时间】:2021-09-01 15:06:55
【问题描述】:
我的应用程序依赖于一个不经常编辑的公司内部 TS 库。我刚刚更新了它,现在用于构建它的 gulp 任务之一将失败。所有其他任务将单独工作或按顺序/并行使用,但使用 @rollup/stream 包的任务会在声明自己完成后导致 gulp 挂起。这是因为构建自动化最终终止了构建,因为它已暂停 2 小时等待 gulp 任务完成。
由于以下问题导致构建失败,我不得不更新我们的内部库。
'rollup' errored after 1.51 s
Error: '__spreadArray' is not exported by node_modules\tslib\tslib.es6.js, imported by src\ObjectUtils.ts
所以我将tsconfig.json 文件从"target": "es5", 更新为"target": "es6",,这会导致汇总挂起。
运行 gulp rollup 给出以下输出
Using gulpfile path/to/gulp/file
Starting 'rollup'...
//
// Outputs all the files being rolled up here...
//
Finished 'rollup' after 1.43 s
// The gulp process then stays open instead of closing and returning me to my prompt
作为一个例子,我在本地运行了任务,后来我自己用 control+c 退出了它,它报告说在那里等待了 10 分 15 秒
阅读了所有文档和 SO 问题后,我发现我确信我们正在按照 async gulp tasks 文档建议的方式处理流,但它不会起作用。然后我尝试使用.on('end', () => { //etc })自己手动关闭返回的汇总流,将内容包装在promise中并返回一个在流完成时解析的promise,使用提供给每个任务函数的回调,上述组合,但我可以'不让汇总任务正确完成并将控制权返回给提示符。
我已经包含了我的 gulpfile、tsconfig.json 和我的 package.json 依赖项的相关部分。
// gulpfile.js
const gulp = require('gulp');
const gulpif = require('gulp-if');
const terser = require("gulp-terser");
const typescript = require("@rollup/plugin-typescript");
const rollup = require("@rollup/stream");
const buffer = require('vinyl-buffer');
const source = require('vinyl-source-stream');
const production = process.env.NODE_ENV == 'production';
function rollupTask () {
// rollup() returns a stream, so returning this should satisfy the async tasks requirements.
// When the stream closes it should tell gulp the tasks has finished right?
return rollup({
input: "./src/main.ts",
external: [
"lodash",
],
output: {
format: "cjs",
sourcemap: true
},
plugins: [
typescript(),
],
})
.pipe(source("common.bundle.js"))
.pipe(buffer())
.pipe(gulpif(production, terser()))
.pipe(gulp.dest('./dist'));
};
exports.rollup = rollupTask;
// tsconfig.json
{
"compilerOptions": {
"declaration": false,
"module": "es2015",
"target": "es6",
"emitDecoratorMetadata": true,
"listFiles": true,
"noImplicitAny": false,
"noUnusedLocals": true,
"noLib": false,
"removeComments": true,
"sourceMap": true,
"experimentalDecorators": true,
"lib": [
"ES5",
"Dom",
"ScriptHost",
"ES2015",
"ES2016",
"ES2017"
]
}
}
// package.json dependencies
"dependencies": {
"lodash": "^4.17.5"
},
"devDependencies": {
"@rollup/plugin-typescript": "^6.1.0",
"@rollup/stream": "^1.1.0",
"@types/lodash": "^4.14.165",
"del": "^6.0.0",
"gulp": "^4.0.2",
"gulp-if": "^3.0.0",
"gulp-sourcemaps": "^3.0.0",
"gulp-terser": "^2.0.0",
"gulp-tslint": "^8.0.0",
"gulp-typescript": "^5.0.1",
"merge2": "^1.4.1",
"rollup": "^2.33.3",
"tslint": "^6.1.3",
"typescript": "^4.0.5",
"vinyl-buffer": "^1.0.1",
"vinyl-source-stream": "^2.0.0"
},
如果事实证明我可以在tsconfig.json 中继续使用"target": "es5", 并找到__spreadArray 问题的修复程序,那么我也可以这样做。
【问题讨论】:
-
一位同事确实建议我在 Ionic github.com/ionic-team/ionic-framework/issues/23090 阅读此相关问题后增加我们的 TS 版本。但这并没有产生有意义的差异。
标签: javascript node.js typescript gulp rollupjs