Skip to content

Commit

Permalink
Filter manifest keys by browser for all require instances
Browse files Browse the repository at this point in the history
  • Loading branch information
cezaraugusto committed Sep 19, 2024
1 parent fb8574a commit c13d78f
Show file tree
Hide file tree
Showing 23 changed files with 72 additions and 55 deletions.
Binary file removed examples/new-typescript/images/typescript.png
Binary file not shown.
4 changes: 2 additions & 2 deletions programs/develop/webpack/lib/__spec__/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ describe('utils', () => {
})
})

describe('removeManifestKeysNotFromCurrentBrowser', () => {
describe('filterKeysForThisBrowser', () => {
it('should remove keys not from the current browser', () => {
const manifest: Manifest = {
name: 'Test Extension',
Expand All @@ -198,7 +198,7 @@ describe('utils', () => {
'firefox:background': {scripts: ['background.js']}
}
const browser = 'chrome'
const result = utils.removeManifestKeysNotFromCurrentBrowser(
const result = utils.filterKeysForThisBrowser(
manifest,
browser
)
Expand Down
2 changes: 1 addition & 1 deletion programs/develop/webpack/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ export function getHardcodedMessage(manifest: Manifest): {
}
}

export function removeManifestKeysNotFromCurrentBrowser(
export function filterKeysForThisBrowser(
manifest: Manifest,
browser: DevOptions['browser']
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class BrowserFieldsPlugin {
}

patchManifest(manifest: Manifest) {
const patchedManifest = utils.removeManifestKeysNotFromCurrentBrowser(
const patchedManifest = utils.filterKeysForThisBrowser(
manifest,
this.browser
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ export function firefoxRunningServiceWorkerError(
): webpack.WebpackError | null {
if (browser === 'firefox' || browser === 'gecko-based') {
if (manifest.background?.service_worker) {
return new webpack.WebpackError(
messages.firefoxServiceWorkerError()
)
return new webpack.WebpackError(messages.firefoxServiceWorkerError())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import v3Schema from './lib/manifest.schema.v3.json'
import {addCustomFormats} from './lib/custom-validators'
import {requiredFieldErrors} from './required-field-errors'
import {invalidFieldTypeErrors} from './invalid-field-type-errors'

import * as utils from '../../lib/utils'
import handleDeprecatedError from './deprecated-errors'
import {type PluginInterface, type Manifest} from '../../webpack-types'
import {DevOptions} from '../../../module'
import {DevOptions} from '../../../commands/dev'

export class ManifestSchemaErrorsPlugin {
private readonly options: PluginInterface
Expand All @@ -30,8 +30,9 @@ export class ManifestSchemaErrorsPlugin {
}

const validate = ajv.compile(combinedSchema)
const valid = validate(manifest)
const isManifestV3 = manifest.manifest_version === 3
const patchedManifest = utils.filterKeysForThisBrowser(manifest, browser)
const valid = validate(patchedManifest)
const isManifestV3 = patchedManifest.manifest_version === 3

if (!valid) {
if (validate.errors) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function getManifestFieldsData({
}: PluginInterface) {
const context = path.dirname(manifestPath)
const manifest = require(manifestPath)
const manifestNoPrefixes = utils.removeManifestKeysNotFromCurrentBrowser(
const manifestNoPrefixes = utils.filterKeysForThisBrowser(
manifest,
browser || 'chrome'
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ export class ThrowIfRecompileIsNeeded {
this.hasEntriesChanged(updatedCssEntries, css) ||
this.hasEntriesChanged(updatedJsEntries, js)
) {
console.log(
messages.serverRestartRequiredFromHtml(changedFile)
)
console.log(messages.serverRestartRequiredFromHtml(changedFile))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,9 @@ export class JsonPlugin {
// and output the file accordingly.
if (!utils.shouldExclude(thisResource, this.excludeList)) {
if (!fs.existsSync(thisResource)) {

compilation.warnings.push(
new webpack.WebpackError(
messages.entryNotFoundWarn(
feature,
thisResource
)
messages.entryNotFoundWarn(feature, thisResource)
)
)
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,7 @@ export class LocalesPlugin {
if (!fs.existsSync(thisResource)) {
compilation.warnings.push(
new webpack.WebpackError(
messages.entryNotFoundWarn(
feature,
thisResource
)
messages.entryNotFoundWarn(feature, thisResource)
)
)
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ export class EmitManifest {
}
} catch (error: any) {
compilation.errors.push(
new webpack.WebpackError(
messages.manifestInvalidError(error)
)
new webpack.WebpackError(messages.manifestInvalidError(error))
)
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class ThrowIfRecompileIsNeeded {
}

const initialManifest: Manifest = require(this.manifestPath)
const manifest = utils.removeManifestKeysNotFromCurrentBrowser(
const manifest = utils.filterKeysForThisBrowser(
initialManifest,
this.browser
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from 'path'
import webpack from 'webpack'
import {type FilepathList, type PluginInterface} from '../../webpack-types'
import {DevOptions} from '../../../commands/dev'

/**
* ResolvePlugin is responsible for resolving paths for
Expand Down Expand Up @@ -43,12 +44,14 @@ export interface LoaderOptions {

export class ResolvePlugin {
public readonly manifestPath: string
public readonly browser: DevOptions['browser']
public readonly includeList?: FilepathList
public readonly excludeList?: FilepathList
public readonly loaderOptions?: LoaderOptions

constructor(options: PluginInterface & {loaderOptions?: LoaderOptions}) {
this.manifestPath = options.manifestPath
this.browser = options.browser || 'chrome'
this.includeList = options.includeList
this.excludeList = options.excludeList
this.loaderOptions = options.loaderOptions
Expand All @@ -69,6 +72,7 @@ export class ResolvePlugin {
exclude: /node_modules/,
options: {
manifestPath: this.manifestPath,
browser: this.browser,
includeList: this.includeList,
jsx: this.loaderOptions ? this.loaderOptions['jsx'] : false,
typescript: this.loaderOptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ export function emitResolverModule(
resolverAbsolutePath: string
) {
const options = loader.getOptions()
const {manifestPath, includeList} = options
const {manifestPath, includeList, browser} = options

const includeListModule = JSON.stringify(
getFileList(manifestPath, includeList)
getFileList(manifestPath, browser, includeList)
)

const resolverModule = fs.readFileSync(resolverAbsolutePath, 'utf8')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import path from 'path'
import {type FilepathList, type Manifest} from '../../../../webpack-types'
import {getManifestEntries} from './get-manifest-entries'
import * as utils from '../../../../lib/utils'
import {DevOptions} from '../../../../../commands/dev'

function parseIncludeList(
manifestPath: string,
Expand All @@ -19,15 +21,23 @@ function parseIncludeList(
return updatedIncludeList
}

function parseManifestList(manifestPath: string) {
function parseManifestList(
manifestPath: string,
browser: DevOptions['browser']
) {
const manifest: Manifest = require(manifestPath)
const manifestIncludeList = getManifestEntries(manifest)
const patchedManifest = utils.filterKeysForThisBrowser(manifest, browser)
const manifestIncludeList = getManifestEntries(patchedManifest)
return manifestIncludeList
}

export function getFileList(manifestPath: string, includeList?: FilepathList) {
export function getFileList(
manifestPath: string,
browser: DevOptions['browser'],
includeList?: FilepathList
) {
const include = parseIncludeList(manifestPath, includeList)
const manifestInclude = parseManifestList(manifestPath)
const manifestInclude = parseManifestList(manifestPath, browser)
const filesList = {...include, ...manifestInclude}

return filesList
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import {type LoaderContext, type FilepathList} from '../../../webpack-types'
import {type DevOptions} from '../../../../commands/dev'

export interface ResolvePluginContext extends LoaderContext {
resourcePath: string
getOptions: () => {
test: string
manifestPath: string
browser: DevOptions['browser']
includeList: FilepathList
excludeList: FilepathList
typescript: boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const schema: Schema = {
type: 'object',
properties: {
test: {type: 'string'},
browser: {type: 'string'},
manifestPath: {type: 'string'},
includeList: {type: 'object'},
excludeList: {type: 'object'},
Expand Down
3 changes: 2 additions & 1 deletion programs/develop/webpack/plugin-extension/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ export class ExtensionPlugin {
const manifestFieldsData = getManifestFieldsData({manifestPath})
const specialFoldersData = getSpecialFoldersData({manifestPath})

process.env.EXPERIMENTAL_EXTENSION_RESOLVER_PLUGIN &&
process.env.EXPERIMENTAL_EXTENSION_RESOLVER_PLUGIN === 'true' &&
new ResolvePlugin({
manifestPath,
browser: this.browser,
includeList: {
...(specialFoldersData?.pages || {}),
...(specialFoldersData?.scripts || {})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,7 @@ export async function maybeUseTypeScript(
} catch (e) {
const typescriptDependencies = ['typescript']

await installOptionalDependencies(
'TypeScript',
typescriptDependencies
)
await installOptionalDependencies('TypeScript', typescriptDependencies)

// The compiler will exit after installing the dependencies
// as it can't read the new dependencies without a restart.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import {urlToRequest} from 'loader-utils'
import {validate} from 'schema-utils'
import {type LoaderContext} from 'webpack'
import {type Schema} from 'schema-utils/declarations/validate'
import * as utils from '../../../lib/utils'
import {type Manifest} from '../../../webpack-types'
import {DevOptions} from '../../../../commands/dev'

const schema: Schema = {
type: 'object',
Expand All @@ -27,9 +29,10 @@ interface InjectBackgroundClientContext extends LoaderContext<any> {
export default function (this: InjectBackgroundClientContext, source: string) {
const options = this.getOptions()
const manifestPath = options.manifestPath
const browser = options.browser
const browser = options.browser as DevOptions['browser']
const projectPath = path.dirname(manifestPath)
const manifest: Manifest = require(manifestPath)
const patchedManifest = utils.filterKeysForThisBrowser(manifest, browser)

validate(schema, options, {
name: 'reload:inject-background-client',
Expand Down Expand Up @@ -90,14 +93,14 @@ export default function (this: InjectBackgroundClientContext, source: string) {
);
`

let manifestBg: Record<string, any> | undefined = manifest.background
let manifestBg: Record<string, any> | undefined = patchedManifest.background

// Handling for specific browsers
if (browser !== 'firefox') {
manifestBg =
manifest[`chromium:background`] ||
manifest[`chrome:background`] ||
manifest[`edge:background`] ||
patchedManifest[`chromium:background`] ||
patchedManifest[`chrome:background`] ||
patchedManifest[`edge:background`] ||
manifestBg

// Check for background scripts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import {urlToRequest} from 'loader-utils'
import {validate} from 'schema-utils'
import {type LoaderContext} from 'webpack'
import {type Schema} from 'schema-utils/declarations/validate'
import * as utils from '../../../lib/utils'
import {type Manifest} from '../../../webpack-types'
import {DevOptions} from '../../../../commands/dev'

const schema: Schema = {
type: 'object',
Expand All @@ -27,8 +29,10 @@ interface InjectBackgroundClientContext extends LoaderContext<any> {
export default function (this: InjectBackgroundClientContext, source: string) {
const options = this.getOptions()
const manifestPath = options.manifestPath
const browser = options.browser as DevOptions['browser']
const projectPath = path.dirname(manifestPath)
const manifest: Manifest = require(manifestPath)
const patchedManifest = utils.filterKeysForThisBrowser(manifest, browser)

validate(schema, options, {
name: 'reload:inject-background-client',
Expand Down Expand Up @@ -83,9 +87,9 @@ export default function (this: InjectBackgroundClientContext, source: string) {

// Handling for specific browsers
const manifestBg =
manifest['gecko:background'] ||
manifest['firefox:background'] ||
manifest.background
patchedManifest['gecko:background'] ||
patchedManifest['firefox:background'] ||
patchedManifest.background

// Check for background scripts
if (manifestBg) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {type PluginInterface} from '../../../reload-types'
import {type Manifest} from '../../../../webpack-types'
import {type DevOptions} from '../../../../../commands/dev'
import * as messages from '../../../../lib/messages'
import * as utils from '../../../../lib/utils'

export class TargetWebExtensionPlugin {
private readonly manifestPath: string
Expand Down Expand Up @@ -94,10 +95,7 @@ export class TargetWebExtensionPlugin {
if (this.manifestPath) {
const manifest: Manifest = require(this.manifestPath)
const manifestName = manifest.name || 'Extension.js'
const fieldError = messages.backgroundIsRequired(
manifestName,
filePath
)
const fieldError = messages.backgroundIsRequired(manifestName, filePath)
console.error(fieldError)
throw new Error(fieldError)
}
Expand Down Expand Up @@ -135,11 +133,15 @@ export class TargetWebExtensionPlugin {
}

const manifest: Manifest = require(this.manifestPath)
const patchedManifest = utils.filterKeysForThisBrowser(
manifest,
this.browser
)

this.handleBackground(compiler, this.browser, manifest)
this.handleBackground(compiler, this.browser, patchedManifest)

new WebExtension({
background: this.getEntryName(manifest),
background: this.getEntryName(patchedManifest),
weakRuntimeCheck: true
}).apply(compiler)
}
Expand Down
Loading

0 comments on commit c13d78f

Please sign in to comment.