微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.

原文地址:https://www.jb51.cc/js/152563.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐