gpt4 book ai didi

coffeescript - 使用 browserify 获取 coffee-script 依赖树

转载 作者:行者123 更新时间:2023-12-02 01:59:43 26 4
gpt4 key购买 nike

我正在尝试使用 browserify 获取 CoffeeScript 项目的依赖树.

似乎该项目带有从命令行打印依赖树的选项 browserify -t coffeeify --deps ./script.coffee, with

脚本.咖啡

console.log 'hello'
require 'a.coffee'

出现错误(为简洁起见进行了编辑):

... Unexpected token ILLEGAL    at parseDeps (/.../module-deps/index.js:172:45)    at done (/.../browserify/node_modules/module-deps/index.js:152:13)    at applyTransforms (/.../browserify/node_modules/module-deps/index.js:135:41)    at /.../browserify/node_modules/module-deps/index.js:112:17    at fs.js:266:14    at Object.oncomplete (fs.js:107:15)

I would ideally like to automate the dependency tree creation by using the browserify API, but it is not apparent how one would accomplish this.

It seemed reasonable to begin working backwards from module-deps, but it is not apparent that it was designed to walk the dependency tree for a coffee-script project. I did not a related GitHub pull request, however. In this light, my hope was to inject a browserify transform and read the dependencies for each file after browserify had already transformed it into javascript from coffee-script, however module-deps seems only to take filenames and not javascript source itself. Something like the following:

require('browserify')
.add('./script.coffee')
.transform(require('coffeeify')
.transform((file) ->
data = ''
write = (buf) -> data += buf
end = ->
## parse data for dependencies.
console.log "File ", file, "dependencies", mdeps(data)
@queue(data)
@queue(null)
return through(write, end)
)

这不起作用,因为据我所知,mdeps 函数不存在。我希望它会涉及使用其中一种解析器(esprima 等)来查找 require 调用。

有没有人使用 browserify 完成打印 coffee-script 依赖树?看起来它会非常有用,如果还没有人使用它,我会感到惊讶。

最佳答案

似乎有一个 bug in browserify这会阻止 deps() 调用获取转换,因此 deps 正在尝试解析原始咖啡而不是编译后的 js。

针对您的情况的解决方法是执行以下操作:

var coffeeify = require('coffeeify');
require('browserify')
.add('./script.coffee')
.transform(coffeeify)
.deps({transform: [coffeeify]});

即自己将转换列表传递给 deps()

关于coffeescript - 使用 browserify 获取 coffee-script 依赖树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17881692/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com