I have an Angular 4/Django application, all of the angular code in a Django application. The angular app is built using webpack.
I would like webpack to output the .js bundles into the "static" folder and an "index.html" file into the "templates" folder. The <script></script>
tags that are injected will also need to be updated to use Django's "static files" notation:
{% load static %}
<script type="text/javascript" src="{% static 'dist/polyfills.js' %}"></script>
Here is my directory structure (some files omitted for brevity), where project
is the top-level holding the Django project.
project
+ angular
+ app (angular application root)
+ config
+ node_modules
+ src
- webpack.config.js
+ static
+ dist ( webpack output folder)
- app.[hash].js
- vendor.[hash].js
- polyfills.[hash.js
- index.html
+ templates
- index.html
- Note: I know I can copy/move the index.html quite easily, but I need the tags to also use Django's static files.
For clarification. Here is my current "templates/index.html" file that is served by Django. This works fine as it is. I would like to have webpack generating this file if possible because I want to use the [hash] in the naming of the dis files and have that updated automatically here. So I would have app.[hash].js
, vendor.[hash].js
, polyfill.[hash].js
and the Django template would be updated when my angular app is built by webpack.
templates/index.html
{% load static %}
<html>
<head>
<base href="/">
<title>App</title>
<link href="{% static 'dist/app.css' %}" rel="stylesheet">
</head>
<body>
<my-app>Loading...</my-app>
<script type="text/javascript" src="{% static 'dist/polyfills.js' %}"></script>
<script type="text/javascript" src="{% static 'dist/vendor.js' %}"></script>
<script type="text/javascript" src="{% static 'dist/app.js' %}"></script>
</body>
</html>