【问题标题】:how to manage file dependencies in a website如何管理网站中的文件依赖项
【发布时间】:2015-06-05 11:57:16
【问题描述】:

我正在开发一个网站,它逐渐变大了。 我正在寻找了解文件依赖关系的工具或方法。

例如

我在整个网站上都使用图像/js/css 文件,在进行任何更改或删除文件之前,我想知道有多少文件(html、aspx)使用该图像/js/css 文件

提前感谢:)

【问题讨论】:

    标签: javascript node.js gruntjs bower


    【解决方案1】:

    你可以很容易地构建这样的东西。您需要将给定的 html 解析为树并过滤掉外部依赖项。 npm 上有一个名为 deps-html 的模块。

    假设你已经安装了 nodejs,你可以安装它:

    npm install --save deps-html
    

    之后,您可以创建树并计算依赖关系:

    var deps = require('deps-html');
    var fs = require('fs');
    
    var string = fs.readFileSync('/path/to/your/html');
    // build the node tree
    var ast = deps.parse(string);
    // parse all the dependencies
    var matches = deps.extract(ast);
    // count
    console.log(matches.length)
    

    然后就可以运行了

    > node count-deps.js
    

    您还可以通过访问资产type 属性来更具体地计算单个资产:

    matches.forEach(dep => console.log(dep.type))
    

    【讨论】:

    • 感谢回复。我收到错误消息 "const parse5 = require('parse5-utils')" 我需要安装 deps-html global
    • 感谢回复。我收到一个错误消息 ** const parse5 = require('parse5-utils') ^^^^^ SyntaxError: Use of const in strict mode。在 Export.runInThisContext (vm.js:73:16) 在 Module._compile (module.js:443:25) 在 Object.Module._extensions..js (module.js:478:10) 在 Module.load (module .js:355:32) 在 Function.Module._load (module.js:310:12) 在 Module.require (module.js:365:17) 在 require (module.js:384:17) 在 Object. (C:\prpatel2305\work\fin_manag at Module._compile (module.js:460:26) at Object.Module._extensions..js (module.js:478:10)**
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-03
    • 1970-01-01
    • 1970-01-01
    • 2015-04-06
    • 1970-01-01
    • 2011-09-04
    • 1970-01-01
    相关资源
    最近更新 更多