Skip to content

Commit

Permalink
Add webpack as an option
Browse files Browse the repository at this point in the history
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 <project_slug>/static/<project_slug>/ 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.
  • Loading branch information
goldhand committed Jun 19, 2016
1 parent d771f12 commit f036ae4
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ Listed in alphabetical order.
Travis McNeill `@Travistock`_ @tavistock_esq
Vitaly Babiy
Vivian Guillen `@viviangb`_
Will Farley `@goldhand`_ @g01dhand
Yaroslav Halchenko
========================== ============================ ==============

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}
30 changes: 30 additions & 0 deletions hooks/post_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import os
import random
import shutil
from cookiecutter.main import cookiecutter

# Get the root project directory
PROJECT_DIRECTORY = os.path.realpath(os.path.curdir)
Expand Down Expand Up @@ -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(
'[email protected]: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
Expand Down Expand Up @@ -212,6 +237,11 @@ def remove_certbot_files():
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()
Expand Down
10 changes: 10 additions & 0 deletions {{cookiecutter.project_slug}}/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
13 changes: 13 additions & 0 deletions {{cookiecutter.project_slug}}/config/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,5 +243,18 @@

# 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
14 changes: 14 additions & 0 deletions {{cookiecutter.project_slug}}/config/settings/production.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,5 +303,19 @@
{% 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
5 changes: 5 additions & 0 deletions {{cookiecutter.project_slug}}/requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% raw %}{% load staticfiles i18n {% endraw %}{% if cookiecutter.use_compressor == "y" %}compress{% endif %}{% raw %}%}<!DOCTYPE html>
{% 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 %}<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
Expand Down Expand Up @@ -76,7 +77,9 @@
<div class="alert {% if message.tags %}alert-{{ message.tags }}{% endif %}">{{ message }}</div>
{% endfor %}
{% endif %}

{% endraw %}{% if cookiecutter.js_task_runner == 'Webpack' %}{% raw %}
<div id="main"></div>
{% endraw %}{% endif %}{% raw %}
{% block content %}
<p>Use this document as a way to quick start any new project.</p>
{% endblock content %}
Expand All @@ -99,12 +102,17 @@
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.2/js/bootstrap.min.js" integrity="sha384-vZ2WRJMwsjRMW/8U7i6PWi6AlO1L79snBrmgiDpgIWJ82z8eA5lenwvxbMV1PAh7" crossorigin="anonymous"></script>

<!-- Your stuff: Third-party javascript libraries go here -->

{% endraw %}{% if cookiecutter.js_task_runner == 'Webpack' %}{% raw %}
<!-- Webpack bundles -->
{% render_bundle 'vendor' %}
{% render_bundle 'common' %}
{% render_bundle 'main' %}
{% endraw %}{% else %}{% raw %}
<!-- place project specific Javascript in this file -->
{% endraw %}{% if cookiecutter.use_compressor == "y" %}{% raw %}{% compress js %}{% endraw %}{% endif %}{% raw %}
<script src="{% static 'js/project.js' %}"></script>
{% endraw %}{% if cookiecutter.use_compressor == "y" %}{% raw %}{% endcompress %}{% endraw %}{% endif %}{% raw %}

{% endraw %}{% endif %}{% raw %}
{% endblock javascript %}
</body>
</html>
Expand Down

0 comments on commit f036ae4

Please sign in to comment.