【问题标题】:Browserify require file with dynamic path fails, but works when using hard-coded pathBrowserify 需要具有动态路径的文件失败,但在使用硬编码路径时有效
【发布时间】: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


    【解决方案1】:

    此行为是设计使然。您可以在herehere 找到更多信息。

    【讨论】:

    • 谢谢。但我不明白我应该怎么做。
    • 根据创作者的说法,没有解决方法; “browserify 不适用于条件需求,并且不能在不首先解决停止问题的情况下与任意动态需求一起使用。改为做一些不同的事情。这是一条死胡同。”
    • 好吧,没什么好说的,我读了这篇文章,还在努力寻找解决方案,我就是这么做的。谢谢你:)
    猜你喜欢
    • 2019-11-23
    • 2012-04-16
    • 2017-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-20
    • 1970-01-01
    • 2021-10-07
    相关资源
    最近更新 更多