在 Visual Studio Code 中对键绑定运行 npm 命令

Raf*_*off 4 key-bindings npm visual-studio-code

我想使用 webpack 打包保存 js 文件。

这最好使用 webpack watch 来完成。但是无所谓...

下面的答案是我谷歌搜索的结果,我希望在某些时候对某人有用。

Ale*_*lex 11

keybindings.json

{
    "key": "ctrl+shift+alt+b",
    "command": "workbench.action.terminal.sendSequence",
    "args": {
        "text": "npm run test\r"
    },
},
Run Code Online (Sandbox Code Playgroud)

  • 找到 keybindings.json。打开命令面板(通常是 ctrl + shift + P 或 cmd + shift + P),然后查找“首选项:打开键盘快捷键 (JSON)”。确保它是带有 (JSON) 的那个。 (2认同)

Raf*_*off 6

使用 npm 在 VSC 中保存时运行 webpack 捆绑……或任何其他您喜欢的 npm 命令,例如编译打字稿。

将一个添加.vscode/tasks.json到您的项目中:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "command": "npm",
    "isShellCommand": true,
    "showOutput": "never",
    "suppressTaskName": true,
    "tasks": [
        {
            "taskName": "bundle",
            "args": ["run", "bundle"],
            "isBuildCommand": true,
            "showOutput": "never"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

编辑keybindings.json(文件>首选项>键盘快捷键)。

// Place your key bindings in this file to overwrite the defaults
[
    {
        "key" : "ctrl+s",
        "command" : "workbench.action.tasks.build"
    }
]
Run Code Online (Sandbox Code Playgroud)

workbench.action.tasks.build是一个内置的 vsc 钩子。请参阅此处:https : //code.visualstudio.com/docs/customization/keybindings#_tasks

该任务也可以通过 VSC 访问

  1. Ctrl+P
  2. 类型task+ 空格
  3. 查看建议的任务或继续输入其名称