[英]intellisense in Gruntfile.js
可以在Gruntfile中运行intellisense吗?
由于'grunt'不是全局变量,而是Gruntfile VSCode中的参数,因此将假定它只是一个未知的函数参数'any'。
module.exports = function(grunt) {...}
当我将类型添加到参数时,intellisense可以正常工作,但不会咕unt作响,因为它是JS而不是TS。
module.exports = function(grunt: IGrunt) {...}
初学者的步骤...
安装Grunt的打字
npm install --save @types/grunt
找到打字文件
确保您的IDE知道此类型文件的位置:
./node_modules/@types/grunt/index.d.ts
可以在.ts或.js中实现,这取决于您的喜好...(我使用Typescript,因为编译器会进行很多类型安全检查)
打字稿中的自动补全
将现有的gruntfile.js重命名为gruntfile.ts。
添加IGrunt类型以启用自动完成:
module.exports = function (grunt: IGrunt) {
// auto-complete works for grunt-dot now
...
}
最后,请记住编译您的gruntfile.ts,以便生成新的gruntfile.js。
JavaScript自动补全
如果您不想使用打字稿...。
使用jsdoc在javascript中启用自动完成功能:
/**
*
* @param {IGrunt} grunt
*/
module.exports = function (grunt) {
// auto-complete works for grunt-dot now
...
}
您可以创建gruntfile.ts
并将TypeScript与intellisense结合使用。 然后,编译的版本( gruntfile.js
)将由GruntJs使用。
如果您要坚持使用JavaScript,它将与您的JavaScript一样好运行,但是intellisense的效果与grunt: IGrunt
一样好grunt: IGrunt
:
/**
* @param {IGrunt} grunt
*/
module.exports = function (grunt) {...}
(为什么是的,已经有 另一种答案是建议这样做,但我想讲清楚,这可能是所有你需要做的。)
在Visual Studio Code中,如果将typescript.disableAutomaticTypeAcquisition
设置为true
(或者该功能遇到某种障碍)并且在package.json
没有@types/grunt
,则此方法将不起作用,但是如果其中任何一个在这种情况下, grunt: IGrunt
似乎也不起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.