使用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.coffee
和app/scripts/ui/orange.coffee
只是:
"use strict"
define ['flight/component'], (defineComponent) ->
apple = ->
# stuff
defineComponent apple
我的隊友挖掘到[requirejs節點模塊](https://github.com/jrburke/r.js)中包含的r.js,並確認它們沒有以消除路徑的方式規範化路徑重複我有。不知道這是有意的還是不正確的。如果他可以進一步挖掘,也許他可以在GitHub上打開一個問題。 – maxbeatty
我寫了一個叫做grunt-reduce的咕嚕任務,它爲你的頁面做了一大堆優化,包括正確解析和縮小requirejs依賴關係。 如果你還可以正常運行其他優化,也許你應該試試看看你是否可以運行它:https://github.com/Munter/grunt-reduce – Munter
溝渠咕嚕和槓桿webpack ...具體來說,'DedupePlugin'將完全符合您的需求。 https://github.com/webpack/docs/wiki/optimization – Maxwelll