admin管理员组文章数量:1353186
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
3 Answers
Reset to default 9First 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
本文标签: javascriptRequire another file in gulpfile (which isn39t in nodemodules)Stack Overflow
版权声明:本文标题:javascript - Require another file in gulpfile (which isn't in node_modules) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743913230a2560775.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论