Require another file in gulpfile (which isn't in node_modules)

安稳与你 提交于 2019-12-06 16:35:00

First create a new js file (here ./lib/myModule.js):

//./lib/myModule.js
module.exports =  {
    fn1: function() { /**/ },
    fn2: function() { /**/ },
}

You could also pass some arguments to your module:

// ./lib/myAwesomeModule.js
var fn1 = function() {
}
module.exports =  function(args) {
    fn1: fn1,
    fn2: function() { 
        // do something with the args variable
    },
}

Then require it in your gulpfile:

//gulpfile.js
var myModule = require('./lib/myModule')

// Note: here you required and call the function with some parameters
var myAwesomeModule = require('./lib/myAwesomeModule')({
    super: "duper",
    env: "development"
});

// you could also have done
/*
var myAwesomeModuleRequire = require('./lib/myAwesomeModule')
var myAwesomeModule = myAwesomeModuleRequire({
    super: "duper",
    env: "development"
});
*/

gulp.task('test', function() {
   gulp.src()
    .pipe(myModule.fn1)
    .pipe(myAwesomeModule.fn1)
    .gulp.dest()
}
Marina ES

First, you have to add export default <nameOfYourFile> at the end of your file

Then to use it, write import gulp from 'gulp'

If you have an error message, install babel-core and babel-preset-es2015 with NPM, and add a preset "presets": ["es2015"] in your .babelrc config file.

I fix my problem by install:

npm i babel-plugin-add-module-exports

Then i add "plugins": [["add-module-exports"]] to the .babelrc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!