Skip to content

Commit afefd41

Browse files
committed
Document more pitfalls
1 parent 6728fa9 commit afefd41

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/content/docs/ext-dev/pitfalls.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,35 @@ Because of this, you need to make sure the React variable is in scope. `react` i
7979
const React = require("react");
8080
const myElement = <span>Hi!</span>;
8181
```
82+
83+
## Using the wrong moonlight global
84+
85+
What moonlight global exists depends on the entrypoint:
86+
87+
- Web: `moonlight`
88+
- Node: `moonlightNode`
89+
- Host: `moonlightHost`
90+
91+
## Each Webpack module is an entrypoint
92+
93+
Say you were writing a Webpack module that shared some state:
94+
95+
```ts title="myWebpackModule.ts"
96+
export const someState = 1;
97+
```
98+
99+
and you wanted to use it in another Webpack module. Do not directly import the Webpack module as a file:
100+
101+
```ts title="otherWebpackModule.ts"
102+
import { someState } from "./myWebpackModule";
103+
```
104+
105+
as it will make a copy of the module, duplicating your state and causing issues. Each Webpack module is treated as its own entrypoint, and all imports will be duplicated between them. Instead, import it through the `@moonlight-mod/wp` namespace or use `require`:
106+
107+
```ts title="otherWebpackModule.ts"
108+
import { someState } from "@moonlight-mod/wp/extension_myWebpackModule";
109+
// or
110+
const { someState } = require("extension_myWebpackModule");
111+
```
112+
113+
See [the page on ESM Webpack modules](/ext-dev/esm-webpack-modules) for how to type the import statement.

0 commit comments

Comments
 (0)