简体   繁体   English

RequireJs + Rails 4

[英]RequireJs + Rails 4

I'm new to RequireJs, so i little bit stuck with getting my rails 4 app working on production with requirejs pieces. 我是RequireJs的新手,所以我有点想让我的rails 4应用程序使用requirejs进行生产。

I have the following: 我有以下内容:

1) Require_rails gem 1)Require_rails宝石

gem 'requirejs-rails'

2) Piece of require-js called in a middle of a html.erb file 2)在html.erb文件中间调用的require-js

<%= requirejs_include_tag asset_url("scribe/scribe-editor.js") %>

3) In this a file I have following 3)在这个文件中,我有以下内容

require({
  paths: {
    'scribe': 'scribe/bower_components/scribe/scribe',
    'scribe-plugin-blockquote-command': 'scribe/bower_components/scribe-plugin-blockquote-command/scribe-plugin-blockquote-command',
    'scribe-plugin-code-command': 'scribe/bower_components/scribe-plugin-code-command/scribe-plugin-code-command'
  }
}, [
  'scribe',
  'scribe-plugin-blockquote-command',
  'scribe-plugin-code-command',
], function (
  Scribe,
  scribePluginBlockquoteCommand,
  scribePluginCodeCommand,
) {
...
});

4) All these files located in vendor/javascripts/scribe 4)所有这些文件都位于vendor / javascripts / scribe中

vendor/javascripts/scribe/scribe-editor.js
vendor/javascripts/scribe/bower_components/scribe/scribe.js
vendor/javascripts/scribe/bower_components/scribe/scribe/scribe-plugin-blockquote-command/scribe-plugin-blockquote-command.js
vendor/javascripts/scribe/bower_components/scribe/scribe-plugin-code-command/scribe-plugin-code-command.js

5) On production I have the following code. 5)在生产中,我有以下代码。

<script src="/assets/require-21be67676bcf2b75c547dd382d849e26.js"></script>
<script>require.config({"baseUrl":"http://domain.com/assets","paths":{"application":"/assets/application-e720d39c2f6b94f1fd554d97b48ee312"}}); require(["http://domain.com/assets/scribe/scribe-editor-a848a8a552e89e8a8f6e9a62d10cd58f.js"]);</script>

6) And finally, my asset.rb file: 6)最后,我的asset.rb文件:

Rails.application.config.assets.precompile += %w( 
    scribe/scribe-editor.js
)

============= 7) The problem: script editor is loaded, but all dependencies come with 404 not found. ============= 7)问题:脚本编辑器已加载,但未找到所有依赖项和404。

Thanks for your help. 谢谢你的帮助。

I have not used requirejs-rails with Rails 4 yet, just rails 3.2 but I think it works mostly the same. 我还没有在Rails 4中使用requirejs-rails,只是在Rails 3.2中使用,但我认为它的工作原理基本相同。

I think the reason you are getting those 404's for the dependencies is because r.js never ran on scribe/scribe-editor instead it ran on application.js which is why you see /assets/application-e720d39c2f6b94f1fd554d97b48ee312 referenced in your production code. 我认为获取依赖项的那些404的原因是因为r.js从未在scribe/scribe-editor运行,而是在application.js上运行,这就是为什么您在生产代码中看到/assets/application-e720d39c2f6b94f1fd554d97b48ee312

First problem I see is that you are using config.assets.precompile . 我看到的第一个问题是您正在使用config.assets.precompile Based on the Troubleshooting section in requirejs-rails Readme you should not include requirejs modules in config.assets.precompile since that will make rails try precompile it using sprockets. 根据requirejs-rails自述文件中的“ 疑难解答”部分,您不应在config.assets.precompile包含requirejs模块,因为这会使rails尝试使用链轮对其进行预编译。 You want r.js to optimize any requirejs modules. 您希望r.js优化任何requirejs模块。 Requirejs-rails will run r.js for you during the precompile step. Requirejs-rails将在预编译步骤中为您运行r.js。 In my project I use config.assets.precompile to compile css, an Javascript that is not a requirejs module. 在我的项目中,我使用config.assets.precompile编译css,这是一个不是requirejs模块的Javascript。 For instance application.js is not a requirejs module in my case (and apperently also in your case). 例如,就我而言,application.js不是requirejs模块(在您的情况下也是如此)。

You don't mention whether you have a requirejs.yml file in your project. 您没有提及项目中是否有requirejs.yml文件。 If not I suggest you add one and move your requirejs config there instead of adding it to the head of the scribe-editor.js module. 如果不是,我建议您添加一个,然后将requirejs配置移到那里,而不是将其添加到scribe-editor.js模块的头部。

requrejs.yml: requrejs.yml:

modules:
   - name: scribe/scribe-editor
paths:
   scribe: scribe/bower_components/scribe/scribe
   scribe-plugin-blockquote-command: scribe/bower_components/scribe-plugin-blockquote-command/scribe-plugin-blockquote-command
   scribe-plugin-code-command: scribe/bower_components/scribe-plugin-code-command/scribe-plugin-code-command

RequireJS-Rails will use the modules config as part of the r.js build config to indicate what files need to be run through r.js. RequireJS-Rails将使用modules配置作为r.js构建配置的一部分,以指示需要通过r.js运行哪些文件。

RequireJS-Rails will convert the paths into Javascript and add it as a <script> tag before the require.js script tag (excluding the modules stuff) as the requirejs.config. RequireJS-Rails会将路径转换为Javascript,并将其作为<script>标记添加到require.js脚本标记(不包括模块内容)之前,作为requirejs.config。 Anything that can be in a requirejs config can be added to requirejs.yml 可以将requirejs配置中的所有内容添加到requirejs.yml

You can then change your scribe-editor module definition to something like: 然后,您可以将scribe-editor模块定义更改为以下内容:

define(['scribe', 'scribe-plugin-blockquote-command', 'scribe-plugin-code-command'], 
       function( Scribe, scribePluginBlockquoteCommand, scribePluginCodeCommand) {
...
});

Lastly I don't think you want the asset_url() call in requirejs_include_tag , you need to reference the requirejs module. 最后,我认为您不需要在requirejs_include_tagasset_url()调用,您需要引用requirejs模块。 Something like <%= requirejs_include_tag('scribe/scribe-editor') %> should work. <%= requirejs_include_tag('scribe/scribe-editor') %>应该可以工作。

You should end up with 2 script tags added to your response in production as a result of the requirejs_include_tag . 由于requirejs_include_tag的结果,您应该最终在生产中的响应中添加2个script标签。 If I am guessing right it should look a lot like this(the SHA's I just made up): 如果我猜对了,它应该看起来像这样(SHA刚由我组成):

<script>var require = {"baseUrl":"/assets","paths":{"scribe/scribe-editor":"/assets/scribe/scribe-editor-c48f6b842cf0f40976393a1d2f4568e0"}};</script>
<script data-main="scribe/scribe-editor-c48f6b842cf0f40976393a1d2f4568e0" src="/assets/require-cbfc3f690e2109e37e09aefd8fe40332.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM