2013-04-16 74 views
10

使用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需要在每個腳本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) 

這裏是我requirejs從我Gruntfile配置:

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 
+0

我的隊友挖掘到[requirejs節點模塊](https://github.com/jrburke/r.js)中包含的r.js,並確認它們沒有以消除路徑的方式規範化路徑重複我有。不知道這是有意的還是不正確的。如果他可以進一步挖掘,也許他可以在GitHub上打開一個問題。 – maxbeatty

+0

我寫了一個叫做grunt-reduce的咕嚕任務,它爲你的頁面做了一大堆優化,包括正確解析和縮小requirejs依賴關係。 如果你還可以正常運行其他優化,也許你應該試試看看你是否可以運行它:https://github.com/Munter/grunt-reduce – Munter

+1

溝渠咕嚕和槓桿webpack ...具體來說,'DedupePlugin'將完全符合您的需求。 https://github.com/webpack/docs/wiki/optimization – Maxwelll

回答

0

嘗試在咕嚕-的contrib-requirejs選項設置的baseUrl:

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

已記錄here

+0

我已將'baseUrl'設置爲'.tmp/scripts',因爲這是我的CoffeeScript編譯到的地方。我用我的項目佈局的更多細節更新了這個問題。 – maxbeatty

0

我看到一些導致問題的代碼。我知道這個問題在三年前被問過,所以當時有些問題可能不適用。但我去了back in time,並在回答之前查看了3年前的航班代碼。

  1. 爲什麼你要爲grunt插件提供requireLib?你應該不需要這樣做,因爲grunt插件應該加載它自己的插件。結果將是兩個需要libs加載,這只是令人困惑。

  2. 勻場配置旨在與non-AMD modules only一起使用,但是您正在勻稱都是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文件和相關的代碼,這將幫助很多。