forked from tahowallet/extension
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
inject window-provider at build time
into provider bridge The async fetch call to load the `window-provider.js` runtime caused some really bad race conditions, so we needed a way to have the content of the `window-provider.js` readily available when the provider-bridge starts running. This functionality had to work in both dev and prod environments so the solution was to create a webpack plugin for this. The trickiest part was finding the proper hooks in webpack and escaping the source code so it can be stored as a string.
- Loading branch information
Gergo Nagy
committed
Mar 22, 2022
1 parent
902be0a
commit 6e686bb
Showing
3 changed files
with
67 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { Compilation, Compiler } from "webpack" | ||
|
||
const PLUGIN_NAME = "RuntimeDefine" | ||
const WINDOW_PROVIDER_FILENAME = "window-provider.js" | ||
const PROVIDER_BRIDGE_FILENAME = "provider-bridge.js" | ||
|
||
export default class InjectWindowProvider { | ||
// eslint-disable-next-line class-methods-use-this | ||
apply(compiler: Compiler): void { | ||
compiler.hooks.thisCompilation.tap( | ||
PLUGIN_NAME, | ||
(compilation: Compilation) => { | ||
compilation.hooks.processAssets.tap( | ||
{ | ||
name: PLUGIN_NAME, | ||
stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_INLINE, | ||
}, | ||
(assets) => { | ||
let windowProviderSource = | ||
// @ts-expect-error this exist | ||
// eslint-disable-next-line no-underscore-dangle | ||
assets[WINDOW_PROVIDER_FILENAME]._children[0]._value | ||
|
||
// need to encode so it can be used as a string | ||
// in non optimised builds the source is a multi line string > `` needs to be used | ||
// but ${ needs to be escaped separatly otherwise it breaks the `` | ||
windowProviderSource = encodeURI(windowProviderSource).replaceAll( | ||
"${", | ||
"\\${" | ||
) | ||
windowProviderSource = `\`${windowProviderSource}\`` | ||
const providerBridgeSource = | ||
// @ts-expect-error this exist | ||
// eslint-disable-next-line no-underscore-dangle | ||
assets[PROVIDER_BRIDGE_FILENAME]._children[0]._value | ||
|
||
// @ts-expect-error this exist | ||
// eslint-disable-next-line no-underscore-dangle,no-param-reassign | ||
assets[PROVIDER_BRIDGE_FILENAME]._children[0]._value = | ||
providerBridgeSource.replace( | ||
// eslint-disable-next-line no-useless-escape | ||
`\"@@@WINDOW_PROVIDER@@@\"`, | ||
windowProviderSource | ||
) | ||
|
||
// eslint-disable-next-line no-param-reassign | ||
delete assets[WINDOW_PROVIDER_FILENAME] | ||
// eslint-disable-next-line no-param-reassign | ||
delete assets[`${WINDOW_PROVIDER_FILENAME}.map`] | ||
} | ||
) | ||
} | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters