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.coffee
và app/scripts/ui/orange.coffee
chỉ đơn giản là:
"use strict"
define ['flight/component'], (defineComponent) ->
apple = ->
# stuff
defineComponent apple
Đồ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
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
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