2013-04-16 19 views
10

Khi tối ưu hóa dự án require.js của tôi bằng cách sử dụng tác vụ grunt-contrib-requirejs, nhiều tập lệnh được yêu cầu nhiều lần do đường dẫn tương đối. Dưới đây là danh sách các phụ thuộc outputted trong xây dựng:Làm cách nào để loại bỏ các yêu cầu trùng lặp do đường dẫn tương đối gây ra?

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 

Chú ý cách utils.js được bao gồm 7 lần:

.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 đòi hỏi utils.js trong mỗi kịch bản trong lib của họ với con đường của ./util và đôi khi đòi hỏi các phụ thuộc khác sau đó yêu cầu ./util một lần nữa.

grunt-contrib-requirejs chuyển các tùy chọn của họ trực tiếp đến requirejs bao gồm chức năng trimDots được cho là "[trim]. Và .. từ một mảng phân đoạn đường dẫn".

Tại sao việc này không quan tâm đến một số bản sao rõ ràng?

Tôi có thể làm gì để loại bỏ các bản sao khác trong đó đường dẫn tương đối tương đương với cùng một đường dẫn tuyệt đối?

Nếu đường dẫn tương đối bình thường hóa với đường dẫn tuyệt đối, tất cả sẽ tốt.

Cập nhật:

Đây là cách dự án của tôi được cấu trúc:

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

Dưới đây là requirejs của tôi cấu hình từ Gruntfile tôi:

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' 

Dưới đây là những gì trong 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() 

Dưới đây là những gì trong app/scripts/app.coffee:

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

    initialize: initialize 

Cả app/scripts/ui/apple.coffeeapp/scripts/ui/orange.coffee chỉ đơn giản là:

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

Đồng đội của tôi đào sâu vào r.js được bao gồm trong mô-đun nút [requirejs] (https://github.com/jrburke/r.js) và xác nhận rằng họ không chuẩn hóa các đường dẫn theo cách sẽ loại bỏ bản sao tôi có. Không chắc chắn nếu điều này là dự định hoặc không chính xác. Nếu anh ta có thể đào sâu hơn, có lẽ anh ta có thể mở một vấn đề trên GitHub. – maxbeatty

+0

Tôi đã viết một tác vụ grunt được gọi là grunt-reduce, thực hiện một loạt các tối ưu hóa cho trang của bạn, bao gồm giải quyết chính xác và rút gọn các phụ thuộc requirejs. Nếu bạn không sao với các tối ưu hóa khác đang chạy, có thể bạn nên thử xem liệu bạn có thể chạy nó ra khỏi hộp hay không: https://github.com/Munter/grunt-reduce – Munter

+1

mương grunt và đòn bẩy webpack ... Cụ thể là 'DedupePlugin' sẽ làm chính xác những gì bạn cần. https://github.com/webpack/docs/wiki/optimization – Maxwelll

Trả lời

0

Hãy thử thiết lập baseUrl trong các tùy chọn grunt-contrib-requirejs:

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

Tài liệu here.

+0

Tôi đã đặt 'baseUrl' thành' .tmp/scripts' vì đó là nơi mà CoffeeScript của tôi được biên dịch. Tôi đã cập nhật câu hỏi với chi tiết hơn về bố cục dự án của mình. – maxbeatty

0

Tôi thấy một vài điều với mã của bạn gây ra câu hỏi. Tôi biết câu hỏi này đã được hỏi hơn ba năm trước vì vậy một số trong những mối quan tâm này có thể đã không được áp dụng trở lại sau đó. Nhưng tôi đã đi back in time và xem mã chuyến bay từ 3 năm trước trước khi trả lời.

  1. Tại sao bạn cung cấp requireLib cho plugin grunt? Bạn không cần phải làm điều đó vì plugin grunt nên tải riêng của nó. Kết quả sẽ là hai yêu cầu tải libs, mà chỉ là khó hiểu.

  2. Cấu hình Shim có nghĩa là được sử dụng với non-AMD modules only, tuy nhiên bạn đang làm mờ cả "ứng dụng" và chỉ mục "chuyến bay", cả hai mô-đun AMD. Tôi sẽ mong đợi một số hành vi kỳ lạ từ này.

  3. Để khắc phụC# 2 và để làm cho mã của bạn đơn giản hơn, bạn có thể loại bỏ các shim hoàn toàn và chỉ cần lập bản đồ tất cả mọi thứ "bay" liên quan:

    # 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 
    

Tôi không chắc chắn nếu điều này sẽ khắc phục sự cố của bạn, vì vậy nếu bạn có thể tải lên một tệp zip nhỏ có mã có liên quan sẽ giúp ích rất nhiều.