首页 > 解决方案 > 如何在 gulp 中动态设置目标路径?

问题描述

我有一个简单的 gulp 任务,将文件夹树从node-modules特定目的地移动,保持文件夹结构如下:

function libraries(){

    let libPaths = [
        './node_modules/jquery/dist/**/*.*', // Note: copying the ``dist`` folder
        './node_modules/bootstrap-icons/**/*.*' // Note: copying the module
    ]

    return gulp.src(libPaths, {base: './node_modules/'})
        .pipe(gulp.dest(
                (vinyl) => {
                    // Something here?
                    return '/destination/'
                }
            )
        );
    }

结果:

destination
├── bootstrap-icons
│   ├── LICENSE.md
│   ├── README.md
│   ├── bootstrap-icons.svg
│   ├── font
│   ├── icons
│   └── package.json
└── jquery
    └── dist            // <- problem
        ├── jquery.js
        ├── jquery.min.js
        ├── jquery.min.map
        ├── jquery.slim.js
        ├── jquery.slim.min.js
        └── jquery.slim.min.map

期待:

destination
├── bootstrap-icons
│   ├── LICENSE.md
│   ├── README.md
│   ├── bootstrap-icons.svg
│   ├── font
│   ├── icons
│   └── package.json
└── jquery
    ├── jquery.js
    ├── jquery.min.js
    ├── jquery.min.map
    ├── jquery.slim.js
    ├── jquery.slim.min.js
    └── jquery.slim.min.map

我将如何检测源文件何时在dist/例如./node_modules/jquery/dist/jquery.js将目标输出设置为/destination/jquery/jquery.js- 而不是在/destination/jquery/dist/jquery.js.

标签: javascriptgulp

解决方案


我让它像这样工作:

function libraries(){

    let libPaths = [
        './node_modules/jquery/dist/**/*.*', // Note: copying the ``dist`` folder
        './node_modules/bootstrap-icons/**/*.*' // Note: copying the module
    ]

    return gulp.src(libPaths, {base: './node_modules/'})
        .pipe(gulp.dest(
                (vinyl) => {
                    vinyl.path = path.join(
                        vinyl.cwd, vinyl.base,
                        vinyl.relative.replace('dist\\', ''));
                    return '/destination/'
                }
            )
        );
    }

推荐阅读