Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

anonymouse703's avatar

How to apply downloaded theme in Laravel 10?

The problem I was confused is the downloaded theme used webpack in compiling assets while Laravel 10 used vite as default..

My question is how can I transfer the webpack configuration into vite configuration?

example

this is the them webpack conf

const path = require('path')
const glob = require('glob')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')

const INCLUDE_PATTERN = /<include src="(.+)"\s*\/?>(?:<\/include>)?/gi
const processNestedHtml = (content, loaderContext, dir = null) =>
  !INCLUDE_PATTERN.test(content)
    ? content
    : content.replace(INCLUDE_PATTERN, (m, src) => {
        const filePath = path.resolve(dir || loaderContext.context, src)
        loaderContext.dependency(filePath)
        return processNestedHtml(
          loaderContext.fs.readFileSync(filePath, 'utf8'),
          loaderContext,
          path.dirname(filePath)
        )
      })

// HTML generation
const paths = []
const generateHTMLPlugins = () =>
  glob.sync('./src/*.html').map((dir) => {
    const filename = path.basename(dir)

    if (filename !== '404.html') {
      paths.push(filename)
    }

    return new HtmlWebpackPlugin({
      filename,
      template: `./src/${filename}`,
      favicon: `./src/images/favicon.ico`,
      inject: 'body',
    })
  })

module.exports = {
  mode: 'development',
  entry: './src/js/index.js',
  devServer: {
    static: {
      directory: path.join(__dirname, './build'),
    },
    compress: true,
    port: 3000,
  },
  module: {
    rules: [
      {
        test: /\.m?js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env'],
            plugins: [
              [
                'prismjs',
                {
                  languages: ['javascript', 'css', 'markup'],
                  plugins: ['copy-to-clipboard'],
                  css: true,
                },
              ],
            ],
          },
        },
      },
      {
        test: /\.css$/i,
        use: [
          MiniCssExtractPlugin.loader,
          'css-loader',
          {
            loader: 'postcss-loader',
            options: {
              postcssOptions: {
                plugins: [
                  require('autoprefixer')({
                    overrideBrowserslist: ['last 2 versions'],
                  }),
                ],
              },
            },
          },
        ],
      },
      {
        test: /\.(png|svg|jpg|jpeg|gif)$/i,
        type: 'asset/resource',
      },
      {
        test: /\.(woff|woff2|eot|ttf|otf)$/i,
        type: 'asset/resource',
      },
      {
        test: /\.html$/,
        loader: 'html-loader',
        options: {
          preprocessor: processNestedHtml,
        },
      },
    ],
  },
  plugins: [
    ...generateHTMLPlugins(),
    new MiniCssExtractPlugin({
      filename: 'style.css',
      chunkFilename: 'style.css',
    }),
  ],
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'build'),
    clean: true,
    assetModuleFilename: '[path][name][ext]',
  },
  target: 'web', // fix for "browserslist" error message
  stats: 'errors-only', // suppress irrelevant log messages
}

and this is the Laravel 10 default vite conf

import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import vue from '@vitejs/plugin-vue';

export default defineConfig({
    plugins: [
        laravel({
            input: 'resources/js/app.js',
            refresh: true,
        }),
        vue({
            template: {
                transformAssetUrls: {
                    base: null,
                    includeAbsolute: false,
                },
            },
        }),
    ],
});
0 likes
13 replies
alden8's avatar

@anonymouse703

-- integrate theme files:

--- merge the theme's asset files (JavaScript, CSS, images, etc.) into your Laravel 10 project's resources directory, respecting their original structure

--- adjust any file paths within theme files to match their new locations

-- adapt Vite configuration:

--- install required CSS preprocessors (e.g., Sass, Less) via npm or yarn

--- add necessary plugins to your vite.config.js file

import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import laravel from 'laravel-vite-plugin';
import sass from 'vite-plugin-sass'; // Example for Sass

export default defineConfig({
  plugins: [
    laravel({
      input: 'resources/js/app.js',
      refresh: true,
    }),
    vue(),
    sass(), // Add CSS preprocessor plugin
  ],
});

--- JavaScript processing:

---- vite usually handles modern JavaScript features without transpilation

---- if the theme uses legacy features, consider Babel for transpilation. Install @vitejs/plugin-babel and configure it

--- HTML Templates:

---- Vite's @vitejs/plugin-vue handles Vue-based templates

---- for plain HTML templates, create separate HTML files and include them in your Blade views

---- if the theme uses html-loader, explore potential alternatives like vite-plugin-html or manual processing

--- Assets:

---- Vite automatically handles assets in public and resources directories

---- adjust paths in theme files accordingly

-- address missing features:

---- Webpack Plugins: Find Vite-compatible alternatives or implement custom solutions for missing Webpack plugins

---- MiniCssExtractPlugin: Vite handles CSS extraction differently. Use its built-in CSS handling or explore plugins like vite-plugin-css for more control

---- PrismJS: Integrate PrismJS using a Vite-compatible approach, such as a plugin or manual configuration

-- testing and adjustments:

---- thoroughly test the integrated theme to ensure it functions correctly with Vite

---- make necessary adjustments to paths, configurations, or code as needed

gych's avatar

@anonymouse703 You have multiple options to include the assets but I import them to app.js Then add app.js in the vite config input but not all assets need to be imported. Which assets exactly are you talking about?

alden8's avatar

@anonymouse703 In most cases, you don't need to explicitly include assets directly in vite.config.js, app.js, or app.blade.js when using Vite in Laravel 10. Vite automatically handles assets placed in specific directories within your project.

-- automatically Handled Asset Locations:

--- public directory: Any files placed in the public directory at the root of your project are served as-is during development and copied to the dist directory during build. This is ideal for static assets like images, fonts, and favicons.

--- resources directory: Vite scans the resources directory for specific file types and handles them accordingly:

--- JavaScript: Files with .js or .mjs extensions are transpiled (if necessary) and bundled using Vite's efficient bundling mechanism.

--- CSS: Files with .css or .less (if Sass is configured) extensions are processed based on your Vite configuration and injected into the build.

--- Images, fonts, etc.: Files like .png, .jpg, .svg, .woff, .eot, etc. are treated as assets and handled appropriately, either inlined or copied to the build directory.

-- including Assets Manually:

While Vite automatically handles most assets, there are situations where you might need to include them manually:

--- Non-default file types: If you have assets with non-standard extensions, you may need to configure Vite to recognize them using the assetsInclude option in vite.config.js.

--- Dynamic imports: For assets loaded dynamically based on user interaction, use JavaScript methods like import() or dynamic require() to include them at runtime.

--- HTML templating: Within your Blade templates, use standard Laravel directives like @asset() or and tags to reference asset paths.

Example:

In your Blade template (app.blade.php):

<img src="@asset('images/logo.png')" alt="Logo">
<link rel="stylesheet" href="@vite('css/main.css')">

This will automatically handle the asset processing and provide the correct paths during development and build.

anonymouse703's avatar

@alden8 so my setup right now is wrong? I did this in the app.js

import './bootstrap'; import '../css/app.css';

import '../assets/js/main.js'; import '../assets/css/style.css'; import '../assets/libs/simplebar/simplebar.min.css'; import '../assets/libs/@simonwep/pickr/themes/nano.min.css'; import '../assets/libs/jsvectormap/css/jsvectormap.min.css';

import '../assets/libs/apexcharts/apexcharts.min.js'; import '../assets/libs/chart.js/chart.min.js'; import '../assets/js/index.js'; import '../assets/libs/@popperjs/core/umd/popper.min.js'; import '../assets/libs/@simonwep/pickr/pickr.es5.min.js'; import '../assets/js/defaultmenu.js'; import '../assets/js/sticky.js'; import '../assets/js/switch.js'; import '../assets/libs/preline/preline.js'; import '../assets/libs/simplebar/simplebar.min.js'; import '../assets/js/custom.js'; import '../assets/js/custom-switcher.js';

gych's avatar

@anonymouse703 Import all your css files in app.css and all your js files in boostrap.js, in my opinion its a better approach and will keep your app.js much file cleaner.

anonymouse703's avatar

@gych I really didn't know how to apply theme in Laravel 10 - vue because I got errors like

perfect-scrollbar.min.js:15 Uncaught Error: no element is specified to initialize PerfectScrollbar

but in the pure laravel it seems ok

alden8's avatar

@anonymouse703 It looks like the javascript that forms the PerfectScrollbar on this page is connected, but the tag to which the PerfectScrollbar connection is configured in javascript is not. You need to look at what tag is configured in the PerfectScrollbar javascript and find out why this tag is not on the page.

anonymouse703's avatar

@gych I solved the perfect scrollbar but I have new problem and I can't trace it

dashboard:86 
        
        
        GET http://junkyard.test/[object%20Promise

Can I share the github repo? I tried to make a starter kits for Laravel-Vue

gych's avatar

@anonymouse703 Ok nice that you've already solved that error. You can share the link of the repo.

anonymouse703's avatar

but there are some effect that is not working. Like darkmode and toggle although I have no error in console

anonymouse703's avatar

@alden8 I have confusion,

If I do like this

 @vite([
        'resources/dist/bower_components/select2/dist/css/select2.min.css',
        'resources/dist/bower_components/bootstrap-daterangepicker/daterangepicker.css',
        'resources/dist/bower_components/dropzone/dist/dropzone.css',
        'resources/dist/bower_components/datatables.net-bs/css/dataTables.bootstrap.min.css',
        'resources/dist/bower_components/fullcalendar/dist/fullcalendar.min.css',
        'resources/dist/bower_components/perfect-scrollbar/css/perfect-scrollbar.min.css',
        'resources/dist/bower_components/slick-carousel/slick/slick.css',
        'resources/dist/css/main.css?version=4.5.0',
        'resources/dist/bower_components/jquery/dist/jquery.min.js',
        'resources/dist/bower_components/popper.js/dist/umd/popper.min.js',
        'resources/dist/bower_components/moment/moment.js',
        'resources/dist/bower_components/chart.js/dist/Chart.min.js',
        'resources/dist/bower_components/select2/dist/js/select2.full.min.js',
        'resources/dist/bower_components/jquery-bar-rating/dist/jquery.barrating.min.js',
        'resources/dist/bower_components/ckeditor/ckeditor.js',
        'resources/dist/bower_components/bootstrap-validator/dist/validator.min.js',
        'resources/dist/bower_components/bootstrap-daterangepicker/daterangepicker.js',
        'resources/dist/bower_components/ion.rangeSlider/js/ion.rangeSlider.min.js',
        'resources/dist/bower_components/dropzone/dist/dropzone.js',
        'resources/dist/bower_components/editable-table/mindmup-editabletable.js',
        'resources/dist/bower_components/datatables.net/js/jquery.dataTables.min.js',
        'resources/dist/bower_components/datatables.net-bs/js/dataTables.bootstrap.min.js',
        'resources/dist/bower_components/fullcalendar/dist/fullcalendar.min.js',
        'resources/dist/bower_components/perfect-scrollbar/js/perfect-scrollbar.jquery.min.js',
        'resources/dist/bower_components/tether/dist/js/tether.min.js',
        'resources/dist/bower_components/slick-carousel/slick/slick.min.js',
        'resources/dist/bower_components/bootstrap/js/dist/util.js',
        'resources/dist/bower_components/bootstrap/js/dist/alert.js',
        'resources/dist/bower_components/bootstrap/js/dist/button.js',
        'resources/dist/bower_components/bootstrap/js/dist/carousel.js',
        'resources/dist/bower_components/bootstrap/js/dist/collapse.js',
        'resources/dist/bower_components/bootstrap/js/dist/dropdown.js',
        'resources/dist/bower_components/bootstrap/js/dist/modal.js',
        'resources/dist/bower_components/bootstrap/js/dist/tab.js',
        'resources/dist/bower_components/bootstrap/js/dist/tooltip.js',
        'resources/dist/bower_components/bootstrap/js/dist/popover.js',
        'resources/dist/js/demo_customizer.js?version=4.5.0',
        'resources/dist/js/main.js?version=4.5.0',
        'resources/js/app.js',
        "resources/js/Pages/{$page['component']}.vue",
    ])
    @inertiaHead

I got errors

popper.min.js:4  Uncaught TypeError: Cannot set properties of undefined (setting 'Popper')
    at popper.min.js:4:147
    at popper.min.js:4:153
(anonymous) @ popper.min.js:4
(anonymous) @ popper.min.js:4
Show 2 more frames
Show less
moment.js:6  Uncaught TypeError: Cannot set properties of undefined (setting 'moment')
    at moment.js:6:19
    at moment.js:7:2
(anonymous) @ moment.js:6
(anonymous) @ moment.js:7
Show 2 more frames
Show less
ckeditor.js:195  Uncaught TypeError: Cannot read properties of undefined (reading 'document')
    at ckeditor.js:195:22
    at ckeditor.js:195:388
    at ckeditor.js:197:353
    at ckeditor.js:1260:20
(anonymous) @ ckeditor.js:195
(anonymous) @ ckeditor.js:195
(anonymous) @ ckeditor.js:197
(anonymous) @ ckeditor.js:1260
Show 4 more frames
Show less
daterangepicker.js:28  Uncaught TypeError: Cannot read properties of undefined (reading 'moment')
    at daterangepicker.js:28:45
    at daterangepicker.js:30:2
(anonymous) @ daterangepicker.js:28
(anonymous) @ daterangepicker.js:30
Show 2 more frames
Show less
fullcalendar.min.js:6  Uncaught TypeError: Cannot read properties of undefined (reading 'fn')
    at Object.<anonymous> (fullcalendar.min.js:6:15580)
    at e (fullcalendar.min.js:6:465)
    at Object.<anonymous> (fullcalendar.min.js:8:6924)
    at e (fullcalendar.min.js:6:465)
    at Object.<anonymous> (fullcalendar.min.js:6:24445)
    at e (fullcalendar.min.js:6:465)
    at Object.<anonymous> (fullcalendar.min.js:12:13902)
    at e (fullcalendar.min.js:6:465)
    at fullcalendar.min.js:6:822
    at fullcalendar.min.js:6:833

Bur if do like this

<head>
 <link href="{{ asset('dist/favicon.png') }}" rel="shortcut icon">
    <link href="{{ asset('dist/apple-touch-icon.png') }}" rel="apple-touch-icon">
    <link href="https://fonts.googleapis.com/css?family=Lato:300,400,700" rel="stylesheet" type="text/css">
    <link href="{{ asset('dist/bower_components/select2/dist/css/select2.min.css') }}" rel="stylesheet">
    <link href="{{ asset('dist/bower_components/bootstrap-daterangepicker/daterangepicker.css') }}" rel="stylesheet">
    <link href="{{ asset('dist/bower_components/dropzone/dist/dropzone.css') }}" rel="stylesheet">
    <link href="{{ asset('dist/bower_components/datatables.net-bs/css/dataTables.bootstrap.min.css') }}"
        rel="stylesheet">
    <link href="{{ asset('dist/bower_components/fullcalendar/dist/fullcalendar.min.css') }}" rel="stylesheet">
    <link href="{{ asset('dist/bower_components/perfect-scrollbar/css/perfect-scrollbar.min.css') }}" rel="stylesheet">
    <link href="{{ asset('dist/bower_components/slick-carousel/slick/slick.css') }}" rel="stylesheet">
    <link href="{{ asset('dist/css/main.css?version=4.5.0') }}" rel="stylesheet">

    <!-- Scripts -->
    @routes
    @vite(['resources/js/app.js', "resources/js/Pages/{$page['component']}.vue"])
    @inertiaHead
</head>


<body class="menu-position-side menu-side-left full-screen with-content-panel">
    @inertia
    <script src="{{ asset('dist/bower_components/jquery/dist/jquery.min.js') }}"></script>
    <script src="{{ asset('dist/bower_components/popper.js/dist/umd/popper.min.js') }}"></script>
    <script src="{{ asset('dist/bower_components/moment/moment.js') }}"></script>
    <script src="{{ asset('dist/bower_components/chart.js/dist/Chart.min.js') }}"></script>
    <script src="{{ asset('dist/bower_components/select2/dist/js/select2.full.min.js') }}"></script>
    <script src="{{ asset('dist/bower_components/jquery-bar-rating/dist/jquery.barrating.min.js') }}"></script>
    <script src="{{ asset('dist/bower_components/ckeditor/ckeditor.js') }}"></script>
    <script src="{{ asset('dist/bower_components/bootstrap-validator/dist/validator.min.js') }}"></script>
    <script src="{{ asset('dist/bower_components/bootstrap-daterangepicker/daterangepicker.js') }}"></script>
    <script src="{{ asset('dist/bower_components/ion.rangeSlider/js/ion.rangeSlider.min.js') }}"></script>
    <script src="{{ asset('dist/bower_components/dropzone/dist/dropzone.js') }}"></script>
    <script src="{{ asset('dist/bower_components/editable-table/mindmup-editabletable.js') }}"></script>
    <script src="{{ asset('dist/bower_components/datatables.net/js/jquery.dataTables.min.js') }}"></script>
    <script src="{{ asset('dist/bower_components/datatables.net-bs/js/dataTables.bootstrap.min.js') }}"></script>
    <script src="{{ asset('dist/bower_components/fullcalendar/dist/fullcalendar.min.js') }}"></script>
    <script src="{{ asset('dist/bower_components/perfect-scrollbar/js/perfect-scrollbar.jquery.min.js') }}"></script>
    <script src="{{ asset('dist/bower_components/tether/dist/js/tether.min.js') }}"></script>
    <script src="{{ asset('dist/bower_components/slick-carousel/slick/slick.min.js') }}"></script>
    <script src="{{ asset('dist/bower_components/bootstrap/js/dist/util.js') }}"></script>
    <script src="{{ asset('dist/bower_components/bootstrap/js/dist/alert.js') }}"></script>
    <script src="{{ asset('dist/bower_components/bootstrap/js/dist/button.js') }}"></script>
    <script src="{{ asset('dist/bower_components/bootstrap/js/dist/carousel.js') }}"></script>
    <script src="{{ asset('dist/bower_components/bootstrap/js/dist/collapse.js') }}"></script>
    <script src="{{ asset('dist/bower_components/bootstrap/js/dist/dropdown.js') }}"></script>
    <script src="{{ asset('dist/bower_components/bootstrap/js/dist/modal.js') }}"></script>
    <script src="{{ asset('dist/bower_components/bootstrap/js/dist/tab.js') }}"></script>
    <script src="{{ asset('dist/bower_components/bootstrap/js/dist/tooltip.js') }}"></script>
    <script src="{{ asset('dist/bower_components/bootstrap/js/dist/popover.js') }}"></script>
    <script src="{{ asset('dist/js/demo_customizer.js?version=4.5.0') }}"></script>
    <script src="{{ asset('dist/js/main.js?version=4.5.0') }}"></script>
    <script>
        (function(i, s, o, g, r, a, m) {
            i['GoogleAnalyticsObject'] = r;
            i[r] = i[r] || function() {
                (i[r].q = i[r].q || []).push(arguments)
            }, i[r].l = 1 * new Date();
            a = s.createElement(o),
                m = s.getElementsByTagName(o)[0];
            a.async = 1;
            a.src = g;
            m.parentNode.insertBefore(a, m)
        })(window, document, 'script', 'https://www.google-analytics.com/analytics.js', 'ga');

        ga('create', 'UA-XXXXXXXX-9', 'auto');
        ga('send', 'pageview');
    </script>

</body>

I have no error but some effects is not working like Toogle, Open Sidebar on mobile mode and others

Please or to participate in this conversation.