gpt4 book ai didi

javascript - 如何消除由相对路径引起的重复要求?

转载 作者:可可西里 更新时间:2023-11-01 02:48:56 28 4
gpt4 key购买 nike

当使用 grunt-contrib-requirejs 任务优化我的 require.js 项目时,由于相对路径,许多脚本需要多次。以下是构建期间输出的依赖项列表:

components/requirejs/require.js
.tmp/scripts/../../components/flight/lib/././utils.js
.tmp/scripts/../../components/flight/lib/./././utils.js
.tmp/scripts/../../components/flight/lib/././../tools/debug/../../lib/./utils.js
.tmp/scripts/../../components/flight/lib/././../tools/debug/../../lib/registry.js
.tmp/scripts/../../components/flight/lib/././../tools/debug/../../lib/utils.js
.tmp/scripts/../../components/flight/lib/././../tools/debug/debug.js
.tmp/scripts/../../components/flight/lib/././compose.js
.tmp/scripts/../../components/flight/lib/./advice.js
.tmp/scripts/../../components/flight/lib/./utils.js
.tmp/scripts/../../components/flight/lib/./../tools/debug/../../lib/./utils.js
.tmp/scripts/../../components/flight/lib/./../tools/debug/../../lib/registry.js
.tmp/scripts/../../components/flight/lib/./../tools/debug/../../lib/utils.js
.tmp/scripts/../../components/flight/lib/./../tools/debug/debug.js
.tmp/scripts/../../components/flight/lib/./compose.js
.tmp/scripts/../../components/flight/lib/./registry.js
.tmp/scripts/../../components/flight/lib/component.js

注意 utils.js 被包含了 7 次:

.tmp/scripts/../../components/flight/lib/./utils.js
.tmp/scripts/../../components/flight/lib/././utils.js
.tmp/scripts/../../components/flight/lib/./././utils.js
.tmp/scripts/../../components/flight/lib/./../tools/debug/../../lib/utils.js
.tmp/scripts/../../components/flight/lib/./../tools/debug/../../lib/./utils.js
.tmp/scripts/../../components/flight/lib/././../tools/debug/../../lib/utils.js
.tmp/scripts/../../components/flight/lib/././../tools/debug/../../lib/./utils.js

Flight在它们的 lib 中的每个脚本中都需要 utils.js 路径为 ./util 并且有时需要其他依赖项,然后需要 。/util 再次。

grunt-contrib-requirejs将他们的选项直接传递给 requirejs其中包括一个函数 trimDots这应该是“[ trim ] . 和 .. 来自一组路径段。”

为什么不处理一些明显的重复项?

我该怎么做才能消除相对路径等同于相同绝对路径的其他重复项?

如果相对路径规范化为绝对路径,一切都会很好。

更新:

这就是我的项目的结构:

.tmp/scripts/ (where coffeescript is compiled)
app/scripts/ (coffeescript source)
components/ (bower components)
dist/ (where optimized code is output)
Gruntfile.coffee (requirejs config)

这是我的 Gruntfile 中的 requirejs 配置:

requirejs:
dist:
options:
baseUrl: '.tmp/scripts'
# paths relative to baseUrl
paths:
requireLib: '../../components/requirejs/require'
include: 'requireLib'
optimize: 'uglify2'
generateSourceMaps: true
preserveLicenseComments: false
useStrict: true
wrap: true
name: 'main'
out: 'dist/main.js'
mainConfigFile: '.tmp/scripts/main.js'

这是 app/scripts/main.coffee 中的内容:

require.config
paths:
# required dependencies
jquery: '../../components/jquery/jquery'
es5shim: '../../components/es5-shim/es5-shim'
es5sham: '../../components/es5-shim/es5-sham'
# plugins
text: '../../components/requirejs-text/text'
pickadate: '../../components/pickadate/source/pickadate.legacy'
map:
'*':
'flight/component': '../../components/flight/lib/component'
shim:
'../../components/flight/lib/index':
deps: ['jquery', 'es5shim', 'es5sham']
'app':
deps: ['../../components/flight/lib/index']

require ['app'], (App) ->
App.initialize()

这是 app/scripts/app.coffee 中的内容:

define [
'ui/apple',
'ui/orange'
], (Apple, Orange) ->
initialize = ->
Apple.attachTo document
Orange.attachTo document
return

initialize: initialize

app/scripts/ui/apple.coffeeapp/scripts/ui/orange.coffee 都是:

"use strict"
define ['flight/component'], (defineComponent) ->
apple = ->
# stuff
defineComponent apple

最佳答案

尝试在 grunt-contrib-requirejs 选项中设置 baseUrl:

requirejs: {
compile: {
options: {
baseUrl: "path/to/base"
}
}
}

已记录 here .

关于javascript - 如何消除由相对路径引起的重复要求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16026592/

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