Gulp:gulpfile.js之外的变量?

时间:2016-04-19 14:41:39

标签: javascript variables gulp

是否可以将我的变量放在gulpfile.js之外?例如,这个块放入paths.js

    path = {
    css: 'web/default/css/',
    scss: 'web/default/scss/',
    js: 'web/default/js/',
    images: 'web/default/images/',
    imgDev: 'web/default/imgDev/',
    imgRetina: 'web/default/imgDev/imgRetina/',
    tplSprite: 'web/default/tools/',
    tpl: 'themes/default/templates/',
    versionBrowsers: 'last 2 versions',
    srv: 'http://www.sbd.local/',
    concatJsScripts: [
        'web/default/js/assets/jquery.raty.js',
        'web/default/js/assets/jquery.bxslider.min.js',
        'web/default/js/assets/main.js'
    ]
};

并在我的gulpfile.js中,致电path.srvpath.js

由于

1 个答案:

答案 0 :(得分:0)

您始终可以require外部文件并访问所有导出的对象属性

// paths.js
'use strict';
module.exports = {
  css: 'web/default/css/',
  scss: 'web/default/scss/',
  js: 'web/default/js/',
  images: 'web/default/images/',
  imgDev: 'web/default/imgDev/',
  imgRetina: 'web/default/imgDev/imgRetina/',
  tplSprite: 'web/default/tools/',
  tpl: 'themes/default/templates/',
  versionBrowsers: 'last 2 versions',
  srv: 'http://www.sbd.local/',
  concatJsScripts: [
    'web/default/js/assets/jquery.raty.js',
    'web/default/js/assets/jquery.bxslider.min.js',
    'web/default/js/assets/main.js'
  ]
};


// gulpfile.js
'use strict';
var path = require('./paths.js');

// path to css
var css = path.css;

或者,您也可以定义路径JSON格式,使其看起来更像配置文件。

// paths.json
{
  "css": "web/default/css/",
  "scss": "web/default/scss/",
  "js": "web/default/js/",
  "images": "web/default/images/",
  "imgDev": "web/default/imgDev/",
  "imgRetina": "web/default/imgDev/imgRetina/",
  "tplSprite": "web/default/tools/",
  "tpl": "themes/default/templates/",
  "versionBrowsers": "last 2 versions",
  "srv": "http://www.sbd.local/",
  "concatJsScripts": [
    "web/default/js/assets/jquery.raty.js",
    "web/default/js/assets/jquery.bxslider.min.js",
    "web/default/js/assets/main.js"
  ]
};