Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Ostap Brehin
Ostap Brehin

Posted on • Edited on

     

How to use Preact with Laravel Mix

Update 2023: I highly advise using Vite with their officialpreset, as it's much easier to configure and miles faster than Webpack.

In this article, I share my configuration forPreact usage withLaravel Mix.

Install these dependencies before you get started:

  • preact
  • @babel/plugin-proposal-class-properties
  • @babel/preset-react

You can do it with this command:

npminstallpreact @babel/plugin-proposal-class-properties @babel/preset-react
Enter fullscreen modeExit fullscreen mode

Next, add this content to yourwebpack.mix.js

// webpack.mix.jsconstmix=require('laravel-mix');mix.webpackConfig({"resolve":{"alias":{"react":"preact/compat","react-dom":"preact/compat"}}});mix.babelConfig({"plugins":["@babel/plugin-proposal-class-properties"],});mix.react('resources/js/app.js','public/js');
Enter fullscreen modeExit fullscreen mode

And here is an example app component, you can store it inresources/js/app.js file:

// app.jsimport{h,render,Component}from'preact';window.React=require('preact');classAppextendsComponent{render(){return<h1>PreactfromLaravelMix</h1>;}}render(<App/>,document.body);
Enter fullscreen modeExit fullscreen mode

Note: As we set out rendering node asdocument.body, you should place your script tag inside<body> tag. If you need to place<script> tag in the<head> section - you will need to adddefer attribute.

<body><script src="{{ mix('js/app.js') }}"></script></body>
Enter fullscreen modeExit fullscreen mode

That's all, happy coding! :)

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
abdes profile image
Zakari Abdessamad
Web Developer focused on PHP and JavaScript (Laravel,React,VueJS)
  • Location
    Germany
  • Joined
• Edited on• Edited

Thanks for the post ... but What's benfit of using "babel/plugin-proposal-class-properties"?

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

19 y.o maker from 🇺🇦 Ukraine
  • Location
    🇬🇧 England
  • Joined

More fromOstap Brehin

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp