在gulpfile.js中将package.json依赖项用作folderstructure

在gulpfile.js中将package.json依赖项用作folderstructure,gulp,node-modules,package.json,gulp-sass,Gulp,Node Modules,Package.json,Gulp Sass,我正在尝试创建一个自动化流程,以便在项目中包含节点模块。一些模块包含css,因此我尝试以一种方式创建gulpfile.js,它可以读取这些模块并包含该模块的css 我尽量有选择性,只选择作为依赖项安装的文件夹。不是整个node_modules文件夹 My gulpfile.js: // Import (not showing all for the sake of the question) const sourcemaps = require('gulp-sourcemaps'); const

我正在尝试创建一个自动化流程,以便在项目中包含节点模块。一些模块包含css,因此我尝试以一种方式创建gulpfile.js,它可以读取这些模块并包含该模块的css

我尽量有选择性,只选择作为依赖项安装的文件夹。不是整个node_modules文件夹

My gulpfile.js:

// Import (not showing all for the sake of the question)
const sourcemaps = require('gulp-sourcemaps');
const sass = require('gulp-sass');
const postcss = require('gulp-postcss');
const autoprefixer = require('autoprefixer');
const cssnano = require('cssnano');
const fs = require('fs');
const json = JSON.parse(fs.readFileSync('./package.json'));

// File paths
const files = { 
    cssPath: 'assets/styles/**/*.scss',
    jsPath: 'assets/scripts/**/*.js',
    imgPath: 'assets/images/**/*',
    modulesPath: ['node_modules']+json.dependencies+'/**/*.scss'
    //Desired output: node_modules/module_name/all_folders/all.scss
}

// Compile CSS
function styles(){
    return src([files.cssPath, files.modulesPath])
        .pipe(sourcemaps.init())
        .pipe(sass())
        .pipe(postcss([ autoprefixer(), cssnano() ]))
        .pipe(sourcemaps.write('.'))
        .pipe(dest('dist/styles')
    );
}

当我运行“gulp styles”时,函数运行得很好,但不包括所需的样式。我做错了什么?

首先,有一种更简单的方法可以获取您的
package.json
文件:

const-package=require('./package.json');
然后需要依赖项的名称,这些名称是
依赖项
对象中的键。将这些映射到全局,如下所示:

const文件={
...
modulePath:Object.keys(package.dependencies).map(module=>`node_modules/${module}/***.scss`)
}
最后,在
styles
任务中分解该数组:

返回src([files.cssPath,…files.modulePath])