From 6c05417b90c01c73119b96fa8db98f6dc60714d0 Mon Sep 17 00:00:00 2001 From: Jerome Caisip Date: Fri, 13 Sep 2019 11:58:23 +0800 Subject: [PATCH] Add StimulusJS integration. --- CONTRIBUTORS.rst | 2 + cookiecutter.json | 1 + docs/project-generation-options.rst | 6 +++ hooks/post_gen_project.py | 7 +++ .../package.json | 24 +++++++++ .../home/controllers/sample_controller.js | 5 ++ .../src/pages/home/index.js | 6 +++ .../webpack.config.dev.js | 51 +++++++++++++++++++ .../webpack.config.prod.js | 5 ++ 9 files changed, 107 insertions(+) create mode 100644 {{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/static/{{cookiecutter.project_slug}}/package.json create mode 100644 {{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/static/{{cookiecutter.project_slug}}/src/pages/home/controllers/sample_controller.js create mode 100644 {{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/static/{{cookiecutter.project_slug}}/src/pages/home/index.js create mode 100644 {{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/static/{{cookiecutter.project_slug}}/webpack.config.dev.js create mode 100644 {{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/static/{{cookiecutter.project_slug}}/webpack.config.prod.js diff --git a/CONTRIBUTORS.rst b/CONTRIBUTORS.rst index 4c3ad8a04..1bbbd26be 100644 --- a/CONTRIBUTORS.rst +++ b/CONTRIBUTORS.rst @@ -121,6 +121,7 @@ Listed in alphabetical order. Jan Van Bruggen `@jvanbrug`_ Jelmer Draaijer `@foarsitter`_ Jens Nilsson `@phiberjenz`_ + Jerome Caisip `@jeromecaisip`_ Jerome Leclanche `@jleclanche`_ @Adys Jimmy Gitonga `@afrowave`_ @afrowave John Cass `@jcass77`_ @cass_john @@ -276,6 +277,7 @@ Listed in alphabetical order. .. _@jangeador: https://github.com/jangeador .. _@jazztpt: https://github.com/jazztpt .. _@jcass77: https://github.com/jcass77 +.. _@jeromecaisip: https://github.com/jeromecaisip .. _@jleclanche: https://github.com/jleclanche .. _@juliocc: https://github.com/juliocc .. _@jvanbrug: https://github.com/jvanbrug diff --git a/cookiecutter.json b/cookiecutter.json index d6d217ca4..87f877e47 100644 --- a/cookiecutter.json +++ b/cookiecutter.json @@ -35,6 +35,7 @@ ], "custom_bootstrap_compilation": "n", "use_compressor": "n", + "use_stimulusJS": "n", "use_celery": "n", "use_mailhog": "n", "use_sentry": "n", diff --git a/docs/project-generation-options.rst b/docs/project-generation-options.rst index afa9b8aff..0304b89ca 100644 --- a/docs/project-generation-options.rst +++ b/docs/project-generation-options.rst @@ -78,6 +78,9 @@ custom_bootstrap_compilation: use_compressor: Indicates whether the project should be configured to use `Django Compressor`_. +use_stimulusJS: + Indicates whether the project should be configured to use `Stimulus JS`_. + use_celery: Indicates whether the project should be configured to use Celery_. @@ -138,3 +141,6 @@ debug: .. _Heroku: https://github.com/heroku/heroku-buildpack-python .. _Travis CI: https://travis-ci.org/ + +.. _Stimulus JS: https://stimulusjs.org/ + diff --git a/hooks/post_gen_project.py b/hooks/post_gen_project.py index ff84f1806..5aaa829d7 100644 --- a/hooks/post_gen_project.py +++ b/hooks/post_gen_project.py @@ -279,6 +279,10 @@ def remove_node_dockerfile(): shutil.rmtree(os.path.join("compose", "local", "node")) +def remove_stimulus_js_files(): + shutil.rmtree(os.path.join("{{cookiecutter.project_slug}}", "static", "{{cookiecutter.project_slug}}")) + + def main(): debug = "{{ cookiecutter.debug }}".lower() == "y" @@ -342,6 +346,9 @@ def main(): if "{{ cookiecutter.use_travisci }}".lower() == "n": remove_dottravisyml_file() + if "{{ cookiecutter.use_stimulusJS }}".lower() == "n": + remove_stimulus_js_files() + print(SUCCESS + "Project initialized, keep up the good work!" + TERMINATOR) diff --git a/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/static/{{cookiecutter.project_slug}}/package.json b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/static/{{cookiecutter.project_slug}}/package.json new file mode 100644 index 000000000..d79e13500 --- /dev/null +++ b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/static/{{cookiecutter.project_slug}}/package.json @@ -0,0 +1,24 @@ +{ + "name": "static", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "dev": "webpack --watch --config webpack.config.dev.js", + "build": "webpack --config webpack.config.prod.js" + }, + "keywords": [], + "author": "", + "license": "ISC", + "devDependencies": { + "@babel/core": "^7.4.5", + "@babel/preset-env": "^7.4.5", + "axios": "^0.19.0", + "babel-loader": "^8.0.6", + "babel-plugin-transform-class-properties": "^6.24.1", + "stimulus": "^1.1.1", + "webpack": "^4.34.0", + "webpack-cli": "^3.3.4" + }, + "dependencies": {} +} diff --git a/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/static/{{cookiecutter.project_slug}}/src/pages/home/controllers/sample_controller.js b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/static/{{cookiecutter.project_slug}}/src/pages/home/controllers/sample_controller.js new file mode 100644 index 000000000..c15ff4c76 --- /dev/null +++ b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/static/{{cookiecutter.project_slug}}/src/pages/home/controllers/sample_controller.js @@ -0,0 +1,5 @@ +import {Controller} from "stimulus" + +export default class extends Controller { + +} diff --git a/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/static/{{cookiecutter.project_slug}}/src/pages/home/index.js b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/static/{{cookiecutter.project_slug}}/src/pages/home/index.js new file mode 100644 index 000000000..cee63678a --- /dev/null +++ b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/static/{{cookiecutter.project_slug}}/src/pages/home/index.js @@ -0,0 +1,6 @@ +import {Application} from "stimulus" +import {definitionsFromContext} from "stimulus/webpack-helpers" + +const application = Application.start(); +const context = require.context("./controllers", true, /\.js$/); +application.load(definitionsFromContext(context)); diff --git a/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/static/{{cookiecutter.project_slug}}/webpack.config.dev.js b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/static/{{cookiecutter.project_slug}}/webpack.config.dev.js new file mode 100644 index 000000000..387bcb3b4 --- /dev/null +++ b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/static/{{cookiecutter.project_slug}}/webpack.config.dev.js @@ -0,0 +1,51 @@ +const path = require('path'); + +module.exports = { + entry: { + 'home': './src/pages/home/index.js', + }, + output: { + filename: '[name].js', + path: path.resolve(__dirname, 'dist'), + publicPath: 'static/' + }, + mode: 'development', + optimization: { + splitChunks: { + minSize: 0, + cacheGroups: { + commons: { + name: 'commons', + chunks: 'all', + } + } + } + }, + module: { + rules: [ + { + test: /\.(png|jpg)$/, + use: [ + 'file-loader' + ] + }, + { + test: /\.css$/, + use: [ + 'css-loader', + ] + }, + { + test: /\.js$/, + exclude: /node_modules/, + use: { + loader: "babel-loader", + options: { + presets: ['@babel/env'], + plugins: ['transform-class-properties'] + } + } + }, + ] + } +}; diff --git a/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/static/{{cookiecutter.project_slug}}/webpack.config.prod.js b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/static/{{cookiecutter.project_slug}}/webpack.config.prod.js new file mode 100644 index 000000000..535176759 --- /dev/null +++ b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/static/{{cookiecutter.project_slug}}/webpack.config.prod.js @@ -0,0 +1,5 @@ +const path = require('path'); +const dev_exports = require('./webpack.config.dev'); + +dev_exports['mode'] = 'production'; +module.exports = dev_exports;