如何在gulp管道中设置全局变量

时间:2018-09-12 09:39:06

标签: javascript gulp

var destPath = ""; // Empty path as default init
gulp.task("style", function() {
    console.log(destPath); // ""
    return gulp.src(env.devPath + assets.src + styles.src + styles.srcFile)
        .pipe(buffer())
        .pipe(plumber())
        .pipe(sass({
            includePaths: ['node_modules/']
        }))
        .pipe(postcss([
            autoprefixer()
        ]))
        .pipe(csscomb())
        .pipe(rename(styles.destFile))
        .pipe(
            destPath === ""
            ? prompt.prompt({
                type: 'list',
                name: 'path',
                message: 'Choose project: ',
                choices: ["Static", "WP"],
                pageSize:'10'
            }, function(res){
                console.log(destPath);
                console.log("You choose: " + res.path);
                switch(res.path) {
                    case "Static": 
                    destPath = env.staticPath; // Set this value
                    break;
                    case "WP": 
                    destPath = env.wpPath; // Or this
                    break;
                }

                console.log(destPath); // Have that value
            }) 
            : ''
        )
        .pipe(gulp.dest(destPath + styles.dest)) // Have "" 
        .pipe(minify())
        .pipe(rename(styles.destMinFile))
        .pipe(gulp.dest(destPath + styles.dest)) // destPath == ""
        .pipe(server.stream());
});

我有全局变量将空路径保留为默认值,并且想在管道内将此变量的值设置为将其用作目标路径,但不能这样做,请参见代码注释。你能帮我吗?

0 个答案:

没有答案