Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RequireJS optimization doesn't correctly detect define method in i18n.js #314

Closed
thegillis opened this issue Mar 11, 2015 · 2 comments
Closed

Comments

@thegillis
Copy link

I'm currently incorporating the i18n-js gem into my project. We currently export translations and use the requirejs-rails gem to load the i18n.js file from the server. We then will use the r.js optimization tool to assemble all of our dependencies into one JS file.

Although the contents of the i18n.js file are included in this r.js optimized version, it does not correctly detect the define method and appends a "shim" define at the end. Unfortunately this causes the I18n JS object to be inaccessible. I have tracked the problem down to the global closure inside the define call.

Steps to reproduce (current version of r.js included in the latest requirejs-rails gem on ubuntu server with packages nodejs npm):

wget 'https://raw.githubusercontent.com/jwhitley/requirejs-rails/0.9.5/bin/r.js'
# master copy as of 3/11/2015
wget 'https://raw.githubusercontent.com/fnando/i18n-js/f67fa51427dd004b43f20c46c4016a71012d6699/app/assets/javascripts/i18n.js'
# Optimized version
nodejs r.js -o baseUrl=. name=i18n out=i18n-optim.js
# Non-optimized version
nodejs r.js -o baseUrl=. name=i18n out=i18n-nooptim.js optimize=none

If you view the i18n-nooptim.js file, the last line is:

define("i18n.js", function(){});

Unfortunately this causes all require statements to return undefined for the 'i18n' dependency:

require(['i18n'], function(I18n) {
  I18n.t('true'); // FAIL
});

This is also identical to the most recent development copy of the r.js tool:

# master as of 3/11/2015
wget -O r.2.1.16.js 'https://raw.githubusercontent.com/jrburke/r.js/443809bfe5d668bf4217c4f26b4636011cf91aa6/dist/r.js'
nodejs r.2.1.16.js -o baseUrl=. name=i18n out=i18n-16-optim.js
nodejs r.2.1.16.js -o baseUrl=. name=i18n out=i18n-16-nooptim.js optimize=none

You will notice that the i18n-16-nooptim.js and i18n-16-optim.js files suffer from the same problem.

The fix is to replace the line:

define('i18n', (function(global){ return function(){ return factory(global); }})(this));

With:

var global=this;
define('i18n', function(){ return factory(global);});

I believe that these are functionally identical, but I'm not sure about the original intention for this define statement statement, so I'm not sure if it will violate any edge cases. It's even fewer bytes :)

I'll create a pull request as an example fix.

@thegillis
Copy link
Author

Notes:

This issue appears to be similar to this problem:
requirejs/r.js#704

In this case though it was happening with jQuery.

I was not able to follow the change to the parser to figure out how it relates to this problem.

@johnnyshields
Copy link

@thegillis if we add ES6 module support to this library, does it fix your issue?

@fnando fnando closed this as completed May 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
4 participants