如何消除相对路径引起的重复需求?

max*_*tty 11 javascript requirejs gruntjs

使用该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
Run Code Online (Sandbox Code Playgroud)

请注意如何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
Run Code Online (Sandbox Code Playgroud)

飞行需要utils.js在其中的每个脚本中lib使用路径,./util并且有时需要其他依赖性,然后./util再次需要.

grunt-contrib-requirejs将他们的选项直接传递给包含函数的requirejstrimDots应该从路径段数组中"修剪...和...".

为什么不照顾一些明显的重复?

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

如果相对路径将标准化为绝对路径,则一切都会很好.

更新:

这就是我的项目结构:

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

这是我的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'
Run Code Online (Sandbox Code Playgroud)

这是什么 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()
Run Code Online (Sandbox Code Playgroud)

这是什么 app/scripts/app.coffee:

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

  initialize: initialize
Run Code Online (Sandbox Code Playgroud)

两者app/scripts/ui/apple.coffeeapp/scripts/ui/orange.coffee仅仅是:

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

jgi*_*ich 0

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

requirejs: {
    compile: {
        options: {
            baseUrl: "path/to/base"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

记录在这里