Sails.js指定要从自动插入中排除的JS文件

时间:2015-03-29 08:16:16

标签: javascript node.js sails.js browserify

我试图设置我的风帆应用程序以使用Browserify(工作正常)。但是,我希望不将非浏览器化文件自动注入我的网页。

在我的tasks/pipeline.js文件中我尝试了这个(我需要浏览的js文件位于browserify目录中):

// Client-side javascript files to inject in order
// (uses Grunt-style wildcard/glob/splat expressions)
var jsFilesToInject = [

  // Load sails.io before everything else
  'js/dependencies/sails.io.js',

  // Dependencies like jQuery, or Angular are brought in here
  'js/dependencies/**/*.js',

  // All of the rest of your client-side js files
  // will be injected here in no particular order.
  'js/**/*.js',

  // Ignore browserify directory
  '!js/browserify/**/*'
];

然而,这不起作用,我的非浏览器化文件被注入网页。我是Sails的新手,所以很可能这根本不是实现这一目标的正确方法。任何建议都将不胜感激。

3 个答案:

答案 0 :(得分:3)

您可以使用“exclude”(!)运算符排除文件,如下所示:

var cssFilesToInject = [
  'styles/**/*.css',
  '!styles/ie8.css',
  '!styles/ie9.css'
];

// Client-side javascript files to inject in order
// (uses Grunt-style wildcard/glob/splat expressions)
var jsFilesToInject = [

  // Load sails.io before everything else
  'js/dependencies/sails.io.js',

  // Dependencies like jQuery, or Angular are brought in here
  'js/dependencies/**/*.js',

  // All of the rest of your client-side js files
  // will be injected here in no particular order.
  'js/**/*.js',

  "!js/dependencies/respond/respond.src.js",
];

答案 1 :(得分:0)

它实际上相当容易,只是不要把**放在那里。 **通配符表示递归搜索。我要做的是以下几点:

var jsFilesToInject = [

  // Load sails.io before everything else
  'js/dependencies/sails.io.js',

  // Dependencies like jQuery, or Angular are brought in here
  'js/dependencies/**/*.js',

  // All of the rest of your client-side js files
  // will be injected here in no particular order.
  'js/*.js', //all files directly in .js folder will be included, but any folders not specified above will not
];

然后,如果你将一个文件夹添加到你想要包含的js目录中,只需确保在依赖项和最后一行之间指定它,如

'js/folderIWantToInclude/**/*.js'

不要忘记,那个咕噜声将所有文件复制到.tmp,有时您需要手动清除它以使这些更改生效。 我还建议访问Grunt Configuration file docs作为参考 - 这是包含在内而不是风帆的咕噜声。

答案 2 :(得分:0)

通过快速修复,您可以按照自己的方式工作: 在pipeline.js结尾处,替换以下代码块:

module.exports.cssFilesToInject = cssFilesToInject.map(function(path) {
  return '.tmp/public/' + path;
});
module.exports.jsFilesToInject = jsFilesToInject.map(function(path) {
  return '.tmp/public/' + path;
});
module.exports.templateFilesToInject = templateFilesToInject.map(function(path) {
  return 'assets/' + path;
});

使用

module.exports.cssFilesToInject = cssFilesToInject.map(function(path) {
  var tmpPath = '.tmp/public/';
  if (path.substring(0,1) == '!')
    return '!' + tmpPath + path.substring(1);
  return tmpPath + path;
});
module.exports.jsFilesToInject = jsFilesToInject.map(function(path) {
  var tmpPath = '.tmp/public/';
  if (path.substring(0,1) == '!')
    return '!' + tmpPath + path.substring(1);
  return tmpPath + path;
});
module.exports.templateFilesToInject = templateFilesToInject.map(function(path) {
  var tmpPath = 'assets/';
  if (path.substring(0,1) == '!')
    return '!' + tmpPath + path.substring(1);
  return tmpPath + path;
});

正如您所看到的,原始代码会将tmp文件夹的相对路径预先设置为给定规则,从而导致!位于最终路径的中间位置。如需完整解释see my answer to a very similar issue