Skip to content

Commit

Permalink
Lint RunChromePlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
cezaraugusto committed Feb 22, 2024
1 parent c951f18 commit 4a8e18b
Show file tree
Hide file tree
Showing 15 changed files with 37 additions and 1,631 deletions.
16 changes: 8 additions & 8 deletions packages/run-chrome-extension/helpers/messages.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'path'
import {Compiler} from 'webpack'
import {type Compiler} from 'webpack'
import {log, error} from 'console'
import {
underline,
Expand All @@ -8,12 +8,13 @@ import {
green,
blue,
red,
yellow,
black
} from '@colors/colors/safe'
// @ts-ignore
import prefersYarn from 'prefers-yarn'
import getDirectorySize from '../steps/calculateDirSize'
import {ManifestBase} from '../manifest-types'
import {type ManifestBase} from '../manifest-types'

interface Data {
id: string
Expand All @@ -22,19 +23,19 @@ interface Data {
}

function manifestFieldError(feature: string, htmlFilePath: string) {
const hintMessage = `Check the \`${feature}\` field in your manifest.json file and try again.`
const hintMessage = `Check the ${bold(feature)} field in your manifest.json file and try again.`

const errorMessage = `[manifest.json] File path \`${htmlFilePath}\` not found. ${hintMessage}`
return errorMessage
}

function manifestNotFound() {
log(`
# Error! Can't find the project's manifest file.
${bold("Error! Can't find the project's manifest file.")}
Check your extension \`manifest.json\` file and ensure its path points to
Check your extension ${yellow('manifest.json')} file and ensure its path points to
one of the options above, and try again.
`)
`)
}

function extensionData(
Expand All @@ -57,7 +58,7 @@ Ensure your extension is enabled and that no hanging Chrome instance is open the
}

const compilerOptions = compiler.options
const {id, manifest, management} = message.data
const {id, management} = message.data

if (!management) {
if (process.env.EXTENSION_ENV === 'development') {
Expand All @@ -83,7 +84,6 @@ Ensure your extension is enabled and that no hanging Chrome instance is open the
})
const fixedId = manifestFromCompiler.id === id
const hasHost = hostPermissions && hostPermissions.length
management.enabled

log('')
log(`${bold(`• Name:`)} ${name}`)
Expand Down
4 changes: 2 additions & 2 deletions packages/run-chrome-extension/helpers/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Compilation} from 'webpack'
import {Manifest} from '../types'
import {type Compilation} from 'webpack'
import {type Manifest} from '../types'

export function getManifestContent(
compilation: Compilation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {urlToRequest} from 'loader-utils'
import {validate} from 'schema-utils'
import {type LoaderContext} from 'webpack'
import {type Schema} from 'schema-utils/declarations/validate'
import {type Manifest} from '../types'

const schema: Schema = {
type: 'object',
Expand All @@ -27,7 +28,7 @@ export default function (this: InjectBackgroundAcceptContext, source: string) {
const options = this.getOptions()
const manifestPath = options.manifestPath
const projectPath = path.dirname(manifestPath)
const manifest = require(manifestPath)
const manifest: Manifest = require(manifestPath)

validate(schema, options, {
name: 'Inject Reload (background.scripts and background.service_worker) Script',
Expand Down Expand Up @@ -94,7 +95,7 @@ export default function (this: InjectBackgroundAcceptContext, source: string) {
if (manifest.background) {
if (manifest.background.scripts) {
for (const bgScript of [manifest.background.scripts[0]]) {
const absoluteUrl = path.resolve(projectPath, bgScript)
const absoluteUrl = path.resolve(projectPath, bgScript as string)

if (url.includes(absoluteUrl)) {
return `${generalReloadCode}${source}`
Expand All @@ -105,7 +106,7 @@ export default function (this: InjectBackgroundAcceptContext, source: string) {
if (manifest.background.service_worker) {
const absoluteUrl = path.resolve(
projectPath,
manifest.background.service_worker
manifest.background.service_worker as string
)
if (url.includes(absoluteUrl)) {
return `${generalReloadCode}${source}`
Expand Down
10 changes: 3 additions & 7 deletions packages/run-chrome-extension/manifest-types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
export interface ManifestIcons {
[size: number]: string
}
export type ManifestIcons = Record<number, string>;

export interface SearchProvider {
name?: string | undefined
Expand Down Expand Up @@ -145,8 +143,7 @@ export interface ManifestBase {
}
| undefined
commands?:
| {
[name: string]: {
| Record<string, {
suggested_key?:
| {
default?: string | undefined
Expand All @@ -158,8 +155,7 @@ export interface ManifestBase {
| undefined
description?: string | undefined
global?: boolean | undefined
}
}
}>
| undefined
content_capabilities?:
| {
Expand Down
4 changes: 2 additions & 2 deletions packages/run-chrome-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"node": ">=18"
},
"name": "webpack-run-chrome-extension",
"version": "0.6.0",
"version": "1.0.0",
"description": "Run and build your extension on Chrome with auto-reload support",
"main": "./dist/module.js",
"types": "./dist/module.d.ts",
Expand Down Expand Up @@ -39,7 +39,7 @@
"webextension"
],
"peerDependencies": {
"webpack": "^5.68.0"
"webpack": "^5.00.0"
},
"dependencies": {
"@types/loader-utils": "^2.0.6",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default class CreateWebSocketServer {
'RunChromeExtensionPlugin (CreateWebSocketServer)',
(compiler, done) => {
const files = compiler.modifiedFiles || new Set()
const changedFile = files.values().next().value
const changedFile: string | undefined = files.values().next().value

if (!changedFile) {
done()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import path from 'path'
import fs from 'fs'
import WebSocket from 'ws'
import manifestFields, {getPagesPath} from 'browser-extension-manifest-fields'
import manifestFields from 'browser-extension-manifest-fields'
import {type RunChromeExtensionInterface} from '../../../types'
// import parseScript from '../../../helpers/parseScript'

Expand All @@ -25,9 +24,9 @@ export default function messageDispatcher(
) {
if (!updatedFile || !options.manifestPath) return

const manifestLocales = manifestFields(options.manifestPath!).locales
const manifestScripts = manifestFields(options.manifestPath!).scripts
const jsonScripts = manifestFields(options.manifestPath!).json
const manifestLocales = manifestFields(options.manifestPath).locales
const manifestScripts = manifestFields(options.manifestPath).scripts
const jsonScripts = manifestFields(options.manifestPath).json

// Ensure the manifest itself is watched.
if (path.basename(updatedFile) === 'manifest.json') {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import WebSocket from 'ws'
import {type Compiler} from 'webpack'
import messages from '../../../helpers/messages'
import {StatsPreset} from '../../../types'
import {type StatsPreset} from '../../../types'
import isFirstRun from '../../RunChromePlugin/chrome/isFirstRun'

export default function (
Expand Down Expand Up @@ -38,10 +38,12 @@ export default function (

// We're only ready when the extension says so
ws.on('message', (msg) => {
// eslint-disable-next-line @typescript-eslint/no-base-to-string
const message = JSON.parse(msg.toString())

if (message.status === 'clientReady') {
if (statsConfig === true) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
messages.extensionData(compiler, message, isUserFirstRun)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path'
import {type RunChromeExtensionInterface} from '../../../types'
import createUserDataDir from '../../../steps/RunChromePlugin/chrome/createUserDataDir'
import createUserDataDir from './createUserDataDir'

const managerExtension = path.resolve(
__dirname,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default class ChromeExtensionLauncherPlugin {
? `"${chrome}" "${this.options.startingUrl}"`
: `"${chrome}"`

if (!fs.existsSync(path.resolve(chrome))) {
if (!fs.existsSync(path.resolve(chrome as string))) {
console.error(
`${bgWhite(black(`chrome-runtime`))} ${green(
`►►►`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ApplyManifestDevDefaultsPlugin {
// See https://github.com/awesome-webextension/webpack-target-webextension#source-map.
// For V3, see https://developer.chrome.com/docs/extensions/migrating/improve-security/#update-csp
...(manifest.manifest_version === 2 &&
patchV2CSP(manifest.content_security_policy)),
patchV2CSP(manifest.content_security_policy as string)),

// Set permission scripting as it's required for reload to work
// with content scripts in v3. See:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default function patchExternallyConnectable(manifest: any) {
return {
externally_connectable: {
...manifest.externally_connectable,
ids: [...new Set(manifest.externally_connectable.ids || []), '*']
ids: [...new Set(manifest.externally_connectable.ids as string[] || []), '*']
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import fs from 'fs'
import path from 'path'
import type webpack from 'webpack'
import WebExtension from 'webpack-target-webextension'
import {type RunChromeExtensionInterface} from '../../../types'
import {type RunChromeExtensionInterface,type Manifest} from '../../../types'
import messages from '../../../helpers/messages'
import {Manifest} from '../../../types'

class TargetWebExtensionPlugin {
private readonly manifestPath?: string
Expand All @@ -16,7 +15,7 @@ class TargetWebExtensionPlugin {
private handleMissingBackground(manifest: Manifest) {
if (manifest.manifest_version === 3) {
if (manifest.background.service_worker) {
const serviceWorker = manifest.background.service_worker
const serviceWorker = manifest.background.service_worker as string
const serviceWorkerPath = path.join(
path.dirname(this.manifestPath!),
serviceWorker
Expand All @@ -33,7 +32,7 @@ class TargetWebExtensionPlugin {
}

if (manifest.background.scripts) {
const backgroundScripts = manifest.background.scripts
const backgroundScripts = manifest.background.scripts as string[]
const backgroundPath = path.join(
path.dirname(this.manifestPath!),
backgroundScripts[0]
Expand Down Expand Up @@ -70,7 +69,7 @@ class TargetWebExtensionPlugin {
return
}

const manifest = require(this.manifestPath)
const manifest: Manifest = require(this.manifestPath)

if (manifest.background) {
this.handleMissingBackground(manifest)
Expand Down
4 changes: 2 additions & 2 deletions packages/run-chrome-extension/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ManifestBase} from './manifest-types'
import {type ManifestBase} from './manifest-types'

export interface RunChromeExtensionInterface extends PluginOptions {
manifestPath?: string
Expand All @@ -20,7 +20,7 @@ export type StatsPreset =
| 'errors-warnings' // Alternative: none - Only output errors and warnings happen
| 'minimal' // Alternative: none - Only output when errors or new compilation happen
| 'none' // Alternative: false -tput nothing
| 'normal' //Alternative: true - Standard output
| 'normal' // Alternative: true - Standard output
| 'verbose' // Alternative: none - Output everything
| 'detailed' // Alternative: none - Output everything except chunkModules and chunkRootModules
| 'summary' // Alternative: none - Output webpack version, warnings count and errors count
Expand Down
Loading

0 comments on commit 4a8e18b

Please sign in to comment.