Skip to content

Commit 0db286c

Browse files
committed
Updated docusaurus plugin
1 parent 845824a commit 0db286c

2 files changed

Lines changed: 50 additions & 16 deletions

File tree

website/docs/introduction/docusaurus-usage.md

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,49 @@ export default function Root({ children }) {
2525

2626
You can override the default CodeBlock by swizzling the component, [more about swizzling](https://docusaurus.io/docs/swizzling).
2727

28-
For a full example, check out an example of a Docusaurus site using `react-py` at https://github.com/James-Ansley/python-docusaurus-template, specifically `src/theme/CodeBlock` and `src/components/CodeEditor`.
28+
For a full example, check out an example of a Docusaurus site using `react-py` at https://github.com/elilambnz/python-docusaurus-template, specifically `src/theme/CodeBlock` and `src/components/CodeEditor`.
2929

3030
## Docusaurus config
3131

3232
We've encountered a Webpack issue when bundling a Docusaurus site with this package. The following plugin can be added to `docusaurus.config.js` to resolve this issue:
3333

3434
```js
3535
plugins: [
36-
async function disableUsedExports() {
36+
async function reactPyWebpackFix() {
3737
return {
38-
name: 'disable-used-exports',
39-
configureWebpack() {
38+
name: 'react-py-webpack-fix',
39+
configureWebpack(_config, isServer) {
40+
if (isServer) return {}
41+
4042
return {
41-
optimization: {
42-
usedExports: false
43-
}
43+
// Add webpack globals to all chunks to fix worker context issues
44+
// This resolves the "__webpack_require__ is not defined" error in web workers
45+
plugins: [
46+
new (require('webpack').BannerPlugin)({
47+
banner: `
48+
if (typeof __webpack_require__ === 'undefined') {
49+
var __webpack_require__ = {
50+
gca: function(e) { return e = {}[e] || e, __webpack_require__.p + __webpack_require__.u(e); },
51+
p: '',
52+
u: function(e) { return ''; }
53+
};
54+
}
55+
`,
56+
raw: true,
57+
test: /\.js$/
58+
})
59+
]
4460
}
4561
}
4662
}
4763
}
4864
]
4965
```
5066

51-
[Read more about this issue here](https://github.com/facebook/docusaurus/issues/8389).
67+
This configuration resolves the `__webpack_require__ is not defined` error that occurs when web workers are processed by webpack. The BannerPlugin injects the necessary webpack globals into all JavaScript chunks, ensuring that worker files have access to the `__webpack_require__` object they expect.
68+
69+
[Read more about the original issue here](https://github.com/facebook/docusaurus/issues/8389).
5270

5371
## Python Docusaurus Template
5472

55-
Get started with `react-py` and Docusaurus, you can use the [Python Docusaurus Template](https://github.com/James-Ansley/python-docusaurus-template). Click the green "Use this template" button on the repository page to clone, then follow the Docusaurus configuration steps for your specific site.
73+
Get started with `react-py` and Docusaurus, you can use the [Python Docusaurus Template](https://github.com/elilambnz/python-docusaurus-template). Click the green "Use this template" button on the repository page to clone, then follow the Docusaurus configuration steps for your specific site.

website/docusaurus.config.js

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const config = {
5555
[
5656
require.resolve('@easyops-cn/docusaurus-search-local'),
5757
/** @type {import("@easyops-cn/docusaurus-search-local").PluginOptions} */
58-
({ hashed: true })
58+
{ hashed: true }
5959
]
6060
],
6161

@@ -94,14 +94,30 @@ const config = {
9494
}),
9595

9696
plugins: [
97-
async function disableUsedExports() {
97+
async function reactPyWebpackFix() {
9898
return {
99-
name: 'disable-used-exports',
100-
configureWebpack() {
99+
name: 'react-py-webpack-fix',
100+
configureWebpack(_config, isServer) {
101+
if (isServer) return {}
102+
101103
return {
102-
optimization: {
103-
usedExports: false
104-
}
104+
// Add webpack globals to all chunks to fix worker context issues
105+
// This resolves the "__webpack_require__ is not defined" error in web workers
106+
plugins: [
107+
new (require('webpack').BannerPlugin)({
108+
banner: `
109+
if (typeof __webpack_require__ === 'undefined') {
110+
var __webpack_require__ = {
111+
gca: function(e) { return e = {}[e] || e, __webpack_require__.p + __webpack_require__.u(e); },
112+
p: '',
113+
u: function(e) { return ''; }
114+
};
115+
}
116+
`,
117+
raw: true,
118+
test: /\.js$/
119+
})
120+
]
105121
}
106122
}
107123
}

0 commit comments

Comments
 (0)