m-porter / vue-template-loader
Laravel package which will download your html templates as built by webpack-html-loader for use in your projects.
Installs: 23
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/m-porter/vue-template-loader
Requires
- php: >=7.2.5
- ext-curl: *
- illuminate/support: ^6.0|^7.0|^8.0|^9.0
Requires (Dev)
This package is auto-updated.
Last update: 2025-10-29 02:23:29 UTC
README
Laravel package which will download your html templates as built by webpack-html-loader for use in your projects.
Intended for use with vue-cli.
Installation
Require this package with composer.
composer require m-porter/vue-template-loader
| Laravel version | This package version |
|---|---|
| ^6.0|^7.0 | ^6.0.0 |
| ^5.5,<5.9 | ^0.2.0 |
Package Discovery
If you don't use auto-discovery, add the ServiceProvider to the providers array in config/app.php.
MPorter\VueTemplateLoader::class,
Configuration
Copy the package config to your local config with artisan's vendor:publish command:
php artisan vendor:publish --provider="MPorter\VueTemplateLoader\ServiceProvider"
Usage
This package is very opinionated and requires changes to your default vue.config.js file. Vue-template-loader is intended to be used with vue-cli's multi-page mode.
You can assume the following laravel project structure for this usage tutorial. (Modified directory structure after running vue create frontend).
<laravel project root>/frontend
├── package-lock.json
├── package.json
├── src
│ ├── example-app
│ │ ├── App.vue
│ │ ├── assets
│ │ │ └── logo.png
│ │ ├── components
│ │ │ └── HelloWorld.vue
│ │ ├── index.blade.php
│ │ └── main.js
└── vue.config.js
-
Remove the default
appentry withchainWebpack.// vue.config.js module.exports = { + chainWebpack: (config) => { + config.entryPoints.delete('app'); + }, }; -
Update
outputDirto point at laravel'spublicdirectory.// vue.config.js module.exports = { + outputDir: '../public/assets', chainWebpack: (config) => { config.entryPoints.delete('app'); }, }; -
Update
publicPathfor both local development and prod. This will allow you to use both hmr and built files locally.NODE_ENVwill already exist onprocess.envbutWEBPACK_HOSTandWEBPACK_PORTwill not. You will need to either add it to your npm scripts (e.g.WEBPACK_HOST=0.0.0.0 vue-cli-service serve) or use a npm package likedotenvto read your laravel.envfile.// vue.config.js + const isProd = process.env.NODE_ENV === 'production'; + const host = process.env.WEBPACK_HOST || '0.0.0.0'; + const port = process.env.WEBPACK_PORT || 8080; module.exports = { + publicPath: isProd ? '/assets' : `http://${host}:${port}/`, outputDir: '../public/assets', chainWebpack: (config) => { config.entryPoints.delete('app'); }, };
-
Update
pagesto include your frontend app. For this example, you can assume the following app structure.NOTE: Your folder CANNOT be named
app. This is a reserved folder name in vue-cli.// vue.config.js const isProd = process.env.NODE_ENV === 'production'; const host = process.env.WEBPACK_HOST || '0.0.0.0'; const port = process.env.WEBPACK_PORT || 8080; + const resourcePath = (n) => path.join('../../resources/views/vue', `${n}.blade.php`); + const filenameForEnv = (n) => (isProd ? resourcePath(n) : `${n}.html`); module.exports = { + pages: { + 'example-app': { + title: 'Example App', + entry: 'src/example-app/main.js', + template: 'src/example-app/index.blade.php', + filename: filenameForEnv('example-app'), + }, + }, publicPath: isProd ? '/assets' : `http://${host}:${port}/`, outputDir: '../public/assets', chainWebpack: (config) => { config.entryPoints.delete('app'); }, };
Two helper functions,
resourcePathandfilenameForEnv, were added to the config to help manage output file naming based on the current environment.resourcePathhandles sending the built html template to the vue-template-loader expectedresource_path('views/vue')directory it reads from on the PHP side. -
Test it out! Run
npm run servefrom your frontend directory and modify yourroutes/web.php.// routes/web.php use Illuminate\Support\Facades\Route; use MPorter\VueTemplateLoader\Loader; Route::get('/', function () { return view(Loader::getTemplate('example-app')); });
You should now be able to see the default vue page!
-
Update your
.gitignoreto avoid checking in built templates and files.public/assets/ resources/views/vue/