andreyfedoseev / django-static-precompiler

Django Static Precompiler provides template tags and filters to compile CoffeeScript, LiveScript, SASS / SCSS, LESS, Stylus, Babel and Handlebars. It works with both inline code and external files.
Other
221 stars 60 forks source link

how to make source files not be under static directory? #89

Closed KevinSeghetti closed 6 years ago

KevinSeghetti commented 8 years ago

Trying to add react to a babel6 static compile. Did that with a plugin directive. Got an error that it couldn't find that plugin. After some digging, it turns out the source path babel is being handed is in the public/static area (which in my project is not under the project tree). So babel was unable to locate its plugins (because it looks up the tree from the source file)

More digging: the reason the source path is pointing to the static output is because compilers/base.py:get_full_source_path looks there first, and there is a copy of my source file there (maybe it should like in the source tree first, unsure).

But really, the issue here is that my source file is getting copied to the static tree, when really it shouldn't. So I figured I would just move the source file so it wasn't under a static dir in the source tree. But if I do that, then this doesn't work <script src="{% static "path/to/script.coffee"|compile %}"> since the static plugin only searches inside of static directories, and ./manage.py compilestatic doesn't seem to find it either.

I have worked around this issue by putting all of my jsx files in a sub-dir of /static//jsx and then done ./manage.py collectstatic --noinput -i jsx -v 0 so the jsx directory doesn't get copied.

So, my question is: is there a way to locate the source file outside of the source static directories, and still get it to work both for runserver based dev, and compilestatic based deployment?

andreyfedoseev commented 8 years ago

It's not clear what is the issues. Is it:

  1. Babel doesn't locate its plugins
  2. Static precompiler can't locate the source files

Regarding the first case, Babel plugins are installed with npm and it has nothing to do with static_precompiler. The typical location of the plugins is the node_modules folder in your working directory.

Regarding the second case, static_precompiler locates the source files (almost) the same way is Django does. It first looks for the files in your STATIC_ROOT folder, then it uses django.contrib.staticfiles.finders to locate the files. If your files are located outside of the project tree, you might want to use STATICFILES_DIRS setting, see https://docs.djangoproject.com/en/1.10/ref/settings/#std:setting-STATICFILES_DIRS

KevinSeghetti commented 8 years ago

Yea, sorry I wrote a novel.

My problem only occurs when I try to deploy (when I do ./manage.py compilestatic)

I have a directory structure of:

/static//es6. with source files in es6. (and that location is included in STATICFILES_DIRS) I have them there so that works. My actual question is: Can I place the source file someplace not included in STATICFILES_DIRS and still get that to work? (that way the source files won't get copied by./manage.py collectstatic). If so, what is the syntax to do that? On Wed, Aug 31, 2016 at 8:43 AM, Andrey Fedoseev notifications@github.com wrote: > It's not clear what is the issues. Is it: > 1. Babel doesn't locate its plugins > 2. Static precompiler can't locate the source files > > The problem is, static precompiler is finding the source file in the > ../public/static directory, instead of the one in the source directory. > Which causes babel to fail, because it assumes the source file to be > transpiled _will_ be in a directory under the directory containing > node_modules. It seems to me the proper solution is to not have the source files get copied to ../public/static (that is happening when I do ./manage.py collectstatic, because the source files are under /static/ ) Regarding the first case, Babel plugins are installed with npm and it has > nothing to do with static_precompiler. The typical location of the > plugins is the node_modules folder in your working directory. > > Regarding the second case, static_precompiler locates the source files > (almost) the same way is Django does. It first looks for the files in your > STATIC_ROOT folder, > > and that is actually the problem. It is finding the copy of the source file > copied to STATIC_ROOT, and asking babel to transpile that version. But > STATIC_ROOT doesn't have node_modules, so babel can't find it plugins. What is the reasoning for looking in STATIC_ROOT before looking in the project tree? If it just looked in the project tree first it would find the correct source file instead of the one being copied. (although, as indicated above, it is a better solution to not copy those source files into STATIC_ROOT at all). then it uses django.contrib.staticfiles.finders to locate the files. If > your files are located outside of the project tree, you might want to use > STATICFILES_DIRS setting, see https://docs.djangoproject. > com/en/1.10/ref/settings/#std:setting-STATICFILES_DIRS > > — > You are receiving this because you authored the thread. > Reply to this email directly, view it on GitHub > https://github.com/andreyfedoseev/django-static-precompiler/issues/89#issuecomment-243806588, > or mute the thread > https://github.com/notifications/unsubscribe-auth/AAP9_IVPT37N3zpVdiziI0xbS0eAoE_lks5qlaEXgaJpZM4JxCfL > . thanks, you efforts are appreciated (overall the precompiler works great). ## Kevin Seghetti: E-Mail: kts@tenetti.org, HTTP: www.tenetti.org GPG public key: http://wiki.tenetti.org/bin/view/KevinSeghetti/GPGKey Copyright is a temporary loan from the public domain, not property
andreyfedoseev commented 8 years ago

Can I place the source file someplace not included in STATICFILES_DIRS and still get that to work? (that way the source files won't get copied by./manage.py collectstatic).

--ignore option for collectstatic might help.

But STATIC_ROOT doesn't have node_modules, so babel can't find it plugins.

You can install node package either globally, or locally. In the latter case, you'll have node_modules under your current directory. If you run compilestatic from the same directory, node should be able to find them. If you have node_modules somewhere else, try setting NODE_PATH directory, like env NODE_PATH=/directory/where/node_modules/is/located ./manage.py compilestatic. Or you can just install them globally with npm -g option.

KevinSeghetti commented 8 years ago

On Wed, Aug 31, 2016 at 6:19 PM, Andrey Fedoseev notifications@github.com wrote:

Can I place the source file someplace not included in STATICFILES_DIRS and still get that to work? (that way the source files won't get copied by./manage.py collectstatic).

--ignore option for collectstatic might help.

That is what I ended up doing, but was hoping for a way to move the source files to not be in a directory that collectstatic would copy. I might dig into the static module and see if there is a way to specify a search path that is searched by

but not searched by manage.py collectstatic.

If not, maybe the cleanest solution would be to duplicate the functionality of the static tag in

in the precompiler, maybe something like

Unless I have missed something and there is already a way to do that ( I am still feeling my way through how a lot of these components work, so apologies if I did )

But STATIC_ROOT doesn't have node_modules, so babel can't find it plugins.

You can install node package either globally, or locally. In the latter case, you'll have node_modules under your current directory. If you run compilestatic from the same directory, node should be able to find them.

Interestingly, babel doesn't work that way (I discovered by digging into the code). When it goes looking for its modules, it starts with the path of the source file it has been asked to transpile, and works up from there, looking for the node_modules directory. (I have been told by someone who knows much more about node.js than I do that that is intentional, to insure the environment doesn't change between dev and release).

If you have node_modules somewhere else, try setting NODE_PATH directory,

like env NODE_PATH=/directory/where/node_modules/is/located ./manage.py compilestatic. Or you can just install them globally with npm -g option.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/andreyfedoseev/django-static-precompiler/issues/89#issuecomment-243949479, or mute the thread https://github.com/notifications/unsubscribe-auth/AAP9_DW6-1tU3x6PqPTs-lEujH58OV7qks5qligugaJpZM4JxCfL .

Thanks for the suggestions,

Kevin Seghetti: E-Mail: kts@tenetti.org, HTTP: www.tenetti.org GPG public key: http://wiki.tenetti.org/bin/view/KevinSeghetti/GPGKey Copyright is a temporary loan from the public domain, not property