Skip to content

Commit

Permalink
Fix background service_worker not reloading
Browse files Browse the repository at this point in the history
  • Loading branch information
cezaraugusto committed Sep 3, 2024
1 parent 3875f8d commit cfa5d3b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 38 deletions.
2 changes: 1 addition & 1 deletion programs/develop/webpack/lib/__spec__/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ describe('utils', () => {
updateHash: () => {},
buffer: () => {
return Buffer.from(JSON.stringify(manifestContent))
},
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -17,35 +18,22 @@ export interface ManifestFields {
web_accessible_resources: Record<string, any>
}

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)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'path'
import fs from 'fs'

export function getLocaleFields(
export function localesFields(
context: string,
manifestPath: string
): string[] | undefined {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, webpack.EntryObject> = {}

for (const [feature, scriptPath] of Object.entries(scriptFields)) {
const scriptImports = getScriptEntries(scriptPath, this.excludeList)
const cssImports = getCssEntries(scriptPath, this.excludeList)
const entryImports = [...scriptImports]
Expand All @@ -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
}
}
}
Original file line number Diff line number Diff line change
@@ -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<typeof WebSocket, any>,
Expand All @@ -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') {
Expand Down

0 comments on commit cfa5d3b

Please sign in to comment.