問題描述
本身不是一個特定于 gulp 的問題,而是如何從 gulpfile.js 中的 package.json 文件中獲取信息;例如,我想獲取主頁或名稱并在任務中使用它.
Not a gulp-specific question per-se, but how would one get info from the package.json file within the gulpfile.js; For instance, I want to get the homepage or the name and use it in a task.
推薦答案
不要將 require('./package.json')
用于 watch 進程,就像使用 require
會將模塊解析為第一個請求的結果.
Don't use require('./package.json')
for a watch process, as using require
will resolve the module as the results of the first request.
因此,如果您正在編輯 package.json
,除非您停止監視進程并重新啟動它,否則這些編輯將不起作用.
So if you are editing your package.json
those edits won't work unless you stop your watch process and restart it.
對于 gulp watch 進程,最好在每次執行任務時重新讀取文件并解析它,方法是使用 節點的fs
方法
For a gulp watch process it would be best to re-read the file and parse it each time that your task is executed, by using node's fs
method
var fs = require('fs')
var json = JSON.parse(fs.readFileSync('./package.json'))
這篇關于Javascript:在 gulpfile.js 中獲取 package.json 數據的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!