Correct setting of REQUIRE_BASE_URL to work in file processing and also when serving

145 views
Skip to first unread message

induct...@gmail.com

unread,
May 27, 2013, 8:04:36 AM5/27/13
to django-...@googlegroups.com
Hi,

I am trying to get django-require working to manage JS dependencies and minification. However, I am having difficulty with the requireJS baseUrl.

require.js is at project/static/require.js, and page JS is at project/static/pages/pageA.js and so on.

The following are the django-require settings.

    REQUIRE_BASE_URL = os.path.join(PROJECT_DIR, 'static')
    REQUIRE_BUILD_PROFILE = 'app.build.js'
    REQUIRE_JS = "require.js"
    REQUIRE_STANDALONE_MODULES = {}
    REQUIRE_DEBUG = DEBUG
    REQUIRE_EXCLUDE = ("build.txt",)
    REQUIRE_ENVIRONMENT = "auto"


I can't get the base URL to be correct during static file collection/processing and when serving via the browser. If I set it like this:

    REQUIRE_BASE_URL = os.path.join(PROJECT_DIR, 'static')

then the compilation process is successful (node finds r.js and so on). However, the template tags like {% require_module pages/pageA %} render like this:

    <script data-main="/var/www/project/static/pages/pageA.js" src="/var/www/project/static/require.js"></script>

This fails from the browser side, as the path is the path on the machines actual file system, not in the website structure!

However, if I set the base URL like this:

    REQUIRE_BASE_URL = 'static'

then the compilation step fails, since r.js is being called with a path that doesn't exist on the local file system (but would exist on the web site):

    File "/usr/local/lib/python2.7/dist-packages/require/storage.py", line 50, in run_optimizer
      raise OptimizationError("Error while running r.js optimizer.")


 The rendered script tags are correct in this case.

Adding a setting to the app.build.js like "baseUrl: '/static'", doesn't seem to have any effect. I saw the note "'baseUrl' will be overidden by django-require during the build process," but I am unsure if that means the overwriting applies only during the build process or if it happens during the build process and persists.

What is the right way to feed r.js the local file paths during the build process, but output website-relevant paths in the template tag when the site is served?

Thanks,

Inductiveload

Dave Hall

unread,
May 27, 2013, 1:19:03 PM5/27/13
to induct...@gmail.com, django-...@googlegroups.com
In my experience, RequireJS works a lot better if all main scripts (as would be set as data-main on the requirejs script tag), are in the root of the baseUrl.

So, in your case, I'd use all the default settings for django-require, and then just set REQUIRE_BUILD_PROFILE.

I'd then place all your data-main scripts in the root of your js folder.

The author of RequireJS actually recommends keeping the folder structure very simple for RequireJS projects.




--
You received this message because you are subscribed to the Google Groups "django-require discussion and announcements" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-requir...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

induct...@gmail.com

unread,
May 28, 2013, 7:18:16 AM5/28/13
to django-...@googlegroups.com, induct...@gmail.com
Hi Dave,


So, in your case, I'd use all the default settings for django-require, and then just set REQUIRE_BUILD_PROFILE.

I changed my settings.py to include only the following:

    STATICFILES_STORAGE = 'require.storage.OptimizedStaticFilesStorage'
    REQUIRE_BUILD_PROFILE = "app.build.js"

 
I'd then place all your data-main scripts in the root of your js folder.

The author of RequireJS actually recommends keeping the folder structure very simple for RequireJS projects.


I haven't flattened the JS structure yet, as I'm still working on the overall layout. However, relative dependencies are working fine for my purposes now, as if CSS and JS compression on collectstatic. I will work on tidying the JS tree as a separate step, as well as adding paths and so on to the RequireJS config.

Everything seems to be working, and I can even build a monolithic standalone app with REQUIRE_STANDALONE_MODULES.

So it seems that my problems were caused by specifying too many settings in settings.py.

Thanks for your help, and thanks for a really useful tool!

Inductiveload

Dave Hall

unread,
May 28, 2013, 7:25:37 AM5/28/13
to induct...@gmail.com, django-...@googlegroups.com
Happy to help!


--
Reply all
Reply to author
Forward
0 new messages