我在 grunt 方面遇到了一些问题,因为它似乎做了比我想要的更多的事情。我有两个文件夹,其中有两个本地 grunt,但是当我从一个文件夹本地启动它时,它也执行另一个文件夹中的“其他”grunt 应该执行的操作。我在每个文件夹中都有一个包含不同代码的 index.html 文件,导致不同的 JavaScript (.js) 文件,但 grunt 会执行这两个文件。 (首先是另一个文件夹中的那个,然后是我启动它的文件夹中的那个。)
我目前不会发布任何代码,因为我觉得这个问题应该相对与代码无关,但如果有人认为有必要回答这个问题,我会做的。
您认为问题是否源于我在本地 grunt 之前安装的全局 grunt? (即它搜索所有文件并执行指令?)
有什么提示、想法、建议吗?谢谢。
编辑:更好地描述我的文件结构。在桌面上,我有两个文件夹(项目),其中安装了两个本地 grunt。在第一个项目中,index.html是:
<html>
<head><title>Testing grunt</title></head>
<body>
<script src="scripts/app.js"></script>
<h1>It works!</h1>
</body>
</html>
这里,app.js 调用另一个 .js 文件来触发警报消息。
这作为测试工作正常。但在第二个项目(文件夹)中,它也在桌面中,index.html 是:
<!DOCTYPE html>
<html>
<head>
<title>Cloud clicker game</title>
</head>
<body>
<h1>Hello!!!</h1>
<div id="game"></div>
<script src="scripts/phaser.min.js"></script>
<script src="scripts/app.js"></script>
</body>
</html>
导致一个不同的 app.js 文件,这是我使用 Phaser.js 库制作的一个小示例游戏。
问题是,当我从“game”文件夹启动 grunt 时,都会出现“Hello!!!”并且显示第一个文件夹中的警报,而不是游戏文件夹中应由 grunt 启动的游戏。游戏没有开始,但这是另一个问题。首先,我需要摆脱警报(即正在启动的“其他”index.html)
希望这有助于更好地理解问题/情况。
编辑:这是我的 gruntfile.js
module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
browserify: {
main: {
options: {
browserifyOptions: {
debug: true
},
transform: [["babelify", { "presets": ["es2015"] }]]
},
src: 'src/app.js',
dest: 'scripts/app.js'
}
},
watch: {
files: [ 'src/**/*.js' ],
tasks: ['browserify'],
options: {
spawn: false,
},
},
connect: {
target:{
options: {
port: 9001
}
}
}
});
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-browserify');
grunt.registerTask('default', [ 'connect', 'watch']);
};
编辑:grunt --verbose 的输出
grunt --verbose Initializing
Command-line options: --verbose
Reading "gruntfile.js" Gruntfile...OK
Registering Gruntfile tasks.
Reading package.json...OK
Parsing package.json...OK
Initializing config...OK
Registering "grunt-contrib-connect" local Npm module tasks.
Reading /home/alessandro/Desktop/Cloud_Clicker_Game/node_modules/grunt-contrib-connect/package.json...OK
Parsing /home/alessandro/Desktop/Cloud_Clicker_Game/node_modules/grunt-contrib-connect/package.json...OK
Loading "connect.js" tasks...OK
+ connect
Registering "grunt-contrib-watch" local Npm module tasks.
Reading /home/alessandro/Desktop/Cloud_Clicker_Game/node_modules/grunt-contrib-watch/package.json...OK
Parsing /home/alessandro/Desktop/Cloud_Clicker_Game/node_modules/grunt-contrib-watch/package.json...OK
Loading "watch.js" tasks...OK
+ watch
Registering "grunt-browserify" local Npm module tasks.
Reading /home/alessandro/Desktop/Cloud_Clicker_Game/node_modules/grunt-browserify/package.json...OK
Parsing /home/alessandro/Desktop/Cloud_Clicker_Game/node_modules/grunt-browserify/package.json...OK
Loading "browserify.js" tasks...OK
+ browserify
Loading "gruntfile.js" tasks...OK
+ default
No tasks specified, running default tasks.
Running tasks: default
Running "default" task
Running "connect" task
Running "connect:target" (connect) task
Verifying property connect.target exists in config...OK
File: [no files]
Options: protocol="http", port=9001, hostname="0.0.0.0", base=".", directory=null, keepalive=false, debug=false, livereload=false, open=false, useAvailablePort=false, onCreateServer=null, middleware=null
Reading /home/alessandro/Desktop/Cloud_Clicker_Game/node_modules/grunt-contrib-connect/tasks/certs/server.key...OK
Reading /home/alessandro/Desktop/Cloud_Clicker_Game/node_modules/grunt-contrib-connect/tasks/certs/server.crt...OK
Reading /home/alessandro/Desktop/Cloud_Clicker_Game/node_modules/grunt-contrib-connect/tasks/certs/ca.crt...OK
Started connect web server on http://localhost:9001
Running "watch" task
Waiting...
Verifying property watch exists in config...OK
Watching src/app.js for changes.
最佳答案
因此,您实际上从未运行过 browserify
任务。当您在命令行上运行 grunt
而不指定特定任务时,它会运行 default
任务,在本例中仅运行 connect
并观看
。
有两种修复方法:
- 只需在命令行上调用
grunt browserify
- 将默认任务更改为:
['browserify', 'connect', 'watch']
关于javascript - grunt 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41314000/