How to use gulp-load-plugins with TypeScript?

506 views Asked by At

I found no examples for gulp-load-plugins in TypeScript. Unfortunately, my TypeScript is too poor to understand, what should to do from @type/gulp-load-plugins comments.

I tried:

import * as _gulpPlugins from 'gulp-load-plugins';
const gulpPlugins: IGulpPlugins = _gulpPlugins();

return gulp.src(sourceFilesGlobSelections)
        .pipe(gulpPlugins.pug())
        // ...

It makes 4 warnings from Webpack (I dont understand where number such 75:13-25 refers; .pipe(gulpPlugins.pug()) is on 50 row):

WARNING in ../node_modules/gulp-load-plugins/index.js 75:13-25
Critical dependency: the request of a dependency is an expression
 @ ./TaskManagers/MarkupPreprocessingHelper.ts

WARNING in ../node_modules/gulp-load-plugins/index.js 81:48-63
Critical dependency: the request of a dependency is an expression
 @ ./TaskManagers/MarkupPreprocessingHelper.ts

WARNING in ../node_modules/gulp-load-plugins/index.js 117:40-55
Critical dependency: the request of a dependency is an expression
 @ ./TaskManagers/MarkupPreprocessingHelper.ts

WARNING in ../node_modules/gulp-load-plugins/index.js 122:51-66
Critical dependency: the request of a dependency is an expression
 @ ./TaskManagers/MarkupPreprocessingHelper.ts

It was told in @types/gulp-load-plugins:

/**
 * Extend this interface to use Gulp plugins in your gulpfile.js
 */
interface IGulpPlugins {
}

I tried:

interface IGulpPlugins {
  pug: () => NodeJS.ReadWriteStream;
}

It also defined:

declare module 'gulp-load-plugins' {

    interface IOptions {
        // ...
    }

    interface IPluginNameMappings {
        [npmPackageName: string]: string
    }

    /** Loads in any gulp plugins and attaches them to an object, freeing you up from having to manually require each gulp plugin. */
    function gulpLoadPlugins<T extends IGulpPlugins>(options?: IOptions): T;

    // ...
}

It looks like maybe I should use gulpLoadPlugins instead of interface extending ... Is is all that I understand with my current TypeScirpt proficiency, but it not enough to understand how to use gulp-load-plugins in TypeScript.

1

There are 1 answers

0
Takeshi Tokugawa YD On

There is the working example in gulp-load-plugins-tests.ts:

import * as gulp from 'gulp';
import gulpConcat = require('gulp-concat');
import gulpLoadPlugins = require('gulp-load-plugins');

interface GulpPlugins extends IGulpPlugins {
    concat: typeof gulpConcat;
}

gulp.task('taskName', () => {
    gulp.src('*.*')
        .pipe(plugins.concat('concatenated.js'))
        .pipe(gulp.dest('output'));
});

About Critical dependency: the request of a dependency is an expression: do not bundle Node.js dependencies for the webpack projects those target is Node.js (not browser). webpack-node-externals will tell webpack do not bundle Node.js libraries, however it's still possible to import and use them as usual.