【发布时间】:2016-03-01 17:20:21
【问题描述】:
我正在开始使用 Browserify,我正在尝试基于名为 components.coffee 的配置文件来请求一组文件。我将 React 与 CoffeeScript 一起使用,但我怀疑它与问题本身有关。
index.cjsx
path = require 'path'
componentList = require '../commons/components'
components = componentList.map (component) ->
console.log path.relative(__dirname, component)
console.log '../commons/button/button.doc.js'
console.log '../commons/button/button.doc.js' is path.relative(__dirname, component)
console.log require(path.relative(__dirname, component))
console.log require '../commons/button/button.doc.js'
require(path.relative(__dirname, component))
commons/components.coffee
module.exports = [
"../commons/button/button.doc.js"
]
删除console.log require '../commons/button/button.doc.js'行时的输出:
如果我不需要使用硬编码路径的文件,它将失败。我不解释为什么,因为硬编码路径和动态路径是相等的。
我猜它与 Browserify 相关,可能是一个缓存问题,当模块需要通过硬编码字符串时解决。但我真的不知道。
与此同时,我将使用一种解决方法,但我想了解这里发生了什么! :)
我的解决方案是改变我的设计。它现在返回一个已加载文件的数组,而不是返回一个字符串数组的 components.coffee。
commons/components.coffee
module.exports = [
require "../commons/button/button.doc.js"
]
然后,在我的 index.cjsx 中,我只是加载它:
components = require '../commons/components'
【问题讨论】:
标签: reactjs browserify