Let's make Masonite Framework and Laravel Mix work together
Junior Gantin
Posted on August 22, 2018
Masonite is a beautifully crafted Web Framework for Python. We usually use files like CSS, JavaScript and image files known as Web assets to make our web app looks great.
In this article, I'll show you how you can use Laravel Mix for processing and compiling assets into your Masonite web app.
What is Laravel Mix?
Laravel Mix makes asset compiling incredibly easy.
Using Laravel Mix with Masonite is really a simple task. There we go!
Create a new Masonite project
Before we get started, create a new Masonite project. Just install Masonite's CLI called craft.
$ pip install masonite-cli
$ craft new masonite_laravel_mix
$ cd masonite_laravel_mix
$ craft install
Install and setup Laravel Mix
Laravel Mix can be used for any type of application, not just for Laravel apps. To get started, just install laravel-mix as our project dependency.
This is definition of our asset pipeline. It's time to add some npm scripts.
"scripts": {
"dev": "npm run development",
"development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"watch": "npm run development -- --watch",
"watch-poll": "npm run watch -- --watch-poll",
"hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
"prod": "npm run production",
"production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
}
I just copied this npm scripts from Laravel repository - again π .
This scripts helps in asset compilation for development or production.
As you can see it, you need to install cross-env to make it works well.
$ npm install cross-env
Now that weβve done all the hard work, letβs go ahead and a simple html file.
Simple thing to make all this stuff work is to create a template alias. All configurations that are specific to static files can be found in config/storage.py.
In this file we'll add a constant to STATICFILES which is simply a dictionary:
Hopefully this article has helped you understand how Masonite and Laravel Mix can be used together for processing and compiling assets. If you want to contribute or interested in the development of Masonite then be sure to join the Slack or star Masonite's repository on GitHub.