我正在构建一个Django应用程序,它服务于一个单页面的Angular应用程序。
Django serve的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“。
有没有人看过解决这个问题的办法?
发布于 2014-01-20 08:14:21
好了,我想我有一个变通办法。这是假设您希望构建的文件也指向构建资产的静态url。
这将要求您在视图上下文中定义一个STATIC_URL (您将使用src="{{STATIC_URL}}foo/bar.js"而不是src="{% static 'foo/bar.js' %}" )。我不能让{% static %}工作,除非我破解了grunt-usemin的源码。
因此,使用您的示例,这是:
<!-- 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 replacing Django {{STATIC_URL}} references
// when building config for concat and cssmin
var path = require('path');
function createDjangoStaticConcatConfig(context, block) {
var cfg = {files: []};
var staticPattern = /\{\{\s*STATIC_URL\s*\}\}/;
block.dest = block.dest.replace(staticPattern, '');
var outfile = path.join(context.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: []
};
context.inFiles.forEach(function(f) {
files.src.push(path.join(context.inDir, f.replace(staticPattern, '')));
});
cfg.files.push(files);
context.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>'
}
}
});发布于 2014-06-20 14:57:12
(我希望在谷歌搜索解决方案时找到这个问题,所以我只是分享我在这里找到的解决方案,以便其他像我一样的人也能找到它。)
我刚刚发现了一个叫做grunt-bridge的插件,它可以用static标签将静态文件包装在html模板中。文档不是很好,但我知道如何让它在我的项目中工作。
如果你有这样的脚本:
<!-- build:js({.tmp,app}) /scripts/scripts.js -->
<script src="scripts/app.js"></script>
<script src="scripts/util/util.js"></script>
...最后输出如下所示:
<script src="{% static 'adjangoappname/scripts/94223d51.scripts.js' %}"></script>使用npm install grunt-bridge --save-dev安装gruntbridge,并在Gruntfile.js的grunt.initConfig中添加如下配置:
bridge: {
distBaseHtml: {
options: {
pattern: '{% static \'adjangoappname{path}\' %}',
html: '<%= yeoman.minifiedDir %>/index.html', dest: '<%= yeoman.templateDir %>/index.html'
}
},
distIndexHtml: {
options: {
pattern: '{% static \'adjangoappname{path}\' %}',
html: '<%= yeoman.minifiedDir %>/index.html', dest: '<%= yeoman.templateDir %>/index.html'
}
},
// ... etc for each html file you want to modify
},其中<%= yeoman.minifiedDir %>是最终缩小的html文件的输出目录,<%= yeoman.minifiedDir %>是目标模板目录。将adjangoappname替换为您的django应用程序的名称或您想要的任何目录前缀。
然后在registerTask列表的最后添加grunt,如下所示:
grunt.registerTask('build', [
//...
'htmlmin',
'bridge'
]);(我认为可以使配置更紧凑,但这是我发现的第一个有效的方法)
https://stackoverflow.com/questions/18290533
复制相似问题