Django + Django-Pipeline with Javascript "Require"
Asked Answered
B

3

5

I have a module that calls require on a Javascript library. I am trying to compress my .js files with Django-Pipeline but after collectstatic the web-page gives the error:

UncaughtReferenceError: require is not Defined

Normal testing/deployment works fine with collect-static if I don't use Django-Pipeline to compress the files. What is the correct setup for minifying my .js files without breaking dependencies - the documentation is kind of sparse. Is django-pipeline the right choice?

Edit: I've tried to make sure the 'required' models are compressed first in pipeline, but I can't seem to compress the module that require is called from at all without it returning an error.

Bryozoan answered 1/7, 2014 at 0:45 Comment(0)
G
4

In my experience, it's difficult to combine both require.js and django-pipeline (even though they are both great projects).

I've worked on one project which used require.js for all its javascript. I tried to use django-pipeline but couldn't get it to work properly with require.js. We ended up working with https://github.com/etianen/django-require for the javascript, and django-pipeline for the css minification.

I would recommend either trying to remove require.js and manually including the library that it is importing using pipeline, or using require.js to build and minify all your javascript assets. You could do add a new step that builds all your javascript files, and link to those either directly in the templates or using pipeline.

Sorry, this may not be an actual 'solution' to your answer, but you did ask "Is django-pipeline the right choice?" :)

Georgie answered 25/7, 2014 at 16:56 Comment(1)
Agreed. Handling CSS with django-pipeline is great, but in terms of JS we absolutely could use a more flexible solution like django-require.Electra
R
2

This is because your JS compressor (YUGLIFY for example) is also minifying javascript variable names, so require might have been named to something like x or y . To overcome this set proper options in your JS compressor, to not minify the variable names.

http://django-pipeline.readthedocs.org/en/latest/compressors.html#pipeline-yui-js-arguments

For Yuglify, this argument would be mangle:False, which would stop mangling the names.

Reef answered 23/7, 2014 at 7:17 Comment(1)
+1. You're correct that setting mange:false will fix the require break - but one still ends up minifying the entire library (350kb in this case), which is kind of a minus. So I think I'm going to have to try django-require.Bryozoan
K
1

Maybe it's PIPELINE_DISABLE_WRAPPER. Try setting it to True.

See https://django-pipeline.readthedocs.org/en/latest/configuration.html#wrapped-javascript-output

Kofu answered 1/7, 2014 at 7:33 Comment(2)
I do have PIPELINE_DISABLE_WRAPPER - TrueBryozoan
I guess Django-Pipeline can't process require correctly? Unless someone has an update for this.Bryozoan

© 2022 - 2024 — McMap. All rights reserved.