使用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

飞行需要utils.js在他们每个脚本lib与路径./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.coffeeapp/scripts/ui/orange.coffee都是:

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

===============>>#1 票数:0

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

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

记录在这里

===============>>#2 票数:0

我看到你的代码中的一些东西引起了问题。 我知道这个问题是在三年前提出的,所以其中一些问题可能不适用于当时。 但我回过头来看看3年前的航班代码,然后回答。

  1. 为什么要为grunt插件提供requireLib 您不应该这样做,因为grunt插件应该加载它自己的。 结果将是两个需要libs加载,这只是令人困惑。

  2. Shim配置仅适用于非AMD模块 ,但是您要同时填充“app”和“index”,它们都是AMD模块。 我会期待一些奇怪的行为。

  3. 为了修复#2并使代码更简单,您可以完全删除垫片并简单地映射相关的所有“飞行”:

     # 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': '../../components/flight/lib' require ['jquery', 'es5shim', 'es5sham'], (App) -> require ['app'], (App) -> App.initialize() # app.coffee define [ 'flight/index', 'ui/apple', 'ui/orange' ], (Apple, Orange) -> initialize = -> Apple.attachTo document Orange.attachTo document return initialize: initialize 

我不确定这是否能解决您的问题,所以如果您可以上传一个带有相关代码的小型zip文件,这将有很大帮助。

  ask by maxbeatty translate from so

未解决问题?本站智能推荐: