Created
April 20, 2018 00:19
-
-
Save NathanWorkman/0b44b969fdd77d6857c45e9622bcae66 to your computer and use it in GitHub Desktop.
A simple webpack config for Django
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| {% load render_bundle from webpack_loader %} | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <title>Document</title> | |
| {% render_bundle 'project' 'css' %} | |
| </head> | |
| <body> | |
| {% render_bundle 'project' 'js' %} | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | |
| PROJECT_ROOT = os.path.dirname(BASE_DIR) | |
| INSTALLED_APPS = [ | |
| ... | |
| 'webpack_loader', | |
| ... | |
| ] | |
| WEBPACK_LOADER = { | |
| 'DEFAULT': { | |
| 'CACHE': not DEBUG, | |
| 'BUNDLE_DIR_NAME': 'assets/', | |
| 'STATS_FILE': os.path.join(PROJECT_ROOT, 'webpack-bundle.json'), | |
| 'POLL_INTERVAL': 0.1, | |
| 'IGNORE': [ | |
| r'.+\.hot-update\.js', | |
| r'.+\.map' | |
| ] | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var BundleTracker = require('webpack-bundle-tracker'); | |
| var MiniCssExtractPlugin = require('mini-css-extract-plugin'); | |
| var path = require('path'); | |
| var webpack = require('webpack'); | |
| var autoprefixer = require('autoprefixer'); | |
| var resolve = path.resolve.bind(path, __dirname); | |
| var extractCssPlugin; | |
| var fileLoaderPath; | |
| var output; | |
| if (process.env.NODE_ENV === 'production') { | |
| output = { | |
| path: resolve('project/static/assets/'), | |
| filename: '[name].[chunkhash].js', | |
| chunkFilename: '[name].[chunkhash].js', | |
| publicPath: process.env.STATIC_URL || '/static/assets/' | |
| }; | |
| fileLoaderPath = 'file-loader?name=[name].[hash].[ext]'; | |
| extractCssPlugin = new MiniCssExtractPlugin({ | |
| filename: '[name].[chunkhash].css', | |
| chunkFilename: '[id].[chunkhash].css' | |
| }); | |
| } else { | |
| output = { | |
| path: resolve('project/static/assets/'), | |
| filename: '[name].[chunkhash].js', | |
| chunkFilename: '[name].[chunkhash].js', | |
| publicPath: '/static/assets/' | |
| }; | |
| fileLoaderPath = 'file-loader?name=[name].[ext]'; | |
| extractCssPlugin = new MiniCssExtractPlugin({ | |
| filename: '[name].[chunkhash].css', | |
| chunkFilename: '[name].[chunkhash].css' | |
| }); | |
| } | |
| var bundleTrackerPlugin = new BundleTracker({ | |
| filename: 'webpack-bundle.json' | |
| }); | |
| var providePlugin = new webpack.ProvidePlugin({ | |
| $: 'jquery', | |
| jQuery: 'jquery', | |
| 'window.jQuery': 'jquery', | |
| 'Popper': 'popper.js' | |
| }); | |
| var config = { | |
| entry: { | |
| project: './project/static/js/project.js', | |
| }, | |
| output: output, | |
| module: { | |
| rules: [ | |
| { | |
| test: /\.js$/, | |
| exclude: /node_modules/, | |
| loader: 'babel-loader' | |
| }, | |
| { | |
| test: /\.scss$/, | |
| use: [ | |
| MiniCssExtractPlugin.loader, | |
| { | |
| loader: 'css-loader', | |
| options: { | |
| 'sourceMap': true | |
| } | |
| }, | |
| { | |
| loader: 'postcss-loader', | |
| options: { | |
| 'sourceMap': true, | |
| 'plugins': function () { | |
| return [autoprefixer]; | |
| } | |
| } | |
| }, | |
| { | |
| loader: 'sass-loader', | |
| options: { | |
| 'sourceMap': true | |
| } | |
| } | |
| ] | |
| }, | |
| { | |
| test: /\.(eot|otf|png|svg|jpg|ttf|woff|woff2)(\?v=[0-9.]+)?$/, | |
| loader: fileLoaderPath, | |
| include: [ | |
| resolve('node_modules'), | |
| resolve('project/static/fonts'), | |
| resolve('project/static/images'), | |
| ] | |
| } | |
| ] | |
| }, | |
| plugins: [ | |
| bundleTrackerPlugin, | |
| extractCssPlugin, | |
| providePlugin | |
| ], | |
| resolve: { | |
| alias: { | |
| 'jquery': resolve('node_modules/jquery/dist/jquery.js') | |
| } | |
| } | |
| }; | |
| module.exports = config; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment