Skip to content

Commit

Permalink
Fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
cezaraugusto committed Aug 27, 2024
1 parent 294f2dc commit 5931a35
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 39 deletions.
6 changes: 5 additions & 1 deletion programs/develop/webpack/dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ export async function devServer(
const serverConfig: Configuration = {
host: '127.0.0.1',
allowedHosts: 'all',
static: path.join(projectPath, 'public'),
static: {
watch: {
ignored: /\bnode_modules\b/
}
},
compress: false,
devMiddleware: {
writeToDisk: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class AddDependencies {

apply(compiler: Compiler): void {
compiler.hooks.afterCompile.tap(
'ManifestPlugin (AddDependenciesPlugin)',
'manifest:add-dependency',
(compilation) => {
if (compilation.errors?.length) return

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export class WebResourcesPlugin {
chunkGraph.getChunkModulesIterable(chunk)
)

modules.forEach((module) => {
modules.forEach((_module) => {
// Rspack does not provide `getModuleChunks` API.
chunk.files.forEach((file) => {
if (!importedFiles.includes(file)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path'
import {type Compiler} from '@rspack/core'
import { DevOptions } from '../../../../module'
import {DevOptions} from '../../../../module'

export function SetupFirefoxReloadClient(
compiler: Compiler,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ class SetupReloadStrategy {
}).apply(compiler)

// 3 - Add the HMR reloader to the entry point.
// new TargetWebExtensionPlugin({
// manifestPath: this.manifestPath,
// browser: this.browser
// }).apply(compiler)
new TargetWebExtensionPlugin({
manifestPath: this.manifestPath,
browser: this.browser
}).apply(compiler)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'fs'
import path from 'path'
import {type Compiler} from '@rspack/core'
import {WebExtensionPlugin} from './rspack-target-webextension'
// import {WebExtensionPlugin} from './rspack-target-webextension'
import {type PluginInterface} from '../../../reload-types'
import {type Manifest} from '../../../../webpack-types'
import {type DevOptions} from '../../../../../commands/dev'
Expand Down Expand Up @@ -116,19 +116,19 @@ export class TargetWebExtensionPlugin {
}
}

private getEntryName(manifest: Manifest) {
if (manifest.background) {
if (manifest.manifest_version === 3) {
return {serviceWorkerEntry: 'background/service_worker'}
}
// private getEntryName(manifest: Manifest) {
// if (manifest.background) {
// if (manifest.manifest_version === 3) {
// return {serviceWorkerEntry: 'background/service_worker'}
// }

if (manifest.manifest_version === 2) {
return {pageEntry: 'background/script'}
}
}
// if (manifest.manifest_version === 2) {
// return {pageEntry: 'background/script'}
// }
// }

return {pageEntry: 'background'}
}
// return {pageEntry: 'background'}
// }

public apply(compiler: Compiler) {
if (!this.manifestPath || !fs.lstatSync(this.manifestPath).isFile()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class ChuckLoaderRuntimePlugin {
}

apply(compiler: Compiler) {
const {RuntimeGlobals, Template} = compiler.webpack
const {RuntimeGlobals /*, Template */} = compiler.webpack
const {options} = this

compiler.hooks.compilation.tap(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import {Compiler} from '@rspack/core'
import {Compiler, RspackOptionsNormalized} from '@rspack/core'

// @ts-check
export class HMRDevServerPlugin {
apply(compiler: Compiler) {
const options = compiler.options
const options = compiler.options as RspackOptionsNormalized
if (!options.devServer) options.devServer = {}
const devServer = options.devServer

setDefault(devServer, 'devMiddleware', {})
// Extensions cannot be loaded over network
// Extensions cannot be loaded over the network
setDefault(devServer.devMiddleware!, 'writeToDisk', true)

if (!devServer.hot) return
Expand All @@ -22,13 +21,13 @@ export class HMRDevServerPlugin {
}
})

const devServerClient = devServer.client! as Record<string, any>
// Overlay doesn't work well in content script.
const devServerClient = devServer.client as Record<string, any>
// Overlay doesn't work well in content scripts.
setDefault(devServerClient, 'overlay', false)
// Progress is annoying in console.
// Progress is annoying in the console.
setDefault(devServerClient, 'progress', false)
// In content script loaded in https:// pages, it will try to use wss:// because of protocol detection.
setDefault(devServer!, 'webSocketServer', 'ws')
setDefault(devServerClient, 'webSocketTransport', 'ws')

// HMR requires CORS requests in content scripts.
setDefault(devServer, 'allowedHosts', 'all')
Expand All @@ -44,10 +43,16 @@ export class HMRDevServerPlugin {
}
}

function setDefault(obj: Record<string, any>, key: string | number, val: any) {
if (isObject(obj) && obj[key] === undefined) obj[key] = val
function setDefault<T extends object, K extends keyof T>(
obj: T,
key: K,
val: T[K]
) {
if (isObject(obj) && obj[key] === undefined) {
(obj as any)[key] = val
}
}

function isObject(x: any): x is Record<string, any> {
function isObject(x: unknown): x is Record<string, unknown> {
return typeof x === 'object' && x !== null
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {rspack, Compilation, sources} from '@rspack/core'
import BrowserRuntime from '../BrowserRuntime'
import {rspack, sources} from '@rspack/core'

// import()
const DYNAMIC_IMPORT_LOADER = 'dynamicImportLoader'
Expand All @@ -13,7 +12,7 @@ export function LoadScriptRuntimeModule(
rspackLib: typeof rspack,
supportDynamicImport: boolean | undefined,
classicLoaderEnabled: boolean | undefined,
acceptWeak: boolean
_acceptWeak: boolean
) {
const {Template, RuntimeGlobals} = rspackLib

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {rspack, Compilation, sources} from '@rspack/core'
import {rspack, Compilation} from '@rspack/core'
import BrowserRuntime from '../BrowserRuntime'

export function PublicPathRuntimeModule(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class ServiceWorkerEntryPlugin {
apply(compiler: Compiler) {
compiler.hooks.entryOption.tap(
ServiceWorkerEntryPlugin.name,
(context, entries) => {
(_context, entries) => {
if (typeof entries === 'function') {
if (this.options.noWarningDynamicEntry) return

Expand Down Expand Up @@ -44,7 +44,7 @@ export class ServiceWorkerEntryPlugin {
(compilation: Compilation) => {
compilation.hooks.optimizeChunkModules.tap(
ServiceWorkerEntryPlugin.name,
(chunks, chunkGraph) => {
() => {
const entryPoint = compilation.entrypoints.get(this.entry)
if (!entryPoint) return

Expand Down Expand Up @@ -107,7 +107,7 @@ export class ServiceWorkerEntryPlugin {
}

// Helper functions
function chunkHasJs(chunk: Chunk, compilation: Compilation): boolean {
function chunkHasJs(chunk: Chunk): boolean {
for (const file of chunk.files) {
if (file.endsWith('.js')) {
return true
Expand Down

0 comments on commit 5931a35

Please sign in to comment.