【问题标题】:Laravel Vue Non SPA - How to split the files for every page for its vue js components?Laravel Vue Non SPA - 如何为其 vue js 组件拆分每个页面的文件?
【发布时间】:2023-03-21 19:03:01
【问题描述】:

我的应用程序使用的是 laravel vue 但不是 SPA,所以我仍然使用 laravel 刀片来分隔页面。每个页面都在导入 app.js。我的 App.js 是由 webpack 编译的,我所有的 vue 组件都是在该文件上编译的。所以问题是 app.js 获得 MB 大小,并且每个页面加载该文件的速度都会变慢。他们的方式是拆分vuejs的代码还是使用webpack为每个页面分离文件?

这是我的 web 应用程序中的 webpack.js。

const mix = require('laravel-mix');

/*
 |--------------------------------------------------------------------------
 | Mix Asset Management
 |--------------------------------------------------------------------------
 |
 | Mix provides a clean, fluent API for defining some Webpack build steps
 | for your Laravel application. By default, we are compiling the Sass
 | file for the application as well as bundling up all the JS files.
 |
 */

mix.setPublicPath('public')
    .setResourceRoot('../') // Turns assets paths in css relative to css file
    // .options({
    //     processCssUrls: false,
    // })
    .sass('resources/sass/frontend/app.scss', 'css/frontend.css')
    .sass('resources/sass/backend/app.scss', 'css/backend.css')
    .js('resources/js/frontend/app.js', 'js/app.js')
    .js([
        'resources/js/backend/before.js',
        'resources/js/backend/app.js',
        'resources/js/backend/after.js'
    ], 'js/backend.js')

    .extract([
        // Extract packages from node_modules to vendor.js
        'jquery',
        'bootstrap',
        'popper.js',
        'axios',
        'sweetalert2',
        'lodash'
    ])
    .sourceMaps();

if (mix.inProduction()) {
    mix.version()
        .options({
            // Optimize JS minification process
            terser: {
                cache: true,
                parallel: true,
                sourceMap: true
            }
        });
} else {
    // Uses inline source-maps on development
    mix.webpackConfig({
        devtool: 'inline-source-map'
    });
}

我的 laravel 使用的是 laravel 样板模板,他将其他文件分离到 vender.js 中。我的问题是 app.js 变得越来越大,并且由于连接速度较慢的用户很难加载。

【问题讨论】:

    标签: laravel vue.js webpack vuejs2


    【解决方案1】:

    据我所知,您能做的最好的事情是使用Dynamic Importslaravel-mix 在内部使用webpack code-splitting,并且它已包含在最新版本的laravel-mix 中。

    将此添加到您的 webpack.mix.js 文件中

    mix.babelConfig({
      plugins: ['@babel/plugin-syntax-dynamic-import'],
    });
    

    您还需要更改import 组件的方式。

    // Standard import
    import StandardComponent from './components/ExampleComponent.vue';
    
    // Dynamic import
    const DynamicallyImportedComponent =
            () => import('./components/ExampleComponent.vue');
    

    通过这种方式laravel-mix 将在单独的文件中编译组件。并在您的htmlhead 部分中动态插入文件。所以只会在需要时插入文件。

    Webpack 会将动态导入的文件拆分成块,并命名为0.js1.js 等。如果要为文件配置块名,需要在导入语句中添加“神奇”注释告诉 Webpack 你想使用的名字。就像您在 webpack 设置中所做的那样。喜欢:

    const DynamicallyImportedComponent =
            () => import(/* webpackChunkName: "dynamically-imported-component" */ './components/ExampleComponent.vue');
    

    【讨论】:

    • 谢谢@Andy,。这很棒!,。我会研究它...但我需要先 npm 安装@babel/plugin-syntax-dynamic-import 吗?并且 vue 应用程序将导入 DynamicallyImportedComponent?
    • @LeopoldoAbing 你不需要安装@babel/plugin-syntax-dynamic-import,它包含在最新版本的laravel-mix中。
    • @LeopoldoAbing 第二个问题,不行,你需要自己注册例如new Vue({ el: '#app', components: { 'component-a': ComponentA } })
    • @LeopoldoAbing 我希望这一切都对你有意义。
    • 我检查了编译的css文件,它是空的,。
    猜你喜欢
    • 1970-01-01
    • 2021-06-07
    • 2018-12-16
    • 2020-06-05
    • 2018-04-18
    • 1970-01-01
    • 2018-08-01
    • 2021-03-27
    • 2017-03-20
    相关资源
    最近更新 更多