Grunt,将 html 个文件复制到构建时的脚本文件夹
Grunt, copy html files to scripts folder on build
我在 yeoman 中使用 angular-generator。在 gruntfile.js 中,/app/views
中的每个 html 文件都被复制到 dist/views
。但我喜欢将我的指令模板保存在与指令本身相同的文件夹中。
示例:
/app/scripts/widgets/mywidget.directive.js
/app/scripts/widgets/mywidget.tmpl.html
当我构建项目时,我希望 html 文件以与上面相同的文件夹结构结束。
这可能应该在 gruntfile.js 的复制部分完成。
copy: {
dist: {
files: [{
expand: true,
dot: true,
cwd: '<%= yeoman.app %>',
dest: '<%= yeoman.dist %>',
src: [
'*.{ico,png,txt}',
'*.html',
'images/{,*/}*.{webp}',
'styles/fonts/{,*/}*.*'
]
}...
我试图在 src 数组中添加这个:
'<%= yeoman.dist %>/scripts/{,*/}*.tmpl.html'
没有成功。有什么想法吗?
这是正常行为,构建后的所有文件都被复制到 dist 文件夹,因为这是标准的构建输出文件夹。
您可以做的是像这样更改配置:
copy: {
main: {
files: [{
src: ['img/**'],
dest: 'dist/img/',
filter: 'isFile',
expand: true,
flatten: true
}, {
src: ['pdf/**'],
dest: 'dist/pdf/',
filter: 'isFile',
expand: true,
flatten: true
},{
src: ['error/**'],
dest: 'dist/error/',
filter: 'isFile',
expand: true,
flatten: true
}, {
src: ['fonts/**'],
dest: 'dist/fonts/',
filter: 'isFile',
expand: true,
flatten: true
}
}
}
这种方法保留了 dist 文件夹中的旧结构。但我想知道,为什么您不使用 htmlmin 在构建时缩小和打包所有模板...
您可以将所有 .tmpl.html 从 app/scripts/* 移动到 dist/scripts/* ,使用对 gruntfile 的代码修改,如下所示。
files: [{
expand: true,
dot: true,
cwd: '<%= yeoman.app %>',
dest: '<%= yeoman.dist %>',
src: [
'*.{ico,png,txt}',
'*.html',
'views/{,*/}*.html'
]
}, {
// This block handles copying the .tmpl.html from app/scripts to dist/scripts
expand: true,
cwd: '<%= yeoman.app %>/scripts',
dest: '<%= yeoman.dist %>/scripts',
src: '{,*/}*.tmpl.html'
}
...
您还需要将新目录添加到 usemin 块,以确保 filerev 更新使其进入您的模板
usemin: {
html: ['<%= yeoman.dist %>/{,*/}*.html',
'<%= yeoman.dist %>/scripts/{,*/}*.html'],
...
您可能还想将目录添加到 htmlmin 以缩小到 html
htmlmin: {
dist: {
...
files: [
{
expand: true,
cwd: '<%= yeoman.dist %>',
src: ['*.html', 'views/{,*/}*.html', 'scripts/{,*/}*.html'],
dest: '<%= yeoman.dist %>'
}
]
}
已更新
这些脚本现在反映了将任何 .tmpl.html 从 app/scripts/*/
移动到 dist/scripts/*/
。如果您的文件夹结构在脚本内部不止一层,请将 {,*/}*.html
更改为 **/*.html
我在 yeoman 中使用 angular-generator。在 gruntfile.js 中,/app/views
中的每个 html 文件都被复制到 dist/views
。但我喜欢将我的指令模板保存在与指令本身相同的文件夹中。
示例:
/app/scripts/widgets/mywidget.directive.js
/app/scripts/widgets/mywidget.tmpl.html
当我构建项目时,我希望 html 文件以与上面相同的文件夹结构结束。
这可能应该在 gruntfile.js 的复制部分完成。
copy: {
dist: {
files: [{
expand: true,
dot: true,
cwd: '<%= yeoman.app %>',
dest: '<%= yeoman.dist %>',
src: [
'*.{ico,png,txt}',
'*.html',
'images/{,*/}*.{webp}',
'styles/fonts/{,*/}*.*'
]
}...
我试图在 src 数组中添加这个:
'<%= yeoman.dist %>/scripts/{,*/}*.tmpl.html'
没有成功。有什么想法吗?
这是正常行为,构建后的所有文件都被复制到 dist 文件夹,因为这是标准的构建输出文件夹。
您可以做的是像这样更改配置:
copy: {
main: {
files: [{
src: ['img/**'],
dest: 'dist/img/',
filter: 'isFile',
expand: true,
flatten: true
}, {
src: ['pdf/**'],
dest: 'dist/pdf/',
filter: 'isFile',
expand: true,
flatten: true
},{
src: ['error/**'],
dest: 'dist/error/',
filter: 'isFile',
expand: true,
flatten: true
}, {
src: ['fonts/**'],
dest: 'dist/fonts/',
filter: 'isFile',
expand: true,
flatten: true
}
}
}
这种方法保留了 dist 文件夹中的旧结构。但我想知道,为什么您不使用 htmlmin 在构建时缩小和打包所有模板...
您可以将所有 .tmpl.html 从 app/scripts/* 移动到 dist/scripts/* ,使用对 gruntfile 的代码修改,如下所示。
files: [{
expand: true,
dot: true,
cwd: '<%= yeoman.app %>',
dest: '<%= yeoman.dist %>',
src: [
'*.{ico,png,txt}',
'*.html',
'views/{,*/}*.html'
]
}, {
// This block handles copying the .tmpl.html from app/scripts to dist/scripts
expand: true,
cwd: '<%= yeoman.app %>/scripts',
dest: '<%= yeoman.dist %>/scripts',
src: '{,*/}*.tmpl.html'
}
...
您还需要将新目录添加到 usemin 块,以确保 filerev 更新使其进入您的模板
usemin: {
html: ['<%= yeoman.dist %>/{,*/}*.html',
'<%= yeoman.dist %>/scripts/{,*/}*.html'],
...
您可能还想将目录添加到 htmlmin 以缩小到 html
htmlmin: {
dist: {
...
files: [
{
expand: true,
cwd: '<%= yeoman.dist %>',
src: ['*.html', 'views/{,*/}*.html', 'scripts/{,*/}*.html'],
dest: '<%= yeoman.dist %>'
}
]
}
已更新
这些脚本现在反映了将任何 .tmpl.html 从 app/scripts/*/
移动到 dist/scripts/*/
。如果您的文件夹结构在脚本内部不止一层,请将 {,*/}*.html
更改为 **/*.html