I have a gulp file having the following structure:
// =============== helper JS stuff ===============
var production = false;
var folders = {
folder1: "./myfolder",
folder2: "./myfolder2"...
}
function Module() {
this.jsFiles = ...
this.cssFiles = ...
}
var Modules = [
new Module({ first: module }),
new Module({ second: module })...]
// =============== Tasks ===============
gulp.task('myTask', function(){
//work with modules
});
I would like to separate (gulpfile.js) taks from other pure custom javascript things (in special from modules list) in (at least) three files:
Solution
-- gulpFolder
---- consts.js // declare here dirs/folders/other constants
---- module.js // helper 'Module' object declaration
---- listModules.js // a list of 'Module's to work with (to update)
-- gulpfile.js // tasks definitions, works with the module list
Is it possible to just "import" into gulpfile.js
just simple javascript files like described bellow (like consts.js
, module.js
etc)?
I tried to use the requireDir
var requireDir = require('require-dir');
...
var dir = requireDir('./website/!gulp'); // ?
but this code didn't seem to really "import" jsfiles... As I understand, require
and requireDir
works only with nodeJS modules/files...
In node, each javascript source file is a separate namespace. When you require
a file, you can only access what that file has export
-ed. This means you'll have to export
from each of your other files. For example:
module.js:
function Module() {
this.jsFiles = ...
this.cssFiles = ...
}
module.exports = Module; // export!
listModules:
var Modules = [
new Module({ first: module }),
new Module({ second: module })...]
module.exports = Modules; // export!
Then in your main file you can access the code like this:
var Module = require("gulpFolder/module.js");
var Modules = require("gulpFolder/moduleList.js");
gulp.task('myTask', function(){
//work with modules
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.