RequireJS:有没有办法实现多个基本URL? [英] RequireJS: Is there a way to achieve multiple base URLs?

查看:208
本文介绍了RequireJS:有没有办法实现多个基本URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用一个单独的域作为JavaScript框架,它将创建一个基本需求配置,我可以从应用程序中增加。

I want to use a separate domain as a JavaScript framework and it will create a base require config which I can augment from the app.

foo.example.com
    main.js
    lib/foo-specific.js
framework.example.com
    framework.js <-- entry point
    lib/jquery.js
    lib/etc...

最佳,我'我希望能够要求'lib / foo-specific'和/或'lib / jquery'并且让路径很好地解决,但是从我发现的,没有办法做到这一点,除非我使用框架中每个js文件的特定路径键/值。目前,我有一个自定义插件,用不同的基本URL加载给定的路径(例如 fw!lib / jquery ),但如果我想使用 text!插件,它不起作用,因为不支持插件链接。

Optimally, I'd like to be able to require 'lib/foo-specific' and/or 'lib/jquery' and have the paths just resolve nicely, but from what I've found, there's no way to do this, unless I use a specific path key/value for every js file in the framework. At the moment, I've got a custom plugin to load the given path with a different base url (e.g. fw!lib/jquery), though if I wanted to use the text! plugin, it won't work as plugin chaining is unsupported.

请参阅 https://github.com/jpillora / js-framework 我目前得到的,还有 https://github.com/jpillora / prettyprinter 用例。

See https://github.com/jpillora/js-framework for what I've currently got, and also https://github.com/jpillora/prettyprinter for a use case.

有没有一种干净的方法可以解决这个问题?或者实现多个基本URL?

Is there a clean way to solve this ? or to achieve multiple base URLs ?

注意:我也查看了多个需求实例,但我认为这不会起作用,因为我喜欢这个应用程序能够访问框架的配置。

Note: I have also looked into multiple require instances, though I don't think that would work as I'd like the the app to be able to access the framework's config.

推荐答案

James Burke对RequireJS Github问题的回答:问题#447:多个基本网址·jrburke / requirejs

Answered by James Burke on RequireJS Github Issue's page: Issue #447: Multiple Base URLs · jrburke/requirejs.

如果 data-main是脚本的唯一入口点(更多信息的注释),结果很简单,我用以下方法解决了我的特殊问题:

Turns out to be quite simple if data-main is the only entry point to your scripts(comments for more info), I solved my particular problem with the following:

我的应用 index.html

<script src="http://framework.jpillora.com/js/lib/require.js" 
  data-main="http://framework.jpillora.com/js/framework" > </script>

将requirejs入口点设置为 framework.js

has the requirejs entry point set to framework.js:

var framework = ... //set using script elements src attribute

require.config({

    baseUrl: 'js/',

    //Framework paths
    paths: {
      'framework': framework,
      'lib'      : framework + 'js/lib',
      'ext'      : framework + 'js/ext',
      'util'     : framework + 'js/util'
    },

    //Shortcuts
    map: {
      '*': {
        ...
      }
    },

    //Non-modularised libraries with deps
    shim: {
      ...
    }
});

require(['main']);

因此,而不是正常做 index.html-> main.js ,我们正在添加一个额外的步骤 index.html-> framework.js-> main.js ,它提供了应用代码知识框架代码的路径。

So instead of normally doing index.html->main.js, we're adding an extra step index.html->framework.js->main.js, which gives the app code knowledge of paths to the framework code.

例如,在应用程序中 http ://prettyprint.jpillora.com/ ,一旦require已加载 framework.js ,它将设置路径 lib / .. 。 http://framework.jpillora.com/ 并设置 baseUrl as ./ js / 所以一旦 main 是必需的,它将基本网址设置为自己的域, lib 指向另一个域。

For example, in the app http://prettyprint.jpillora.com/, once require has loaded framework.js, it will setup paths to lib/... which to http://framework.jpillora.com/ and set the baseUrl as ./js/ so once main is required, it will have the base url set to it's own domain and lib pointing to another domain.

哪个会导致 require(['lib / foo','view / bar']);
解析为:

Which results in require(['lib/foo', 'view/bar']); resolving to:

http://framework.jpillora.com/js/lib/foo.js
http:// prettyprint。 jpillora.com/js/view/bar.js

如此处所示,该应用程序仅为 main。 js 其他一切都来自框架

As displayed here, the app is only a main.js everything else comes from the framework:

所以最后,每当我加载应用的 main.js 通过上面的 framework.js ,然后我可以访问我所有常用的库和实用程序类。请参阅应用源代码。

So finally, whenever I load an app's main.js via with the above framework.js, I then have access to all of my commonly used libraries and utility classes. See app source.

另请注意,使用 r.js 优化器和一个不错的本地文件结构,可以还将应用程序优化为单个js文件,仅提取框架所需的内容

Also note, with the r.js optimiser and a nice local file structure, one can also optimise the app into a single js file pulling only what's required from framework.

这篇关于RequireJS:有没有办法实现多个基本URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆