From d57a091d2d95e80ba9150d6ad9891f25a7063b79 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Mon, 1 Nov 2021 18:40:50 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E8=B0=83=E6=95=B4workbox=E7=9A=84?= =?UTF-8?q?=E6=AD=A3=E5=88=99=E8=A1=A8=E8=BE=BE=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/build/__tests__/utils.spec.ts | 14 ++++++++++++++ web/build/utils.ts | 5 +++++ web/build/webpack.config.ts | 3 ++- 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 web/build/__tests__/utils.spec.ts create mode 100644 web/build/utils.ts diff --git a/web/build/__tests__/utils.spec.ts b/web/build/__tests__/utils.spec.ts new file mode 100644 index 00000000..39070116 --- /dev/null +++ b/web/build/__tests__/utils.spec.ts @@ -0,0 +1,14 @@ +import { workboxPluginPattern } from '../utils'; + +describe('workboxPluginPattern', () => { + test.each([ + ['/plugins/com.msgbyte.foo/bar.js', true], + ['/plugins/com.msgbyte.foo.foz/bar.js', true], + ['/plugins/com.msgbyte.foo/index.js', false], + ['/plugins/com.msgbyte.foo/index-abcde.js', false], // TODO: 这个期望是true的。但是不会写正则 + ['/plugins/com.msgbyte.foo/index.woff', false], + ['/plugins/com.msgbyte.foo/font.woff', false], + ])('%s: %p', (input, output) => { + expect(workboxPluginPattern.test(input)).toBe(output); + }); +}); diff --git a/web/build/utils.ts b/web/build/utils.ts new file mode 100644 index 00000000..15c90526 --- /dev/null +++ b/web/build/utils.ts @@ -0,0 +1,5 @@ +/** + * workbox 匹配plugin的缓存 + */ +export const workboxPluginPattern = + /plugins\/com\.msgbyte(.*?)\/((?!index).)*?\.js/; diff --git a/web/build/webpack.config.ts b/web/build/webpack.config.ts index 4e2d37a8..c656e3a4 100644 --- a/web/build/webpack.config.ts +++ b/web/build/webpack.config.ts @@ -12,6 +12,7 @@ import CopyPlugin from 'copy-webpack-plugin'; import TsconfigPathsPlugin from 'tsconfig-paths-webpack-plugin'; import fs from 'fs'; import WorkboxPlugin from 'workbox-webpack-plugin'; +import { workboxPluginPattern } from './utils'; // eslint-disable-next-line @typescript-eslint/no-var-requires require('dotenv').config(); @@ -185,7 +186,7 @@ const config: Configuration = { }, { // 匹配内置 plugins 以加速 - urlPattern: /plugins\/com\.msgbyte(.*?)\.js/, + urlPattern: workboxPluginPattern, handler: 'CacheFirst', options: { cacheName: 'builtin-plugins',