【個人的メモ】Wagtail、webpack をインストール

すいません！超個人的メモです

Ubuntu 19.04

## Wagtail

```sh
pip install wagtail
```

```sh
mkdir PROJECTNAME
cd PROJECTNAME
wagtail start MYSITE .
```

## webpack

[django-webpack-loader のセットアップ](https://qiita.com/gsk3beta/items/c2b902c3659610701071)

```
npm init -y
npm install --save-dev webpack webpack-cli webpack-bundle-tracker
```

`webpack.config.js`を作成して編集

```sh
touch webpack.config.js
```

```js:webapck.config.js
var path = require("path")
var webpack = require('webpack')
var BundleTracker = require('webpack-bundle-tracker')

module.exports = {
  mode: 'development',
  context: __dirname,
  entry: {
      // pass
  },

  output: {
      path: path.resolve('./assets/bundles/'),
      filename: "[name]-[hash].js",
  },

  plugins: [
    new BundleTracker({filename: './webpack-stats.json'}),
  ],

  module: {
    rules: [
      // pass
    ]
  }
}
```

webpackの入出力に必要なフォルダの作成

```sh
mkdir -p assets/js
mkdir assets/css
mkdir assets/bundles
```
## Bulma

動作確認も含めてBulmaをインストールしてみます

[webpackでBulmaを使いたい](https://qiita.com/gsk3beta/items/280d38f016a5bbe966b0)

```
npm install --save-dev node-sass sass fibers
npm install --save-dev style-loader css-loader postcss-loader precss autoprefixer sass-loader
npm install --save-dev　bulma
```

```js:webpack.config.js
...

  entry: {
    main_css: './assets/css/main.scss',
  },

...

  module: {
    rules: [
      {
        test: /\.(scss)$/,
        use: [{
          loader: 'style-loader',
        }, {
          loader: 'css-loader',
        }, {
          loader: 'postcss-loader',
          options: {
            plugins: function () {
              return [
                require('precss'),
                require('autoprefixer')
              ];
            }
          }
        }, {
          loader: 'sass-loader'
        }]
      }
    ]
  }
...
```

```scss:assets/css/main.scss
@charset "utf-8";
@import "~bulma/bulma";
```

## django-webpack-loader

再び、[django-webpack-loader のセットアップ](https://qiita.com/gsk3beta/items/c2b902c3659610701071)

```sh
pip install django-webpack-loader
```

```py:MYSITE/settings/base.py
...

INSTALLED_APPS = (
 ...
 'webpack_loader',
)

...

STATICFILES_DIRS = (
    ...
    os.path.join(BASE_DIR, 'assets'),
)

...
# 下記4行は必要ありません
try:
    DEBUG
except NameError:
    DEBUG = True

WEBPACK_LOADER = {
    'DEFAULT': {
        'CACHE': not DEBUG,
        'BUNDLE_DIR_NAME': 'bundles/',
        'STATS_FILE': os.path.join(BASE_DIR, './webpack-stats.json'),
        'POLL_INTERVAL': 0.1,
        'TIMEOUT': None,
        'IGNORE': [r'.+\.hot-update.js', r'.+\.map'],
    }
}
```

watchモードでバンドル

```sh
./node_modules/.bin/webpack --config webpack.config.js --watch
```

```html:home/templates/home/home_page.html
{% extends "base.html" %}
{% load static %}
{% load wagtailcore_tags wagtailimages_tags %}
{% load render_bundle from webpack_loader %}

{% block body_class %}homepage{% endblock %}

{% block extra_css %}
{% render_bundle 'main_css' %}
{% endblock extra_css %}

{% block content %}
<section class="section">
  <div class="container">
    <h1 class="title">
      個人的メモ
    </h1>
    <p class="subtitle">
    <strong>備忘のために</strong>書きました
    </p>
  </div>
</section>
{% endblock content %}

{% block extra_js %}
{% endblock %}
```

![Screenshot from 2019-10-24 00-05-27.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/79562/84414861-f636-84ab-5fa4-cef47e08a5a3.png)