如何为特定模块配置模块回退路径?

时间:2016-06-07 08:54:38

标签: webpack

我的webpack项目取决于依赖“apple”(由名称组成)。 “apple”依赖关系依赖于NODE_PATH设置为NODE_PATH=/path/to/apple-module。如果您不熟悉这种方法,请阅读How I Work Around The require(“../../../../../../../”) Problem In NodeJS(我不认可这种方法。)

我对“apple”模块没有任何控制权。

webpack允许配置fallback路径,当设置为/path/to/apple-module时,“苹果”模块解析可以正常工作。但是,这种方法会危及整个应用程序模块的解析逻辑。

有没有办法设置仅适用于特定模块的回退路径?

以下是使用此模式的公共模块的示例:

https://github.com/firstopinion/formatter.js/blob/51c068bed4e78ba5db7f44911f7ae8ef259f692f/dist/common/formatter.js#L8-L10

var patternMatcher = require('pattern-matcher');
var inptSel = require('inpt-sel');
var utils = require('utils');

请注意,“pattern-matcher”,“inpt-sel”和“utils”是包的not dependencies。这些文件包含在./common path中。结果,我收到了一个错误:

ERROR in ./~/formatter.js/dist/common/formatter.js
Module not found: Error: Can't resolve 'pattern-matcher' in 'c:\Dev\repositories\krypton\kwak-client\node_modules\formatter.js\dist\common'
 @ ./~/formatter.js/dist/common/formatter.js 11:21-47

ERROR in ./~/formatter.js/dist/common/formatter.js
Module not found: Error: Can't resolve 'inpt-sel' in 'c:\Dev\repositories\krypton\kwak-client\node_modules\formatter.js\dist\common'
 @ ./~/formatter.js/dist/common/formatter.js 12:14-33

ERROR in ./~/formatter.js/dist/common/formatter.js
Module not found: Error: Can't resolve 'utils' in 'c:\Dev\repositories\krypton\kwak-client\node_modules\formatter.js\dist\common'
 @ ./~/formatter.js/dist/common/formatter.js 13:12-28

1 个答案:

答案 0 :(得分:-1)

不确定我是否正确,但似乎您需要依赖webpack别名,请参阅https://webpack.github.io/docs/resolving.html#aliasing