m-porter/vue-template-loader 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

m-porter/vue-template-loader

Composer 安装命令:

composer require m-porter/vue-template-loader

包简介

Laravel package which will download your html templates as built by webpack-html-loader for use in your projects.

README 文档

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 app entry with chainWebpack.

    // vue.config.js
    
    module.exports = {
    
    +    chainWebpack: (config) => {
    +        config.entryPoints.delete('app');
    +    },
    
    };
  • Update outputDir to point at laravel's public directory.

    // vue.config.js
    
    module.exports = {
    
    +    outputDir: '../public/assets',
    
        chainWebpack: (config) => {
            config.entryPoints.delete('app');
        },
    
    };
  • Update publicPath for both local development and prod. This will allow you to use both hmr and built files locally.

    NODE_ENV will already exist on process.env but WEBPACK_HOST and WEBPACK_PORT will 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 like dotenv to read your laravel .env file.

    // 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 pages to 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, resourcePath and filenameForEnv, were added to the config to help manage output file naming based on the current environment.

    resourcePath handles sending the built html template to the vue-template-loader expected resource_path('views/vue') directory it reads from on the PHP side.

  • Test it out! Run npm run serve from your frontend directory and modify your routes/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 .gitignore to avoid checking in built templates and files.

    public/assets/
    resources/views/vue/

m-porter/vue-template-loader 适用场景与选型建议

m-porter/vue-template-loader 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 23 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 03 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「html」 「loader」 「laravel」 「webpack」 「vue」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 m-porter/vue-template-loader 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 m-porter/vue-template-loader 我们能提供哪些服务?
定制开发 / 二次开发

基于 m-porter/vue-template-loader 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 23
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 7
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-03-15