From f036ae431ae5d7ff1f40b7665711b29e295605ba Mon Sep 17 00:00:00 2001 From: Will Farley Date: Sat, 11 Jun 2016 16:20:16 -0400 Subject: [PATCH] Add webpack as an option Adds webpack as a js_taskrunner option to cookiecutter-json. Will clone @hzdg/cookiecutter-webpack --pydanny-django branch into the project using cookiecutter's api in post_hooks. The static webpack project will be placed into the /static// directory. The webpack configs are placed in the ./config/ directory. The cookiecutter-webpack project includes react / redux / karma configurations that are brought into the project. --- CONTRIBUTORS.rst | 2 ++ cookiecutter.json | 2 +- hooks/post_gen_project.py | 30 +++++++++++++++++++ {{cookiecutter.project_slug}}/README.rst | 10 +++++++ .../config/settings/common.py | 13 ++++++++ .../config/settings/production.py | 14 +++++++++ .../requirements/base.txt | 5 ++++ .../templates/base.html | 16 +++++++--- 8 files changed, 87 insertions(+), 5 deletions(-) diff --git a/CONTRIBUTORS.rst b/CONTRIBUTORS.rst index 28768fa43..442e49c50 100644 --- a/CONTRIBUTORS.rst +++ b/CONTRIBUTORS.rst @@ -122,6 +122,7 @@ Listed in alphabetical order. Travis McNeill `@Travistock`_ @tavistock_esq Vitaly Babiy Vivian Guillen `@viviangb`_ + Will Farley `@goldhand`_ @g01dhand Yaroslav Halchenko ========================== ============================ ============== @@ -156,6 +157,7 @@ Listed in alphabetical order. .. _@eyadsibai: https://github.com/eyadsibai .. _@garry-cairns: https://github.com/garry-cairns .. _@garrypolley: https://github.com/garrypolley +.. _@goldhand: https://github.com/goldhand .. _@hackebrot: https://github.com/hackebrot .. _@hairychris: https://github.com/hairychris .. _@hjwp: https://github.com/hjwp diff --git a/cookiecutter.json b/cookiecutter.json index d3017023e..55ce2cfd6 100644 --- a/cookiecutter.json +++ b/cookiecutter.json @@ -18,7 +18,7 @@ "use_docker": "y", "use_heroku": "n", "use_compressor": "n", - "js_task_runner": ["Gulp", "Grunt", "None"], + "js_task_runner": ["Gulp", "Grunt", "Webpack", "None"], "use_lets_encrypt": "n", "open_source_license": ["MIT", "BSD", "Apache Software License 2.0", "Not open source"] } diff --git a/hooks/post_gen_project.py b/hooks/post_gen_project.py index d83d92287..2268da229 100644 --- a/hooks/post_gen_project.py +++ b/hooks/post_gen_project.py @@ -16,6 +16,7 @@ from __future__ import print_function import os import random import shutil +from cookiecutter.main import cookiecutter # Get the root project directory PROJECT_DIRECTORY = os.path.realpath(os.path.curdir) @@ -161,6 +162,30 @@ def remove_packageJSON_file(): PROJECT_DIRECTORY, filename )) + +def add_webpack(): + """ + Adds webpack configuration using cookiecutter to install hzdg/cookiecutter-webpack + """ + cookiecutter( + 'git@github.com:hzdg/cookiecutter-webpack.git', + replay=False, overwrite_if_exists=True, output_dir='../', + checkout='pydanny-django', no_input=True, extra_context={ + 'project_name': '{{ cookiecutter.project_name }}', + 'repo_name': '{{ cookiecutter.project_slug }}', + 'repo_owner': '', + 'project_dir': '{{ cookiecutter.project_slug }}', + 'static_root': '{{ cookiecutter.project_slug }}/static/{{ cookiecutter.project_slug }}', + 'production_output_path': '{{ cookiecutter.project_slug }}/static/{{ cookiecutter.project_slug }}/dist/', + 'author_name': '{{ cookiecutter.author_name }}', + 'description': '{{ cookiecutter.description }}', + 'version': '{{ cookiecutter.version }}', + 'existing_project': 'y', + 'css_extension': 'sass', + 'use_ejs': 'n', + 'open_source_license': '{{ cookiecutter.open_source_license }}' + }) + def remove_certbot_files(): """ Removes files needed for certbot if it isn't going to be used @@ -212,6 +237,11 @@ if '{{ cookiecutter.js_task_runner}}'.lower() == 'gulp': remove_grunt_files() elif '{{ cookiecutter.js_task_runner}}'.lower() == 'grunt': remove_gulp_files() +elif '{{ cookiecutter.js_task_runner }}'.lower() == 'webpack': + remove_gulp_files() + remove_grunt_files() + remove_packageJSON_file() + add_webpack() else: remove_gulp_files() remove_grunt_files() diff --git a/{{cookiecutter.project_slug}}/README.rst b/{{cookiecutter.project_slug}}/README.rst index 55a520c54..308c4086a 100644 --- a/{{cookiecutter.project_slug}}/README.rst +++ b/{{cookiecutter.project_slug}}/README.rst @@ -47,6 +47,16 @@ Running tests with py.test :: $ py.test +{% if cookiecutter.js_task_runner == 'Webpack' %} + +Running javascript tests with karma +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +:: + + $ npm test +{% endif %} + Live reloading and Sass CSS compilation ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/{{cookiecutter.project_slug}}/config/settings/common.py b/{{cookiecutter.project_slug}}/config/settings/common.py index cedf3c7f1..898e60ef9 100644 --- a/{{cookiecutter.project_slug}}/config/settings/common.py +++ b/{{cookiecutter.project_slug}}/config/settings/common.py @@ -243,5 +243,18 @@ STATICFILES_FINDERS += ("compressor.finders.CompressorFinder", ) # Location of root django.contrib.admin URL, use {% raw %}{% url 'admin:index' %}{% endraw %} ADMIN_URL = r'^admin/' +{% if cookiecutter.js_task_runner == 'Webpack' %} +# WEBPACK +# ------------------------------------------------------------------------------ +INSTALLED_APPS += ('webpack_loader',) +# Webpack Local Stats file +STATS_FILE = ROOT_DIR('webpack-stats.json') +# Webpack config +WEBPACK_LOADER = { + 'DEFAULT': { + 'STATS_FILE': STATS_FILE + } +} +{% endif %} # Your common stuff: Below this line define 3rd party library settings diff --git a/{{cookiecutter.project_slug}}/config/settings/production.py b/{{cookiecutter.project_slug}}/config/settings/production.py index fdd405048..e4a919128 100644 --- a/{{cookiecutter.project_slug}}/config/settings/production.py +++ b/{{cookiecutter.project_slug}}/config/settings/production.py @@ -303,5 +303,19 @@ LOGGING = { {% endif %} # Custom Admin URL, use {% raw %}{% url 'admin:index' %}{% endraw %} ADMIN_URL = env('DJANGO_ADMIN_URL') +{% if cookiecutter.js_task_runner == 'Webpack' %} + +# WEBPACK +# ------------------------------------------------------------------------------ +# Webpack Production Stats file +STATS_FILE = ROOT_DIR('webpack-stats-production.json') +# Webpack config +WEBPACK_LOADER = { + 'DEFAULT': { + 'BUNDLE_DIR_NAME': '{{ cookiecutter.project_slug }}/static/{{ cookiecutter.project_slug }}/dist/', + 'STATS_FILE': STATS_FILE + } +} +{% endif %} # Your production stuff: Below this line define 3rd party library settings diff --git a/{{cookiecutter.project_slug}}/requirements/base.txt b/{{cookiecutter.project_slug}}/requirements/base.txt index 9c1d4d9f1..4b4b14209 100644 --- a/{{cookiecutter.project_slug}}/requirements/base.txt +++ b/{{cookiecutter.project_slug}}/requirements/base.txt @@ -58,4 +58,9 @@ celery==3.1.23 django_compressor==2.0 {% endif %} +{% if cookiecutter.js_task_runner == 'Webpack' -%} +# Webpack +django-webpack-loader==0.3.0 +{%- endif %} + # Your custom requirements go here diff --git a/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/templates/base.html b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/templates/base.html index b491855cf..19340ac4b 100644 --- a/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/templates/base.html +++ b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/templates/base.html @@ -1,4 +1,5 @@ -{% raw %}{% load staticfiles i18n {% endraw %}{% if cookiecutter.use_compressor == "y" %}compress{% endif %}{% raw %}%} +{% raw %}{% load staticfiles i18n {% endraw %}{% if cookiecutter.use_compressor == "y" %}compress {% endif %}{% raw %}%} +{% endraw %}{% if cookiecutter.js_task_runner == 'Webpack' -%}{% raw %}{% load render_bundle from webpack_loader %}{% endraw %}{%- endif %}{% raw %} @@ -76,7 +77,9 @@
{{ message }}
{% endfor %} {% endif %} - +{% endraw %}{% if cookiecutter.js_task_runner == 'Webpack' %}{% raw %} +
+{% endraw %}{% endif %}{% raw %} {% block content %}

Use this document as a way to quick start any new project.

{% endblock content %} @@ -99,12 +102,17 @@ - +{% endraw %}{% if cookiecutter.js_task_runner == 'Webpack' %}{% raw %} + + {% render_bundle 'vendor' %} + {% render_bundle 'common' %} + {% render_bundle 'main' %} +{% endraw %}{% else %}{% raw %} {% endraw %}{% if cookiecutter.use_compressor == "y" %}{% raw %}{% compress js %}{% endraw %}{% endif %}{% raw %} {% endraw %}{% if cookiecutter.use_compressor == "y" %}{% raw %}{% endcompress %}{% endraw %}{% endif %}{% raw %} - +{% endraw %}{% endif %}{% raw %} {% endblock javascript %}