grunt-include-source不起作用

我尽量简单地使用 – grunt-include-source,但是我没有成功 –

我的Gruntfile是 –

module.exports = function (grunt) { grunt.initConfig({ includeSource: { options: { basePath: 'app/file1.js' }, myTarget: { files: { 'dist/index.html': 'app/index.html' } } } }); grunt.loadNpmTasks('grunt-include-source'); grunt.registerTask('serve', function (target) { grunt.task.run('includeSource'); }); } 

index.html是 –

 <html> <head> </head> <body> <!-- include: "type": "js", "files": "scripts/*.js" --> </body> </html> 

我的文件夹层次是 –

 Gruntfile.js app > file1.js index.html dist > index.html 

我运行grunt serve并获取dist>index.html文件夹的输出 –

 <html> <head> </head> <body> </body> </html> 

没有预期 – <script src="scripts/file1.js"></script>

我继续按照文件和这个问题 ,

为什么我没有得到预期的产出?

你的代码有两个问题,第一个是gruntfile,你指定了错误的path,第二个是你指定的错误源。

让我们按部件进行操作,在你的grunt文件中有这个:

 ... includeSource: { options: { basePath: 'app/file1.js' }, ... 

文档中的basepath选项说:

types:string或数组[String]默认值:''

扩展文件时使用的基本path。 可以是一个数组来支持从多个path扩展文件。

这使得我们可以做的是将一条或多条path作为脚本的基本path。 所以让我们将basePath: 'app'更改为basePath: 'app' ,我们的Gruntfile.js将如下所示:

 module.exports = function (grunt) { grunt.initConfig({ includeSource: { options: { basePath: 'app' }, myTarget: { files: { 'dist/index.html': 'app/index.html' } } } }); grunt.loadNpmTasks('grunt-include-source'); grunt.registerTask('serve', function (target) { grunt.task.run('includeSource'); }); }; 

现在,如果我们运行grunt serve ,它将无法正常工作,为什么? 那么因为在你的index.html你有这个:

 <!-- include: "type": "js", "files": "scripts/*.js" --> 

也就是说,为脚本文件夹内的所有JavaScript文件插入脚本标记,但是我们没有任何脚本文件夹,所以这就是为什么dist/index.html是空的。 让我们将index.html更改为:

 <!-- include: "type": "js", "files": "*.js" --> 

运行grunt serve etvoilà你的index.html已经改成:

 <html> <head> </head> <body> <script src="file1.js"></script> </body> </html> 

现在你只需要将file1.js复制到dist/