0

我正在尝试从 Javascript 动态加载一些 css 文件。

片段:

  if (theme === 'testtheme' || theme === 'testtheme/') {
    css =
      <!-- build:css({.tmp,app}) styles/main_testtheme.css -->
      '<link rel="stylesheet" href="styles/css/main_testtheme.css" type="text/css" />'
      <!-- endbuild -->
    ;
  } else {
    css =
      <!-- build:css({.tmp,app}) styles/main.css -->
      '<link rel="stylesheet" href="styles/css/main.css" type="text/css" />'
      <!-- endbuild -->
    ;
  }

但是,grunt-build 任务将构建注释之间的所有文本替换为以下内容:

<link rel="stylesheet" href="styles/e59b065a.main.css">

从而删除字符串引号并使代码无效。

我想如何运行:

<!-- build:css({.tmp,app}) styles/main.css -->
css = 'styles/css/main.css';
<!-- endbuild -->

应该导致:

css = 'styles/e59b065a.main.css';

这将允许测试未缩小(未构建)和缩小版本。Grunt build 对我来说大约需要 5 分钟,所以我在开发时尽量避免这种情况。

编辑: 我可能可以覆盖 css 的默认 blockReplacement (请参阅https://github.com/yeoman/grunt-usemin#blockreplacements),但这会让任何后来尝试弄清楚为什么他们的样式表不是正确嵌入。

4

1 回答 1

0

我仍然无法为此找到可接受的解决方案,但现在有一个可行的解决方案:

Gruntfile.js 片段:

useminPrepare: {
  html: ['<%= yeoman.app %>/index.html', '<%= yeoman.app %>/includes.html'],
  options: {
    dest: '<%= yeoman.dist %>',
    flow: {
      // i'm using this config for all targets, not only 'html'
      steps: {
        // Here you define your flow for your custom block
        cssQuoted: ['concat', 'cssmin'],
        // use the option below where you have minified files that you just want to concatenate
        concatjs: ['concat'],
        // Note that you NEED to redefine flow for default blocks...
        // These below is default flow.
        js: ['concat', 'uglifyjs'],
        css: ['concat', 'cssmin']
      },
      // also you MUST define 'post' field to something not null
      post: {}
    }
  }
},
usemin: {
  //css_name: ['<%= yeoman.dist %>/{,*/}*.html'],
  html: ['<%= yeoman.dist %>/{,*/}*.html'],
  css: ['<%= yeoman.dist %>/styles/{,*/}*.css'],
  options: {
    //patterns: {
    //  html: [
    //    [ /cssHrefString\s+=\s+['"]([^"']+)["']/gm, 'Update the HTML to reference our revved/min CSS and add quotes' ]
    //  ]
    //},
    blockReplacements: {
      cssQuoted: function(block){
        return '\'<link rel="stylesheet" href="' + block.dest + '">\'';
      },
      concatjs: function (block) {
        return '<script src="' + block.dest + '"></script>';
      }
    }
  }
},

script.js 片段:

var theme = getUrlParameter('theme');
var cssHrefString;
// we need fully qualified css tags below otherwise grunt build will not be able to pick them up
if (theme === 'testtheme' || theme === 'testtheme/') {
  cssHrefString =
    <!-- build:cssQuoted({.tmp,app}) styles/main_testtheme.css -->
    '<link rel="stylesheet" href="styles/css/main_testtheme.css">'
    <!-- endbuild -->
  ;
} else {
  cssHrefString =
    <!-- build:cssQuoted({.tmp,app}) styles/main.css -->
    '<link rel="stylesheet" href="styles/css/main.css">'
    <!-- endbuild -->
  ;
}
$('head').append(cssHrefString);

grunt 配置文件添加了concatjs的定义- 一个仅连接缩小文件的任务,不会在它们上运行 uglifier。cssQuoted获取块之间的字符串并将其替换为带引号的“link rel = ...”标签。

确保你的 grunt-usemin 插件版本是最新的——我花了几个小时尝试旧版本(~0.1.3)的选项。上面的代码适用于 3.0.0。

于 2015-10-15T13:21:31.113 回答