<script src="<%= ... %> "/> 这行如何变成正确的 html 行?
How is the line <script src="<%= ... %> "/> turned into a proper line of html?
在示例 Angular 应用中,angular-app
, on GitHub, the index.html
file contains the line
<script type="text/javascript" src="/static/<%= grunt.config.get('pkg.name') %>.js"></script>
当我尝试 运行 我的网络服务器中的项目时,此行有问题并且无法显示任何内容。
显然我必须先用 G运行t 构建项目才能使它工作。但在我或任何未来的读者有机会深入学习 G运行t 之前,有人可以解释一下 the Grunt file 如何将这条线变成它需要的样子吗?
编辑
根据@JBNizet 的评论,这里是 HTML 代码和 G运行js 代码:
<!DOCTYPE html>
<html lang="en" ng-app="app">
<head>
<link rel="stylesheet" type="text/css" href="/static/<%= grunt.config.get('pkg.name') %>.css"/>
<script type="text/javascript" src="/static/jquery.js"></script>
<script type="text/javascript" src="/static/angular.js"></script>
<script type="text/javascript" src="/static/mongolab.js"></script>
<script type="text/javascript" src="/static/bootstrap.js"></script>
<script type="text/javascript" src="/static/<%= grunt.config.get('pkg.name') %>.js"></script>
</head>
<body ng-controller="AppCtrl">
<div ng-include="'header.tpl.html'"></div>
<div ng-include="'notifications.tpl.html'" class="container-fluid" ng-show="notifications.getCurrent().length"></div>
<div ng-view class="container-fluid"></div>
</body>
</html>
G运行t 文件:
module.exports = function (grunt) {
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-recess');
grunt.loadNpmTasks('grunt-karma');
grunt.loadNpmTasks('grunt-html2js');
// Default task.
grunt.registerTask('default', ['jshint','build','karma:unit']);
grunt.registerTask('build', ['clean','html2js','concat','recess:build','copy:assets']);
grunt.registerTask('release', ['clean','html2js','uglify','jshint','karma:unit','concat:index', 'recess:min','copy:assets']);
grunt.registerTask('test-watch', ['karma:watch']);
// Print a timestamp (useful for when watching)
grunt.registerTask('timestamp', function() {
grunt.log.subhead(Date());
});
var karmaConfig = function(configFile, customOptions) {
var options = { configFile: configFile, keepalive: true };
var travisOptions = process.env.TRAVIS && { browsers: ['Firefox'], reporters: 'dots' };
return grunt.util._.extend(options, customOptions, travisOptions);
};
// Project configuration.
grunt.initConfig({
distdir: 'dist',
pkg: grunt.file.readJSON('package.json'),
banner:
'/*! <%= pkg.title || pkg.name %> - v<%= pkg.version %> - <%= grunt.template.today("yyyy-mm-dd") %>\n' +
'<%= pkg.homepage ? " * " + pkg.homepage + "\n" : "" %>' +
' * Copyright (c) <%= grunt.template.today("yyyy") %> <%= pkg.author %>;\n' +
' * Licensed <%= _.pluck(pkg.licenses, "type").join(", ") %>\n */\n',
src: {
js: ['src/**/*.js'],
jsTpl: ['<%= distdir %>/templates/**/*.js'],
specs: ['test/**/*.spec.js'],
scenarios: ['test/**/*.scenario.js'],
html: ['src/index.html'],
tpl: {
app: ['src/app/**/*.tpl.html'],
common: ['src/common/**/*.tpl.html']
},
less: ['src/less/stylesheet.less'], // recess:build doesn't accept ** in its file patterns
lessWatch: ['src/less/**/*.less']
},
clean: ['<%= distdir %>/*'],
copy: {
assets: {
files: [{ dest: '<%= distdir %>', src : '**', expand: true, cwd: 'src/assets/' }]
}
},
karma: {
unit: { options: karmaConfig('test/config/unit.js') },
watch: { options: karmaConfig('test/config/unit.js', { singleRun:false, autoWatch: true}) }
},
html2js: {
app: {
options: {
base: 'src/app'
},
src: ['<%= src.tpl.app %>'],
dest: '<%= distdir %>/templates/app.js',
module: 'templates.app'
},
common: {
options: {
base: 'src/common'
},
src: ['<%= src.tpl.common %>'],
dest: '<%= distdir %>/templates/common.js',
module: 'templates.common'
}
},
concat:{
dist:{
options: {
banner: "<%= banner %>"
},
src:['<%= src.js %>', '<%= src.jsTpl %>'],
dest:'<%= distdir %>/<%= pkg.name %>.js'
},
index: {
src: ['src/index.html'],
dest: '<%= distdir %>/index.html',
options: {
process: true
}
},
angular: {
src:['vendor/angular/angular.js', 'vendor/angular/angular-route.js'],
dest: '<%= distdir %>/angular.js'
},
mongo: {
src:['vendor/mongolab/*.js'],
dest: '<%= distdir %>/mongolab.js'
},
bootstrap: {
src:['vendor/angular-ui/bootstrap/*.js'],
dest: '<%= distdir %>/bootstrap.js'
},
jquery: {
src:['vendor/jquery/*.js'],
dest: '<%= distdir %>/jquery.js'
}
},
uglify: {
dist:{
options: {
banner: "<%= banner %>"
},
src:['<%= src.js %>' ,'<%= src.jsTpl %>'],
dest:'<%= distdir %>/<%= pkg.name %>.js'
},
angular: {
src:['<%= concat.angular.src %>'],
dest: '<%= distdir %>/angular.js'
},
mongo: {
src:['vendor/mongolab/*.js'],
dest: '<%= distdir %>/mongolab.js'
},
bootstrap: {
src:['vendor/angular-ui/bootstrap/*.js'],
dest: '<%= distdir %>/bootstrap.js'
},
jquery: {
src:['vendor/jquery/*.js'],
dest: '<%= distdir %>/jquery.js'
}
},
recess: {
build: {
files: {
'<%= distdir %>/<%= pkg.name %>.css':
['<%= src.less %>'] },
options: {
compile: true
}
},
min: {
files: {
'<%= distdir %>/<%= pkg.name %>.css': ['<%= src.less %>']
},
options: {
compress: true
}
}
},
watch:{
all: {
files:['<%= src.js %>', '<%= src.specs %>', '<%= src.lessWatch %>', '<%= src.tpl.app %>', '<%= src.tpl.common %>', '<%= src.html %>'],
tasks:['default','timestamp']
},
build: {
files:['<%= src.js %>', '<%= src.specs %>', '<%= src.lessWatch %>', '<%= src.tpl.app %>', '<%= src.tpl.common %>', '<%= src.html %>'],
tasks:['build','timestamp']
}
},
jshint:{
files:['gruntFile.js', '<%= src.js %>', '<%= src.jsTpl %>', '<%= src.specs %>', '<%= src.scenarios %>'],
options:{
curly:true,
eqeqeq:true,
immed:true,
latedef:true,
newcap:true,
noarg:true,
sub:true,
boss:true,
eqnull:true,
globals:{}
}
}
});
};
索引文件被一个 grunt 任务解析,可能 grunt build
,<%= grunt.config.get('pkg.name') %>
部分被替换为包的名称。
grunt.config.get('pkg.name')
就是 JavaScript.
将其更改为 console.log(grunt.config.get('pkg.name'))
看看会发生什么。
具体来说,是 concat
执行模板的 grunt 任务或在 client/src/index.html
中找到的内容,将其放置在 client/dist/index.html`.
在示例 Angular 应用中,angular-app
, on GitHub, the index.html
file contains the line
<script type="text/javascript" src="/static/<%= grunt.config.get('pkg.name') %>.js"></script>
当我尝试 运行 我的网络服务器中的项目时,此行有问题并且无法显示任何内容。
显然我必须先用 G运行t 构建项目才能使它工作。但在我或任何未来的读者有机会深入学习 G运行t 之前,有人可以解释一下 the Grunt file 如何将这条线变成它需要的样子吗?
编辑
根据@JBNizet 的评论,这里是 HTML 代码和 G运行js 代码:
<!DOCTYPE html>
<html lang="en" ng-app="app">
<head>
<link rel="stylesheet" type="text/css" href="/static/<%= grunt.config.get('pkg.name') %>.css"/>
<script type="text/javascript" src="/static/jquery.js"></script>
<script type="text/javascript" src="/static/angular.js"></script>
<script type="text/javascript" src="/static/mongolab.js"></script>
<script type="text/javascript" src="/static/bootstrap.js"></script>
<script type="text/javascript" src="/static/<%= grunt.config.get('pkg.name') %>.js"></script>
</head>
<body ng-controller="AppCtrl">
<div ng-include="'header.tpl.html'"></div>
<div ng-include="'notifications.tpl.html'" class="container-fluid" ng-show="notifications.getCurrent().length"></div>
<div ng-view class="container-fluid"></div>
</body>
</html>
G运行t 文件:
module.exports = function (grunt) {
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-recess');
grunt.loadNpmTasks('grunt-karma');
grunt.loadNpmTasks('grunt-html2js');
// Default task.
grunt.registerTask('default', ['jshint','build','karma:unit']);
grunt.registerTask('build', ['clean','html2js','concat','recess:build','copy:assets']);
grunt.registerTask('release', ['clean','html2js','uglify','jshint','karma:unit','concat:index', 'recess:min','copy:assets']);
grunt.registerTask('test-watch', ['karma:watch']);
// Print a timestamp (useful for when watching)
grunt.registerTask('timestamp', function() {
grunt.log.subhead(Date());
});
var karmaConfig = function(configFile, customOptions) {
var options = { configFile: configFile, keepalive: true };
var travisOptions = process.env.TRAVIS && { browsers: ['Firefox'], reporters: 'dots' };
return grunt.util._.extend(options, customOptions, travisOptions);
};
// Project configuration.
grunt.initConfig({
distdir: 'dist',
pkg: grunt.file.readJSON('package.json'),
banner:
'/*! <%= pkg.title || pkg.name %> - v<%= pkg.version %> - <%= grunt.template.today("yyyy-mm-dd") %>\n' +
'<%= pkg.homepage ? " * " + pkg.homepage + "\n" : "" %>' +
' * Copyright (c) <%= grunt.template.today("yyyy") %> <%= pkg.author %>;\n' +
' * Licensed <%= _.pluck(pkg.licenses, "type").join(", ") %>\n */\n',
src: {
js: ['src/**/*.js'],
jsTpl: ['<%= distdir %>/templates/**/*.js'],
specs: ['test/**/*.spec.js'],
scenarios: ['test/**/*.scenario.js'],
html: ['src/index.html'],
tpl: {
app: ['src/app/**/*.tpl.html'],
common: ['src/common/**/*.tpl.html']
},
less: ['src/less/stylesheet.less'], // recess:build doesn't accept ** in its file patterns
lessWatch: ['src/less/**/*.less']
},
clean: ['<%= distdir %>/*'],
copy: {
assets: {
files: [{ dest: '<%= distdir %>', src : '**', expand: true, cwd: 'src/assets/' }]
}
},
karma: {
unit: { options: karmaConfig('test/config/unit.js') },
watch: { options: karmaConfig('test/config/unit.js', { singleRun:false, autoWatch: true}) }
},
html2js: {
app: {
options: {
base: 'src/app'
},
src: ['<%= src.tpl.app %>'],
dest: '<%= distdir %>/templates/app.js',
module: 'templates.app'
},
common: {
options: {
base: 'src/common'
},
src: ['<%= src.tpl.common %>'],
dest: '<%= distdir %>/templates/common.js',
module: 'templates.common'
}
},
concat:{
dist:{
options: {
banner: "<%= banner %>"
},
src:['<%= src.js %>', '<%= src.jsTpl %>'],
dest:'<%= distdir %>/<%= pkg.name %>.js'
},
index: {
src: ['src/index.html'],
dest: '<%= distdir %>/index.html',
options: {
process: true
}
},
angular: {
src:['vendor/angular/angular.js', 'vendor/angular/angular-route.js'],
dest: '<%= distdir %>/angular.js'
},
mongo: {
src:['vendor/mongolab/*.js'],
dest: '<%= distdir %>/mongolab.js'
},
bootstrap: {
src:['vendor/angular-ui/bootstrap/*.js'],
dest: '<%= distdir %>/bootstrap.js'
},
jquery: {
src:['vendor/jquery/*.js'],
dest: '<%= distdir %>/jquery.js'
}
},
uglify: {
dist:{
options: {
banner: "<%= banner %>"
},
src:['<%= src.js %>' ,'<%= src.jsTpl %>'],
dest:'<%= distdir %>/<%= pkg.name %>.js'
},
angular: {
src:['<%= concat.angular.src %>'],
dest: '<%= distdir %>/angular.js'
},
mongo: {
src:['vendor/mongolab/*.js'],
dest: '<%= distdir %>/mongolab.js'
},
bootstrap: {
src:['vendor/angular-ui/bootstrap/*.js'],
dest: '<%= distdir %>/bootstrap.js'
},
jquery: {
src:['vendor/jquery/*.js'],
dest: '<%= distdir %>/jquery.js'
}
},
recess: {
build: {
files: {
'<%= distdir %>/<%= pkg.name %>.css':
['<%= src.less %>'] },
options: {
compile: true
}
},
min: {
files: {
'<%= distdir %>/<%= pkg.name %>.css': ['<%= src.less %>']
},
options: {
compress: true
}
}
},
watch:{
all: {
files:['<%= src.js %>', '<%= src.specs %>', '<%= src.lessWatch %>', '<%= src.tpl.app %>', '<%= src.tpl.common %>', '<%= src.html %>'],
tasks:['default','timestamp']
},
build: {
files:['<%= src.js %>', '<%= src.specs %>', '<%= src.lessWatch %>', '<%= src.tpl.app %>', '<%= src.tpl.common %>', '<%= src.html %>'],
tasks:['build','timestamp']
}
},
jshint:{
files:['gruntFile.js', '<%= src.js %>', '<%= src.jsTpl %>', '<%= src.specs %>', '<%= src.scenarios %>'],
options:{
curly:true,
eqeqeq:true,
immed:true,
latedef:true,
newcap:true,
noarg:true,
sub:true,
boss:true,
eqnull:true,
globals:{}
}
}
});
};
索引文件被一个 grunt 任务解析,可能 grunt build
,<%= grunt.config.get('pkg.name') %>
部分被替换为包的名称。
grunt.config.get('pkg.name')
就是 JavaScript.
将其更改为 console.log(grunt.config.get('pkg.name'))
看看会发生什么。
具体来说,是 concat
执行模板的 grunt 任务或在 client/src/index.html
中找到的内容,将其放置在 client/dist/index.html`.