使用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
答案 0 :(得分:0)
尝试在grunt-contrib-requirejs选项中设置baseUrl:
requirejs: {
compile: {
options: {
baseUrl: "path/to/base"
}
}
}
记录here。
答案 1 :(得分:0)
我看到一些代码导致问题。我知道这个问题是在三年前提出的,所以其中一些问题可能不适用于当时。但我去了back in time,并在回答之前查看了3年前的航班代码。
为什么要为grunt插件提供requireLib
?您不应该这样做,因为grunt插件应该加载它自己的。结果将是两个需要libs加载,这只是令人困惑。
Shim配置旨在与non-AMD modules only一起使用,但是您要同时填充“app”和“index”,它们都是AMD模块。我希望这会有一些奇怪的行为。
为了修复#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文件,那将会有很大帮助。