网络科技

    今日:495| 主题:244966
收藏本版
互联网、科技极客的综合动态。

[其他] Let's modernize the way we handle frontend code with Django

[复制链接]
校服一穿就变乖 发表于 2016-10-13 12:41:56
151 5

立即注册CoLaBug.com会员,免费获得投稿人的专业资料,享用更多功能,玩转个人品牌!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
The problem

    Django is great but it’s frontend toolchain is stuck in the past. Imagine if someone told you to copy all your python module dependencies in your source tree and import them from there. Unthinkable, right? We’ve pip and virtualenv for that. We also have npm and bower for frontend packages but we still choose to manage frontend packages manually or write very complex wrappers over javascript tools so that we only have to deal with Python. I think this needs to change. The javascript community has come up with some awesome pieces of software. Npm is one of the best, probably the best package manager I’ve come across. Tools like grunt, gulp,
    browserify, webpack are too good to ignore.
    Problems with the currect approach

  
       
  • Manually maintaining dependencies is a pain.   
  • No integration with managers like npm and bower.   
  • Horrible for frontend engineers and designers to work with.   
  • Backend and frontend systems are tightly coupled and sometimes limit each other.  
  What about django-npm, pipeline and compressor?

  Apps like django-pipeline and django-compressor have done a great job with static assets. I’ve been a great fan of django-pipeline actually but I hate how they take away all the transparency in order to make things a bit magical. They are limited in what they can do by the staticfiles system. They are opaque and make it harder to debug problems. They also need companion apps for to integrate any new javascript tool like gulp or browserify. Even after having all these wrappers, the experience can be quite rough at times. Documentation and resources available for javascript tools are not directly applicable sometimes when using these wrappers. You’ve an additional layer of software and configuration to worry about or wonder how your python configuration translates to the javascript. Things can be much better.
  Problems with wrappers

  
       
  • They are opaque, slow and hard to debug.   
  • Limited by django’s staticfiles system.   
  • Docs, stackoverflow, blog posts, written for the javascript tools don’t directly apply to the django wrapper apps.   
  • Limited by staticfiles.   
  • Very tightly coupled with django.  
  So should we abandon staticfiles?

  No, but we should limit it to just collecting pre-bundled static assets to the target directory or static file servers. We should not hook post-processors into it. The build process for frontend assets should be completely decoupled from staticfiles.
  How do we integrate the frontend tools with django then?

  For simpler cases, we don’t even need to integrate. We can use things like gulp or grunt to compile assets and then use collectstatic to sync the builds, but we need some sort of integration to make things a bit smoother. During development, it makes sense to return 500 error code when a build fails so one knows immediately where to look for the problem. It also makes sense to block a request while a build is being compiled to make sure you don’t test older code in the browser. For production use, we can configure our frontend tools to use hashed names in the builds; It would be nice to have an easy way to get reference to hashed bundles in django. In my opinion, integration should stop here. We should not spawn processes from python, translate config in settings.py to native JS config.
  I suggest we use bridges, not wrappers. Instead of writing wrappers around something like webpack and spawning webpack processes form within django, we should run webpack independently and pipe the output to django. If we can come up with a standard for this, we would only have to write a single bridge application for django. Then instead of writing django apps that wrap the javascript tools, we write plugins for the tools that emit useful data to be consumed by django or any other framework.
  webpack-bundle-tracker + django-webpack-loader

      django-webpack-loaderand    webpack-bundle-trackerimplement a system like this. webpack-bundle-tracker plugin emits necessary information about webpack’s compilation process and results so django-webpack-loader can consume it. django-webpack-loader does not care how you use webpack. You could control it from gulp, use the dev server, use the —watch mode or manually run it after every change. Head over to    https://github.com/owais/django-webpack-loader/to see how it all works or readthis guide for setting it all up with optional live reload for react components.  
  What this solves

  
       
  • Use proper package managers like npm and bower instead of manually managing source files.   
  • Use webpack transparently without any limitations and leverage all the documentation and resources.   
  • Handle your frontend assets however you want. Run webpack in watch mode, use grunt, gulp, webpacks’ dev server or anything you desire. No limitations.   
  • Make your frontend engineers and designers happy! :)   
  • Completely decouple frontend build process from django. You could build and serve your static assets from a completely different system as long you give django access to the stats file generated by webpack-bundle-tracker.  
  Limitations of this approach (for now)

  
       
  • Harder to store static files in app directories (totally worth it).   
  • Doesn’t integrate with static files provided by 3rd party apps yet.   
  • Need to setup your frontend toolchain manually but that is very easy most of the time.  
友荐云推荐




上一篇:90后CEO:别再叫我创业,我再也不想创业了
下一篇:It looks like Facebook motivated a lot of people to register to vote
酷辣虫提示酷辣虫禁止发表任何与中华人民共和国法律有抵触的内容!所有内容由用户发布,并不代表酷辣虫的观点,酷辣虫无法对用户发布内容真实性提供任何的保证,请自行验证并承担风险与后果。如您有版权、违规等问题,请通过"联系我们"或"违规举报"告知我们处理。

音乐AS篮球 发表于 2016-10-13 13:07:57
看起来不错
回复 支持 反对

使用道具 举报

邓艾瑾 发表于 2016-10-14 15:56:24
看完了这么强的文章,我想说点什么,但是又不知道说什么好,想来想去还是想想就好了
回复 支持 反对

使用道具 举报

廖莉 发表于 2016-10-29 06:02:28
支持,楼下的跟上哈~
回复 支持 反对

使用道具 举报

苟且的独活着 发表于 2016-11-1 08:31:01
我消极对待减肥,能不能取消我胖子的资格啊
回复 支持 反对

使用道具 举报

刘美星 发表于 2016-11-14 21:24:37
元芳你怎么看?
回复 支持 反对

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读

扫码访问 @iTTTTT瑞翔 的微博
回页顶回复上一篇下一篇回列表手机版
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )|网站地图 酷辣虫

© 2001-2016 Comsenz Inc. Design: Dean. DiscuzFans.

返回顶部 返回列表