使用该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)
这是我的 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.coffee
和app/scripts/ui/orange.coffee
都很简单:
"use strict"
define ['flight/component'], (defineComponent) ->
apple = ->
# stuff
defineComponent apple