Gulp 如何动态设置目的路径?

kpbpu008  于 2022-12-08  发布在  Gulp
关注(0)|答案(1)|浏览(201)

我有一个简单的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

rks48beu

rks48beu1#

我是这样做的:

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/'
                }
            )
        );
    }

相关问题