0

我正在尝试使用 Laravel 和 Vue.js 构建具有离线支持的 PWA。我正在使用 laravel-mix-workbox 插件来设置我的服务工作者,但是我在尝试完成应该是一项简单的任务时遇到了很多麻烦。我有一些从我的应用程序中提供的静态资产(图像、XML 文件等),我无法让工作箱将它们添加到预缓存文件列表中。

我尝试将资产移动到 /resources/img 并添加对 copyDirectory 的调用以尝试将它们包含在内,此外,我尝试了 webpack-copy-plugin,但只包含编译后的资产(js、css、字体等)。这是我的 webpack.mix.js 文件:

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

//mp035 add workbox plugin and copy-webpack-plugin
require('laravel-mix-workbox');
const CopyPlugin = require('copy-webpack-plugin');

/*
 |--------------------------------------------------------------------------
 | 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.
 |
 */

//mp035 fix issue with laravel-mix outputting bad urls in precache manifest for app.js (//js/app.js) and app.css
// and copy assets into place (so they are in the build tree)
mix.webpackConfig({
    output: {
      publicPath: ''
    },
    plugins: [
        new CopyPlugin([
          { from: 'resources/img/*', to: 'public/img', flatten:true },
          { from: 'resources/root/*', to: 'public', flatten:true },
        ]),
      ],
  })


.js('resources/js/app.js', 'public/js')
   .sass('resources/sass/app.scss', 'public/css')
   .sourceMaps().version()

// mp035 add inject manifest plugin to inject workbox manifest into the service worker.
.injectManifest({
    swSrc: './resources/pwa/service-worker.js',
    maximumFileSizeToCacheInBytes: 20000000, // ******************************DEBUG ONLY!!!
});

有谁知道如何将我的/resources/img(或/public/img)中的所有文件包含在预缓存文件列表中?

4

1 回答 1

1

好的,所以看起来这是 laravel-mix-workbox 的问题。删除它并使用通用工作箱 webpack 插件解决了这个问题。对于任何发现这一点的人,这里是更新的 webpack.mix.js:

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

//mp035 add workbox plugin and copy-webpack-plugin
const CopyPlugin = require('copy-webpack-plugin');
const {InjectManifest} = require('workbox-webpack-plugin');

/*
 |--------------------------------------------------------------------------
 | 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.
 |
 */

//mp035 fix issue with laravel-mix outputting bad urls in precache manifest for app.js (//js/app.js) and app.css
// and copy assets into place (so they are in the build tree)
mix.webpackConfig({
    output: {
      publicPath: ''
    },
    plugins: [
        new CopyPlugin([
          { from: 'resources/img/*', to: 'public/img', flatten:true },
          { from: 'resources/root/*', to: 'public', flatten:true },
        ]),
        new InjectManifest({
            swSrc: './resources/pwa/service-worker.js',
            maximumFileSizeToCacheInBytes: 20000000, // ******************************DEBUG ONLY!!!
        }),
      ],
  })


.js('resources/js/app.js', 'public/js')
   .sass('resources/sass/app.scss', 'public/css')
   .sourceMaps().version();

总而言之,将工作箱与 laravel-mix 一起使用是一个非常痛苦的过程,因为 laravel-mix 所做的所有“小”调整都会破坏工作箱插件。如果可能的话,我建议坚持使用普通的 webpack。

于 2020-04-22T08:51:13.337 回答