当前位置: 首页 > 面试题库 >

如何在npm脚本中使用“监视”?

庞书
2023-03-14
问题内容

我的package.json样子是这样的:

{
  "name": "personal_site",
  "version": "1.0.0",
  "description": "My personal website.",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "node-sass": "node-sass --output-style compressed --include-path node_modules/bourbon/app/assets/stylesheets/ --include-path node_modules/bourbon-neat/app/assets/stylesheets/ 'src/scss/styles.scss' 'dist/css/bundle.min.css'",
    "html-minifier": "html-minifier --collapse-whitespace --remove-comments --remove-attribute-quotes -o 'dist/index.html' 'src/index.html'",
    "imagemin": "imagemin src/images dist/images",
    "serve": "http-server ./dist"
  },
  "author": "Dean Gibson",
  "license": "ISC",
  "dependencies": {
    "bourbon": "^4.2.6",
    "bourbon-neat": "^1.7.4",
    "normalize-scss": "^4.0.3"
  },
  "devDependencies": {
    "html-minifier": "^1.3.0",
    "http-server": "^0.9.0",
    "node-sass": "^3.4.2"
  }
}

因此,首先,我必须分别运行每个脚本,例如npm run node-sassor npm run html- minifier等。我理想的情况是运行npm serve将执行以下操作的脚本:

  1. 运行html-minifier
  2. 运行节点ass
  3. 运行图像最小
  4. 运行http服务器
  5. 最后,观看我src文件夹中的所有内容,并在文件更改(例如,node-sass等)时运行相应的脚本。

我怎样才能最好地解决这个问题?


问题答案:

您可以使用查看目录nodemon

一个适合您的解决方案是创建三个监视脚本,每个任务一个:

  • watch:node-sass
  • watch:html-minifier
  • watch:imagemin

然后,以中央脚本watch开始这三个脚本:

{
  "name": "personal_site",
  "version": "1.0.0",
  "description": "My personal website.",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "node-sass": "node-sass --output-style compressed --include-path node_modules/bourbon/app/assets/stylesheets/ --include-path node_modules/bourbon-neat/app/assets/stylesheets/ 'src/scss/styles.scss' 'dist/css/bundle.min.css'",
    "html-minifier": "html-minifier --collapse-whitespace --remove-comments --remove-attribute-quotes -o 'dist/index.html' 'src/index.html'",
    "imagemin": "imagemin src/images dist/images",
    "serve": "http-server ./dist",
    "watch:node-sass": "nodemon -e scss -x \"npm run node-sass\"",
    "watch:html-minifier": "nodemon -e html -x \"npm run html-minifier\"",
    "watch:imagemin": "nodemon --watch src/images -x \"npm run imagemin\"",
    "watch": "npm run watch:node-sass & npm run watch:html-minifier & npm run watch:imagemin"
  },
  "author": "Dean Gibson",
  "license": "ISC",
  "dependencies": {
    "bourbon": "^4.2.6",
    "bourbon-neat": "^1.7.4",
    "normalize-scss": "^4.0.3"
  },
  "devDependencies": {
    "html-minifier": "^1.3.0",
    "http-server": "^0.9.0",
    "node-sass": "^3.4.2"
  }
}

另请阅读:如何将npm用作构建工具。



 类似资料:
  • 我有一个名为的密钥,我想在 的中访问它。 我的文件包含: 我如何访问内的的值,同时仍然保持它的秘密,因为我需要推送公开? 目前,我执行以下操作,但不能跨平台: 当运行脚本时,我会这样做: 这得益于Bash编程语言,但在Windows上不起作用。我需要用替换script中的 。但我希望它是跨平台的。还有别的办法吗?

  • 文件结构应如下所示: (包含已安装的npm模块的文件夹) 我试过这个: 错误: 回溯(最后一次调用):文件“c:\Users\Alifreeze.vscode\extensions\ms python.python-2020.1.58038\pythonFiles\ptvsd_launcher.py”,主文件(ptvsdArgs)第43行“c:\Users\Alifreeze.vscode\ext

  • 问题内容: 我的package.json文件中有一个npm任务,如下所示执行笑话测试: 我想使用grunt 执行此任务。我为此安装了grunt-run并添加了run任务,但是如何在其中调用此npm任务呢? 问题答案: 配置与文档中显示的示例类似的示例。 将的值设置为。 设置和在数组中。 Gruntfile.js 跑步 使用上面显示的配置通过CLI 运行将调用该命令。 注意:向Array 添加(或等

  • 问题内容: 我无法让npm工作。我的package.json文件有 我在与console.logs相同的文件夹中有一个build.js文件。 当我跑步 我得到错误 如果我移动build.js文件并更改package.json文件以包含一个子文件夹 然后我得到错误 怎么了 我正在复制示例文档。 问题答案: 要么 注意: 您缺少和节点命令 文件夹结构很好:

  • 我无法让npm工作。我的包裹。json文件已被删除 我有一个身材。js文件位于与控制台相同的文件夹中。日志。 当我奔跑时 我得到了错误 如果我移动建筑。js文件并更改我的包。json文件有一个子文件夹 那我就知道错误了 出什么问题了?我正在复制示例留档。

  • 为了帮助你消除开发过程中的冗余任务,请注意一些可用的 NPM 脚本。以下命令应该运行在项目的根目录下。当然,你可以使用 yarn run <command> 的方式运行下列任何命令。 npm run build 为了产品和打包来构建你的应用程序。更多信息可以在 构建你的应用程序 部分找到。 npm run dev 在开发环境中运行程序 npm run lint 静态分析所有在 src/ 和 tes