Javascript grunt包含源不';行不通
我试着简单地使用-但我没有成功- 我的Grunfile是-Javascript grunt包含源不';行不通,javascript,node.js,gruntjs,Javascript,Node.js,Gruntjs,我试着简单地使用-但我没有成功- 我的Grunfile是- module.exports = function (grunt) { grunt.initConfig({ includeSource: { options: { basePath: 'app/file1.js' }, myTarget: { files: { 'dist/index.html': 'a
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>
我运行gruntserve
并在dist>index.html
文件夹中获取输出-
<html>
<head>
</head>
<body>
</body>
</html>
没有预期的-
我一直跟在后面,在这
为什么我没有得到预期的输出 您的代码有两个问题,第一个是在Grunfile中指定了错误的路径,第二个是在html中指定了错误的源代码 让我们按部分进行,在您的GrunFile上,您有以下内容:
...
includeSource: {
options: {
basePath: 'app/file1.js'
},
...
<!-- include: "type": "js", "files": "scripts/*.js" -->
文档上的basepath选项显示:
类型:字符串或数组[String]默认值:“”
展开文件时要使用的基本路径。可以是要支持的阵列
从多个路径展开文件
这使我们能够包括一个或多个路径作为脚本的基本路径。因此,让我们将基本路径更改为basePath:“app”
,我们的grunfile.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-service
它将不起作用,为什么?因为在index.html上有以下内容:
...
includeSource: {
options: {
basePath: 'app/file1.js'
},
...
<!-- include: "type": "js", "files": "scripts/*.js" -->
运行grunt-service
et-voilá您的index.html已更改为:
<html>
<head>
</head>
<body>
<script src="file1.js"></script>
</body>
</html>
现在您只需将
file1.js
从app/
复制到dist/
即可发布完整的Grunfile文件。这是整个文件,是否有选项指示特定的js文件?@URL87您可以将index.html文件更改为