Node.js   发布时间:2022-04-24  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何告诉grunt-usemin忽略Django的静态模板标签?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在构建一个Django应用程序,它提供单页Angular应用程序.

Django发布的Angular的index.html具有以下usemin脚本块:

<!-- build:js scripts/scripts.js -->
<script src="{% static "scripts/app.js" %}"></script>
<script src="{% static "scripts/controllers/main.js" %}"></script>
<script src="{% static "scripts/controllers/stats.js" %}"></script>
<script src="{% static "scripts/directives/highchart.js" %}"></script>
<!-- endbuild -->

Usemin尝试在文件系统上找到“{%static”scripts / app.js“%}”,当然也失败了,因为它应该真正想要找到的是“scripts / app.js”.

有没有人见过这个解决方法

解决方法

好吧,我想我有一个解决方法.这假设您希望构建的文件也指向构建资产的静态URl.

这将要求您在视图上下文中定义STATIC_URL(而不是使用src =“{%static’foo / bar.js’%}”您将使用src =“{{STATIC_URL}} foo / bar.js” ).如果不破解grunt-usemin源代码,我无法使{%static%}工作.

所以使用你的例子,这个:

<!-- build:js {{STATIC_URL}}scripts/scripts.js -->
<script src="{{STATIC_URL}}scripts/app.js"></script>
<script src="{{STATIC_URL}}scripts/controllers/main.js"></script>
<script src="{{STATIC_URL}}scripts/controllers/stats.js"></script>
<script src="{{STATIC_URL}}scripts/directives/highchart.js"></script>
<!-- endbuild -->

编译为:

<script src="{{STATIC_URL}}scripts/scripts.js"></script>

为了实现这一点,我不得不添加以下grunt配置(在Gruntfile.js中):

// custom createConfig script for @R_197_9363@cing Django {{STATIC_URL}} references
// when building config for concat and cssmin
var path = require('path');
function createDjangoStaticConcatConfig(co@R_450_10443@t,block) {
  var cfg = {files: []};
  var staticPattern = /\{\{\s*STATIC_URL\s*\}\}/;

  block.dest = block.dest.@R_197_9363@ce(staticPattern,'');
  var outfile = path.join(co@R_450_10443@t.outDir,block.dest);

  // Depending whether or not we're the last of the step we're not going to output the same thing
  var files = {
    dest: outfile,src: []
  };
  co@R_450_10443@t.inFiles.forEach(function(f) {
    files.src.push(path.join(co@R_450_10443@t.inDir,f.@R_197_9363@ce(staticPattern,'')));
  });
  cfg.files.push(files);
  co@R_450_10443@t.outFiles = [block.dest];
  return cfg;
}


grunt.initConfig({
    /*...*/

    // add a preprocessor to modify the concat config to parse out {{STATIC_URL}} using the above method
    useminPrepare: {
      html: 'app/index.html',options: {
        dest: 'dist',flow: {
          steps: {
            js: [
              {
                name: 'concat',createConfig: createDjangoStaticConcatConfig
              },'uglifyjs'
            ],// also apply it to css files
            css: [
              {
                name: 'cssmin',createConfig: createDjangoStaticConcatConfig
              }
            ]
          },// this property is necessary
          post: {}
        }
      }
    },// add a pattern to parse out the actual filename and remove the {{STATIC_URL}} bit
    usemin: {
      html: ['dist/{,*/}*.html'],css: ['dist/styles/{,*/}*.css'],options: {
        assetsDirs: ['dist'],patterns: {
          html: [[/\{\{\s*STATIC_URL\s*\}\}([^'"]*)["']/mg,'django static']]
        }
      }
    },// if you are using bower,also include the jsPattern to automatically 
    // insert {{STATIC_URL}} when inserTing js files
    'bower-install': {
      app: {
        html: 'app/index.html',jsPattern: '<script type="text/javascript" src="{{STATIC_URL}}{{filePath}}"></script>'
      }
    }
});

大佬总结

以上是大佬教程为你收集整理的如何告诉grunt-usemin忽略Django的静态模板标签?全部内容,希望文章能够帮你解决如何告诉grunt-usemin忽略Django的静态模板标签?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。