From cfa5d3b72eaefb4f3e0f88661adcd905d29d4b75 Mon Sep 17 00:00:00 2001 From: Cezar Augusto Date: Tue, 3 Sep 2024 14:19:31 -0300 Subject: [PATCH] Fix background service_worker not reloading --- .../webpack/lib/__spec__/utils.spec.ts | 2 +- .../data/manifest-fields/index.ts | 36 +++++++------------ .../manifest-fields/locales-fields/index.ts | 2 +- .../feature-scripts/steps/add-scripts.ts | 18 +++++----- .../web-socket-server/message-dispatcher.ts | 8 ++--- 5 files changed, 28 insertions(+), 38 deletions(-) diff --git a/programs/develop/webpack/lib/__spec__/utils.spec.ts b/programs/develop/webpack/lib/__spec__/utils.spec.ts index 84b6f73b..626d223f 100644 --- a/programs/develop/webpack/lib/__spec__/utils.spec.ts +++ b/programs/develop/webpack/lib/__spec__/utils.spec.ts @@ -98,7 +98,7 @@ describe('utils', () => { updateHash: () => {}, buffer: () => { return Buffer.from(JSON.stringify(manifestContent)) - }, + } } } } diff --git a/programs/develop/webpack/plugin-extension/data/manifest-fields/index.ts b/programs/develop/webpack/plugin-extension/data/manifest-fields/index.ts index cbdea486..4d2f0713 100644 --- a/programs/develop/webpack/plugin-extension/data/manifest-fields/index.ts +++ b/programs/develop/webpack/plugin-extension/data/manifest-fields/index.ts @@ -2,10 +2,11 @@ import path from 'path' import {htmlFields} from './html-fields' import {iconFields} from './icons-fields' import {jsonFields} from './json-fields' -// import {localesFields} from './locales-fields' +import {localesFields} from './locales-fields' import {scriptsFields} from './scripts-fields' import {webResourcesFields} from './web-resources-fields' -import {type PluginInterface, type Manifest} from '../../../webpack-types' +import {type PluginInterface} from '../../../webpack-types' +import * as utils from '../../../lib/utils' // TODO: cezaraugusto type this export interface ManifestFields { @@ -17,35 +18,22 @@ export interface ManifestFields { web_accessible_resources: Record } -function removePrefixes(manifest: Manifest): Manifest { - function recursivelyRemovePrefixes(obj: Manifest): Manifest { - const result: Manifest = {} - for (const key in obj) { - if (obj.hasOwnProperty(key)) { - const newKey = key.includes(':') ? key.split(':')[1] : key - if (typeof obj[key] === 'object' && !Array.isArray(obj[key])) { - result[newKey] = recursivelyRemovePrefixes(obj[key]) - } else { - result[newKey] = obj[key] - } - } - } - return result - } - - return recursivelyRemovePrefixes(manifest) -} - -export function getManifestFieldsData({manifestPath}: PluginInterface) { +export function getManifestFieldsData({ + manifestPath, + browser +}: PluginInterface) { const context = path.dirname(manifestPath) const manifest = require(manifestPath) - const manifestNoPrefixes = removePrefixes(manifest) + const manifestNoPrefixes = utils.removeManifestKeysNotFromCurrentBrowser( + manifest, + browser || 'chrome' + ) const fieldData = { html: htmlFields(context, manifestNoPrefixes), icons: iconFields(context, manifestNoPrefixes), json: jsonFields(context, manifestNoPrefixes), - // locales: localesFields(context, manifestPath), + locales: localesFields(context, manifestPath), scripts: scriptsFields(context, manifestNoPrefixes), web_accessible_resources: webResourcesFields(manifestNoPrefixes) } diff --git a/programs/develop/webpack/plugin-extension/data/manifest-fields/locales-fields/index.ts b/programs/develop/webpack/plugin-extension/data/manifest-fields/locales-fields/index.ts index c93b3188..03b3750a 100644 --- a/programs/develop/webpack/plugin-extension/data/manifest-fields/locales-fields/index.ts +++ b/programs/develop/webpack/plugin-extension/data/manifest-fields/locales-fields/index.ts @@ -1,7 +1,7 @@ import path from 'path' import fs from 'fs' -export function getLocaleFields( +export function localesFields( context: string, manifestPath: string ): string[] | undefined { diff --git a/programs/develop/webpack/plugin-extension/feature-scripts/steps/add-scripts.ts b/programs/develop/webpack/plugin-extension/feature-scripts/steps/add-scripts.ts index 1ad17d08..db0fb7f7 100644 --- a/programs/develop/webpack/plugin-extension/feature-scripts/steps/add-scripts.ts +++ b/programs/develop/webpack/plugin-extension/feature-scripts/steps/add-scripts.ts @@ -17,8 +17,9 @@ export class AddScripts { public apply(compiler: webpack.Compiler): void { const scriptFields = this.includeList || {} - for (const field of Object.entries(scriptFields)) { - const [feature, scriptPath] = field + const newEntries: Record = {} + + for (const [feature, scriptPath] of Object.entries(scriptFields)) { const scriptImports = getScriptEntries(scriptPath, this.excludeList) const cssImports = getCssEntries(scriptPath, this.excludeList) const entryImports = [...scriptImports] @@ -32,14 +33,15 @@ export class AddScripts { entryImports.push(...cssImports) } - // 1 - Add the script entries to the compilation. if (cssImports.length || scriptImports.length) { - compiler.options.entry = { - ...compiler.options.entry, - // https://webpack.js.org/configuration/entry-context/#entry-descriptor - [feature]: {import: entryImports} - } + newEntries[feature] = {import: entryImports} } } + + // Add all the new entries to the compilation at once + compiler.options.entry = { + ...compiler.options.entry, + ...newEntries + } } } diff --git a/programs/develop/webpack/plugin-reload/steps/create-web-socket-server/web-socket-server/message-dispatcher.ts b/programs/develop/webpack/plugin-reload/steps/create-web-socket-server/web-socket-server/message-dispatcher.ts index 5f94eca7..a5cc15e2 100644 --- a/programs/develop/webpack/plugin-reload/steps/create-web-socket-server/web-socket-server/message-dispatcher.ts +++ b/programs/develop/webpack/plugin-reload/steps/create-web-socket-server/web-socket-server/message-dispatcher.ts @@ -1,6 +1,6 @@ import path from 'path' import WebSocket from 'ws' -import manifestFields from 'browser-extension-manifest-fields' +import {getManifestFieldsData} from '../../../../plugin-extension/data/manifest-fields' function dispatchMessage( server: WebSocket.Server, @@ -22,9 +22,9 @@ export function messageDispatcher( ) { if (!updatedFile || !manifestPath) return - const manifestLocales = manifestFields(manifestPath).locales - const manifestScripts = manifestFields(manifestPath).scripts - const jsonScripts = manifestFields(manifestPath).json + const manifestLocales = getManifestFieldsData({manifestPath}).locales + const manifestScripts = getManifestFieldsData({manifestPath}).scripts + const jsonScripts = getManifestFieldsData({manifestPath}).json if (!server) { if (process.env.EXTENSION_ENV === 'development') {