gpt4 book ai didi

node.js - 有没有办法为 require 注册新的 CoffeeScript 扩展?

转载 作者:搜寻专家 更新时间:2023-11-01 00:02:42 25 4
gpt4 key购买 nike

我目前将我的测试文件称为 test.coffee.unit 用于单元测试...(产生输出 test.spec.unit)

但是,当调用 require "test.coffee.unit" Node 时, Node 将文件解释为 javascript,并且无法评估。我一直在查看文档和 https://github.com/jashkenas/coffee-script/blob/master/lib/coffee-script/coffee-script.js#L192看看我是否能找到方法,但我没有看到任何看起来有希望的东西。

我也看过https://github.com/joyent/node/blob/master/lib/module.js#L347http://nodejs.org/api/modules.html#modules_all_together看看我能从中收集到什么……

总结:

有没有办法注册 .coffee.unit 或只是 .unit 扩展,以便 require 将其评估为 CoffeeScript?

最佳答案

你有两个选择。

  1. 修改 coffee-script 源。
  2. 提取您需要的源代码部分,并修改它们以在不同的范围内工作。

对于任何一个,您都需要引用 section-19 of the coffee-script node module .

相关代码显示了如何配置 require 扩展:

if require.extensions
for ext in ['.coffee', '.litcoffee', '.coffee.md']
require.extensions[ext] = loadFile

coffee-script 用于加载文件的代码:

loadFile = (module, filename) ->
raw = fs.readFileSync filename, 'utf8'
stripped = if raw.charCodeAt(0) is 0xFEFF then raw.substring 1 else raw
answer = compile(stripped, {filename, sourceMap: true, literate: helpers.isLiterate filename})
sourceMaps[filename] = answer.sourceMap
module._compile answer.js, filename

您需要追踪相关性。大多数都在 coffee-script 模块的导出上。例如,

compile(stripped, {filename, sourceMap: true, literate: helpers.isLiterate filename})

cs = require 'coffee-script'
cs.compile(stripped, {filename, sourceMap: true, literate: cs.helpers.isLiterate filename})

非常欢迎使用完整的解决方案编辑此答案。

关于node.js - 有没有办法为 require 注册新的 CoffeeScript 扩展?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17294275/

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