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

当使用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,该功能应该是“从一组路径段”修剪和.

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

我可以做些什么来消除相对路径等同于绝对路径的其他重复项?

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

更新:

这就是我的项目结构:

.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.coffee和app / 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.

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...