我在gulp
merge-stream
的一个星期五晚上很奇怪。有人告诉我:
TypeError: merge.add is not a function
package.json:
{
"name": "myProject",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"bootstrap": "^4.3.1",
"bootstrap-datepicker": "^1.8.0",
"jquery": "^3.4.0",
"jquery-validation": "^1.19.0",
"jquery-validation-unobtrusive": "^3.2.11",
"moment": "^2.24.0",
"popper.js": "^1.15.0",
"spectrum-colorpicker": "^1.8.0",
"spin.js": "^4.0.0"
},
"devDependencies": {
"gulp": "^4.0.0",
"gulp-clean": "^0.4.0",
"gulp-cli": "^2.1.0",
"merge-stream": "^1.0.1"
}
}
gulpfile.js:
var gulp = require("gulp");
var gclean = require("gulp-clean");
var paths = {
node: "./node_modules/",
lib: "./wwwroot/lib/"
};
var packages = {
"bootstrap": {
src: paths.node + "bootstrap/dist/**",
dest: paths.lib + "bootstrap"
},
"bootstrap-datepicker": {
src: paths.node + "bootstrap-datepicker/dist/**",
dest: paths.lib + "bootstrap-datepicker"
},
"jquery": {
src: paths.node + "jquery/dist/**",
dest: paths.lib + "jquery"
},
"jquery-validation": {
src: paths.node + "jquery-validation/dist/**",
dest: paths.lib + "jquery-validation"
},
"jquery-validation-unobtrusive": {
src: paths.node + "jquery-validation-unobtrusive/dist/**",
dest: paths.lib + "jquery-validation-unobtrusive"
},
"moment": {
src: paths.node + "moment/min/**",
dest: paths.lib + "moment"
},
"popper.js": {
src: paths.node + "popper.js/dist/umd/**",
dest: paths.lib + "popper.js"
},
"spectrum-colorpicker": {
src: paths.node + "spectrum-colorpicker/spectrum.*",
dest: paths.lib + "spectrum-colorpicker"
},
"spin.js": {
src: paths.node + "spin.js/spin.*",
dest: paths.lib + "spin.js"
}
};
function clean() {
return gulp.src(paths.lib + "*", { read: false })
.pipe(gclean());
}
function build() {
const merge = require("merge-stream");
for (let item in packages) {
if (packages.hasOwnProperty(item)) {
console.log(item);
console.dir(packages[item]);
merge.add(
gulp.src(packages[item].src)
.pipe(gulp.dest(packages[item].dest))
);
}
}
return merge.isEmpty() ? null : merge;
}
exports.build = build;
exports.clean = clean;
exports.default = gulp.series(clean, build);
命令行结果为:
> gulp
[16:32:04] Using gulpfile H:\Projects\My Projects\myProject\myProject\gulpfile.js
[16:32:04] Starting 'default'...
[16:32:04] Starting 'clean'...
[16:32:04] Finished 'clean' after 15 ms
[16:32:04] Starting 'build'...
bootstrap
{ src: './node_modules/bootstrap/dist/**',
dest: './wwwroot/lib/bootstrap' }
[16:32:04] 'build' errored after 16 ms
[16:32:04] TypeError: merge.add is not a function
at build (H:\Projects\My Projects\myProject\myProject\gulpfile.js:61:19)
at bound (domain.js:395:14)
at runBound (domain.js:408:12)
at asyncRunner (H:\Projects\My Projects\myProject\myProject\node_modules\async-done\index.js:55:18)
at process._tickCallback (internal/process/next_tick.js:61:11)
[16:32:04] 'default' errored after 43 ms
一定很简单,我实在太累了。想法?
答案 0 :(得分:1)
更改
const merge = require("merge-stream");
到
const merge = require("merge-stream")();