最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Require another file in gulpfile (which isn't in node_modules) - Stack Overflow

programmeradmin3浏览0评论

I've been using gulp for a while now and know how to import another node module, e.g.

var sass = require('gulp-sass');

That's fine, but my gulpfile is filling up with code that I'd like to move into a separate file and "require". Specifically I am writing a postcss plugin, which I already have working when declared as a function inside of the gulpfile. My question is how to put my function in an external file and require it like I do a node module. Do I need to "export" the function in the file being required? Do I need to use ES6 modules or something like that?

As an aside, I realise that if i was doing this probably I would either (A) turn this into a proper node module and put it on a private NPM repository, but that seems unnecessary, or (B) turn it into a proper gulp plugin, but that would require learning how to author a gulp plugin and learning about streams and stuff. Both of these are probably better but would take more time so I've decided to just keep the function simple and local for now.

I've been using gulp for a while now and know how to import another node module, e.g.

var sass = require('gulp-sass');

That's fine, but my gulpfile is filling up with code that I'd like to move into a separate file and "require". Specifically I am writing a postcss plugin, which I already have working when declared as a function inside of the gulpfile. My question is how to put my function in an external file and require it like I do a node module. Do I need to "export" the function in the file being required? Do I need to use ES6 modules or something like that?

As an aside, I realise that if i was doing this probably I would either (A) turn this into a proper node module and put it on a private NPM repository, but that seems unnecessary, or (B) turn it into a proper gulp plugin, but that would require learning how to author a gulp plugin and learning about streams and stuff. Both of these are probably better but would take more time so I've decided to just keep the function simple and local for now.

Share Improve this question edited Mar 7, 2016 at 16:47 jonhobbs asked Mar 7, 2016 at 15:27 jonhobbsjonhobbs 28k39 gold badges118 silver badges179 bronze badges 3
  • what about require('./your_folder/your_file') – cl3m Commented Mar 7, 2016 at 15:27
  • That sounds simple but what would be happening if I did this? Is require() a gulp function or is this a new javascript function which works because we're using a newer javascript version? if I did var myfunction - require('...'); how would ai make sure that my inner function gets assigned to the variable? – jonhobbs Commented Mar 7, 2016 at 15:31
  • To answer my own question, module.exports and require() seem to be Node functions, they don't belong to gulp and are not native javascript functions. – jonhobbs Commented Mar 7, 2016 at 15:49
Add a ment  | 

3 Answers 3

Reset to default 9

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()
}

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

发布评论

评论列表(0)

  1. 暂无评论