[NPM] Run npm scripts when files change with onchange

本文介绍如何利用npm脚本自动执行文件变更时的代码检查与测试任务,包括自动lint代码、运行单元测试等。通过配置npm命令与工具如onchange、npm-run-all等,实现对JavaScript和CSS文件的实时监视及自动化处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

In this lesson we will look at how we can setup our npm scripts to execute when the file system has changed. Some common examples of this are automatically linting your code, running unit tests, or using a pre-processor for your CSS.

 

Install:

npm i -D onchange

 

For example, you want to watch all the js file and scss file, to check the tests and lints.

For lints:

    "lint": "npm-run-all lint:**",
    "lint:js": "eslint --cache --fix ./",
    "lint:css": "stylelint '**/*.scss' --syntax scss",
    "lint:css:fix": "stylefmt -R src/",
    "watch:lint": "onchange 'src/**/*.js' 'src/**/*.scss' -- npm run lint",

And for tests:

    "watch": "npm-run-all --parallel watch:*",
    "watch:test": "npm t -- --watch",
    "watch:lint": "onchange 'src/**/*.js' 'src/**/*.scss' -- npm run lint",

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值