问题描述
我有一个项目,其中 src / main / webapp / com / mycompany / frontend / page / index.js
取决于 target / webjars / log4javascript / 1.4.10 / log4javascript.js
。
我添加了 package.json
和 index.js
一起放入以下内容:
{
浏览器:
{
log4javascript:../../../../../../../target/webjars/log4javascript/ 1.4.10 / log4javascript.js
}
我已经在目标
目录中有许多其他依赖项。有没有办法避免为每个依赖重复 ../../../../../../../target/
?
查看。
您可以使用grunt -browserify的aliasMapping选项指定应用程序的根目录:
aliasMappings:[{
cwd:'src',
dest:'myApp',
src:['** / *。js']
}]
然后您可以直接引用根路径中的所有内容,而不必使用任何可怕的 ../
的:
require(myApp / target / webjars / log4javascript / 1.4.10 / log4javascript.js)
code>
当然,这并不能解决问题,它仍然是一条很长的路。
这篇文章的下一段提到了一个很好的观点:如果你在应用程序的另一端调用了这样的方法,这是一个很好的迹象,表明事物可能没有正确构建。
你能分割功能吗进入更小的模块?也许让log4javascript成为自己的模块?
添加到我的答案中,从以下讨论:
如果log4javascript作为浏览器(非NPM)模块存在于package.json文件中,那么您应该只需要 require('log4javascript')
I've got a project where src/main/webapp/com/mycompany/frontend/page/index.js
depends on target/webjars/log4javascript/1.4.10/log4javascript.js
.
I've added package.json
alongside index.js
with the following contents:
{
"browser":
{
"log4javascript": "../../../../../../../target/webjars/log4javascript/1.4.10/log4javascript.js"
}
}
I've got many other dependencies in the target
directory. Is there a way for me to avoid repeating ../../../../../../../target/
for every dependency?
Check out the section Using Non-Relative Paths in this article.
You can use grunt-browserify's aliasMapping option to specify the root of your app:
aliasMappings: [{
cwd: 'src',
dest: 'myApp',
src: ['**/*.js']
}]
and then you can directly refer to everything from the root path, without having to ever use any dreaded ../
's:
require("myApp/target/webjars/log4javascript/1.4.10/log4javascript.js")
Of course, this doesn't resolve the problem that it's still a very long path.
The article's next paragraph makes a very good point: if you're calling things way over at the other end of your application like that, it's a good sign that things may not be correctly architected.
Can you split the functionality into smaller modules? Perhaps make log4javascript its own module?
Add to my answer, from discussion below:
If log4javascript is in your package.json file as a browser (non-NPM) module, you should just be able to require it with require('log4javascript')
这篇关于package.json中的相对路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!