Skip to content

Commit

Permalink
refactor: use import.meta.webpackHot instead of module.hot
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Works committed Jun 2, 2021
1 parent 8ee0c06 commit 3aaa7f2
Show file tree
Hide file tree
Showing 27 changed files with 48 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { Flags } from '../../../utils/flags'
import { getWalletCached, getWalletsCached } from './wallet'
import { resetAllNonce } from './nonce'

const effect = startEffects(module.hot)
const effect = startEffects(import.meta.webpackHot)

//#region tracking chain state
export const updateBlockNumber = debounce(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getWallets } from '../../../plugins/Wallet/services'
import { currentSelectedWalletAddressSettings } from '../../../plugins/Wallet/settings'
import { startEffects } from '../../../utils/side-effects'

const effect = startEffects(module.hot)
const effect = startEffects(import.meta.webpackHot)

//#region tracking wallets
let wallets: WalletRecord[] = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ const CancelableJobs: CancelableJob[] = [
PluginWorker,
]

if (module.hot) {
if (import.meta.webpackHot) {
const cleanup = CancelableJobs.map(startJob)
module.hot.dispose(() => cleanup.forEach((x) => x()))
module.hot.accept()
import.meta.webpackHot.dispose(() => cleanup.forEach((x) => x()))
import.meta.webpackHot.accept()
} else {
CancelableJobs.forEach(startJob)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/maskbook/src/extension/content-script/hmr.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// If the content script runs in https, webpack will connect https://localhost:HMR_PORT
if (module.hot) {
if (import.meta.webpackHot) {
globalThis.WebSocket = new Proxy(WebSocket, {
construct(target, args, newTarget) {
args[0] = removeWss(args[0])
Expand Down
6 changes: 3 additions & 3 deletions packages/maskbook/src/extension/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export const Services = {
export default Services
export const ServicesWithProgress = add(() => import('./service-generator'), 'ServicesWithProgress', true)

if (module.hot && isEnvironment(Environment.ManifestBackground)) {
module.hot.accept(
if (import.meta.webpackHot && isEnvironment(Environment.ManifestBackground)) {
import.meta.webpackHot.accept(
[
'./background-script/CryptoService',
'./background-script/IdentityService',
Expand Down Expand Up @@ -70,7 +70,7 @@ function add<T>(impl: () => Promise<T>, key: string, generator = false): T {
const RPC: (impl: any, opts: AsyncCallOptions) => T = (generator ? AsyncGeneratorCall : AsyncCall) as any
const load = () => getLocalImplementation(`Services.${key}`, impl, channel)
const localImplementation = load()
isBackground && module.hot && document.addEventListener(SERVICE_HMR_EVENT, load)
isBackground && import.meta.webpackHot && document.addEventListener(SERVICE_HMR_EVENT, load)
const service = RPC(localImplementation, {
key,
serializer,
Expand Down
2 changes: 1 addition & 1 deletion packages/maskbook/src/plugin-infra/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from '../plugins/Wallet/settings'
import { startEffects } from '../utils/side-effects'

const effect = startEffects(module.hot)
const effect = startEffects(import.meta.webpackHot)
export function createPluginHost(signal?: AbortSignal): Plugin.__Host.Host {
return {
eth: ethStatusReporter,
Expand Down
2 changes: 1 addition & 1 deletion packages/maskbook/src/plugins/Airdrop/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import { createPluginMessage } from '../utils/createPluginMessage'
import { createPluginRPC } from '../utils/createPluginRPC'
import { AirdropPluginID } from './constants'

if (module.hot) module.hot.accept()
if (import.meta.webpackHot) import.meta.webpackHot.accept()
export const AirdropMessage = createPluginMessage<{ _: unknown }>(AirdropPluginID)
export const PluginAirdropRPC = createPluginRPC(AirdropPluginID, () => import('./services'), AirdropMessage.events._)
2 changes: 1 addition & 1 deletion packages/maskbook/src/plugins/Collectible/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ interface CollectibleMessage {
rpc: unknown
}

if (module.hot) module.hot.accept()
if (import.meta.webpackHot) import.meta.webpackHot.accept()
export const PluginCollectibleMessage = createPluginMessage<CollectibleMessage>(PLUGIN_IDENTIFIER)
export const PluginCollectibleRPC = createPluginRPC(
PLUGIN_IDENTIFIER,
Expand Down
2 changes: 1 addition & 1 deletion packages/maskbook/src/plugins/FileService/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createPluginMessage } from '../../utils/createPluginMessage'
import { createPluginRPC, createPluginRPCGenerator } from '../../utils/createPluginRPC'
import { pluginId } from '../constants'

if (module.hot) module.hot.accept()
if (import.meta.webpackHot) import.meta.webpackHot.accept()
const PluginFileServiceMessage = createPluginMessage<{ _: unknown; _2: unknown }>(pluginId)
export const PluginFileServiceRPC = createPluginRPC(
pluginId,
Expand Down
2 changes: 1 addition & 1 deletion packages/maskbook/src/plugins/ITO/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ interface ITO_Message {
rpc: unknown
}

if (module.hot) module.hot.accept()
if (import.meta.webpackHot) import.meta.webpackHot.accept()
export const PluginITO_Messages: WebExtensionMessage<ITO_Message> = createPluginMessage<ITO_Message>(ITO_PluginID)
export const PluginITO_RPC = createPluginRPC(ITO_PluginID, () => import('./services'), PluginITO_Messages.events.rpc)
2 changes: 1 addition & 1 deletion packages/maskbook/src/plugins/PluginSerivce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* createPluginMessage and createPluginRPC supports HMR out of the box.
* So if you only use these two things, please add the following code to enable HMR.
*
* if (module.hot) module.hot.accept()
* if (import.meta.webpackHot) import.meta.webpackHot.accept()
*/

// Please make sure you have registered your plugin UI at ./PluginUI
Expand Down
2 changes: 1 addition & 1 deletion packages/maskbook/src/plugins/Polls/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { WorkerChannel } from 'async-call-rpc/utils/web/worker'

export const PollMetadataReader = createTypedMessageMetadataReader<PollMetaData>(POLL_META_KEY_1, schema)
export const renderWithPollMetadata = createRenderWithMetadata(PollMetadataReader)
if (module.hot) module.hot.accept()
if (import.meta.webpackHot) import.meta.webpackHot.accept()
const PollMessage = createPluginMessage<{ _: unknown }>(identifier)
export const PluginPollRPC = createPluginRPC(
identifier,
Expand Down
2 changes: 1 addition & 1 deletion packages/maskbook/src/plugins/RedPacket/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ export interface RedPacketMessages {
redPacketUpdated: void
rpc: unknown
}
if (module.hot) module.hot.accept()
if (import.meta.webpackHot) import.meta.webpackHot.accept()
export const RedPacketMessage = createPluginMessage<RedPacketMessages>(RedPacketPluginID)
export const RedPacketRPC = createPluginRPC(RedPacketPluginID, () => import('./services'), RedPacketMessage.events.rpc)
2 changes: 1 addition & 1 deletion packages/maskbook/src/plugins/Trader/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ interface PluginTraderMessage {
rpc: unknown
}

if (module.hot) module.hot.accept()
if (import.meta.webpackHot) import.meta.webpackHot.accept()
export const PluginTraderMessages = createPluginMessage<PluginTraderMessage>(PLUGIN_IDENTIFIER)
export const PluginTraderRPC = createPluginRPC(
PLUGIN_IDENTIFIER,
Expand Down
2 changes: 1 addition & 1 deletion packages/maskbook/src/plugins/Transak/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ interface PluginTransakMessage {
buyTokenDialogUpdated: BuyTokenDialogEvent
}

if (module.hot) module.hot.accept()
if (import.meta.webpackHot) import.meta.webpackHot.accept()
export const PluginTransakMessages = createPluginMessage<PluginTransakMessage>(PLUGIN_IDENTIFIER)
2 changes: 1 addition & 1 deletion packages/maskbook/src/plugins/Wallet/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,6 @@ interface WalletMessage {
rpc: unknown
}

if (module.hot) module.hot.accept()
if (import.meta.webpackHot) import.meta.webpackHot.accept()
export const WalletMessages = createPluginMessage<WalletMessage>(PLUGIN_IDENTIFIER)
export const WalletRPC = createPluginRPC(PLUGIN_IDENTIFIER, () => import('./services'), WalletMessages.events.rpc)
2 changes: 1 addition & 1 deletion packages/maskbook/src/polyfill/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// <reference path="./global.d.ts" />
import './ecmascript/index'
import './web-apis/index'
if (module.hot) module.hot.accept()
if (import.meta.webpackHot) import.meta.webpackHot.accept()
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ defineSocialNetworkUI({
...facebookBase,
load: () => import('./ui-provider'),
hotModuleReload(callback) {
if (module.hot) {
module.hot.accept('./ui-provider.ts', async () => {
if (import.meta.webpackHot) {
import.meta.webpackHot.accept('./ui-provider.ts', async () => {
callback((await import('./ui-provider')).default)
})
}
Expand All @@ -17,8 +17,8 @@ defineSocialNetworkWorker({
...facebookWorkerBase,
load: () => import('./worker-provider'),
hotModuleReload(callback) {
if (module.hot) {
module.hot.accept('./worker-provider.ts', async () => {
if (import.meta.webpackHot) {
import.meta.webpackHot.accept('./worker-provider.ts', async () => {
callback((await import('./worker-provider')).default)
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ defineSocialNetworkUI({
...instagramBase,
load: () => import('./ui-provider'),
hotModuleReload(callback) {
if (module.hot) {
module.hot.accept('./ui-provider.ts', async () => {
if (import.meta.webpackHot) {
import.meta.webpackHot.accept('./ui-provider.ts', async () => {
callback((await import('./ui-provider')).default)
})
}
Expand All @@ -17,8 +17,8 @@ defineSocialNetworkWorker({
...instagramWorkerBase,
load: () => import('./worker-provider'),
hotModuleReload(callback) {
if (module.hot) {
module.hot.accept('./worker-provider.ts', async () => {
if (import.meta.webpackHot) {
import.meta.webpackHot.accept('./worker-provider.ts', async () => {
callback((await import('./worker-provider')).default)
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ defineSocialNetworkUI({
...twitterBase,
load: () => import('./ui-provider'),
hotModuleReload(callback) {
if (module.hot) {
module.hot.accept('./ui-provider.ts', async () => {
if (import.meta.webpackHot) {
import.meta.webpackHot.accept('./ui-provider.ts', async () => {
callback((await import('./ui-provider')).default)
})
}
Expand All @@ -17,8 +17,8 @@ defineSocialNetworkWorker({
...twitterWorkerBase,
load: () => import('./worker-provider'),
hotModuleReload(callback) {
if (module.hot) {
module.hot.accept('./worker-provider.ts', async () => {
if (import.meta.webpackHot) {
import.meta.webpackHot.accept('./worker-provider.ts', async () => {
callback((await import('./worker-provider')).default)
})
}
Expand Down
4 changes: 2 additions & 2 deletions packages/maskbook/src/social-network/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export async function activateSocialNetworkUIInner(ui_deferred: SocialNetworkUI.

const abort = new AbortController()
const { signal } = abort
if (module.hot) {
if (import.meta.webpackHot) {
console.log('SNS adaptor HMR enabled.')
ui_deferred.hotModuleReload?.(async (newDefinition) => {
console.log('SNS adaptor updated. Uninstalling current adaptor.')
Expand Down Expand Up @@ -157,7 +157,7 @@ export async function loadSocialNetworkUI(identifier: string): Promise<SocialNet
if (!define) throw new Error('SNS adaptor not found')
const ui = (await define.load()).default
definedSocialNetworkUIsResolved.set(identifier, ui)
if (module.hot) {
if (import.meta.webpackHot) {
define.hotModuleReload?.((ui) => definedSocialNetworkUIsResolved.set(identifier, ui))
}
return ui
Expand Down
2 changes: 1 addition & 1 deletion packages/maskbook/src/utils/debug/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as shared from '@dimensiondev/maskbook-shared'
import * as kit from '@dimensiondev/holoflows-kit'

// Run this file multiple times should be safe
if (module.hot) module.hot.accept()
if (import.meta.webpackHot) import.meta.webpackHot.accept()

//#region BigNumber to Number
Object.defineProperty(BigNumber.prototype, '__debug__amount__', {
Expand Down
4 changes: 2 additions & 2 deletions packages/maskbook/src/utils/i18n-next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ addMaskThemeI18N(i18nNextInstance)
addDashboardI18N(i18nNextInstance)
i18nNextInstance.languages = ['en', 'zh', 'ko', 'ja']

if (module.hot) {
module.hot.accept(
if (import.meta.webpackHot) {
import.meta.webpackHot.accept(
[
'../_locales/en/messages.json',
'../_locales/zh/messages.json',
Expand Down
2 changes: 1 addition & 1 deletion packages/maskbook/src/utils/native-rpc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { WebviewAPI } from './Web'
import type { AndroidNativeAPIs, iOSNativeAPIs } from './types'

// This module won't be used in Web. Let it not effecting HMR.
if (process.env.architecture === 'web' && module.hot) module.hot.accept()
if (process.env.architecture === 'web' && import.meta.webpackHot) import.meta.webpackHot.accept()
export const hasNativeAPI = process.env.architecture === 'app'
export let nativeAPI:
| { type: 'iOS'; api: _AsyncVersionOf<iOSNativeAPIs> }
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-infra/src/manager/manage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export function createManager<T extends Plugin.Shared.DefinitionWithInit>(_: Cre
const definition = (await loader.load()).default
resolved.set(id, definition)

if (module.hot) {
if (import.meta.webpackHot) {
loader.hotModuleReload(async (_) => {
resolved.set(id, (await _).default)

Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-infra/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export namespace Plugin {
* ```ts
* const loader = {
* load: () => import('./code'),
* hotModuleReload: hot => module.hot && module.hot.accept('./code', () => hot(import('./code')))
* hotModuleReload: hot => import.meta.webpackHot && import.meta.webpackHot.accept('./code', () => hot(import('./code')))
* }
* ```
*
Expand All @@ -31,7 +31,7 @@ export namespace Plugin {
/**
* This provides the functionality for hot module reload on the plugin.
* When the callback is called, the old instance of the plugin will be unloaded, then the new instance will be init.
* @example hotModuleReload: hot => module.hot && module.hot.accept('./path', () => hot(import('./path')))
* @example hotModuleReload: hot => import.meta.webpackHot && import.meta.webpackHot.accept('./path', () => hot(import('./path')))
*/
hotModuleReload(onHot: (hot: Promise<{ default: DeferredModule }>) => void): void
}
Expand Down
9 changes: 6 additions & 3 deletions packages/plugins/example/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ registerPlugin({
SNSAdaptor: {
load: () => import('./SNSAdaptor'),
hotModuleReload: (hot) =>
module.hot && module.hot.accept('./SNSAdaptor/index', () => hot(import('./SNSAdaptor'))),
import.meta.webpackHot &&
import.meta.webpackHot.accept('./SNSAdaptor/index', () => hot(import('./SNSAdaptor'))),
},
Dashboard: {
load: () => import('./Dashboard'),
hotModuleReload: (hot) =>
module.hot && module.hot.accept('./Dashboard/index', () => hot(import('./Dashboard'))),
import.meta.webpackHot &&
import.meta.webpackHot.accept('./Dashboard/index', () => hot(import('./Dashboard'))),
},
Worker: {
load: () => import('./Worker'),
hotModuleReload: (hot) => module.hot && module.hot.accept('./Worker/index', () => hot(import('./Worker'))),
hotModuleReload: (hot) =>
import.meta.webpackHot && import.meta.webpackHot.accept('./Worker/index', () => hot(import('./Worker'))),
},
})

0 comments on commit 3aaa7f2

Please sign in to comment.