当使用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在lib中的每个脚本中都需要utils.js,路径为./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
解决方法
尝试在grunt-contrib-requirejs选项中设置baseUrl:
requirejs: { compile: { options: { baseUrl: "path/to/base" } } }
记录here.