Skip to content

Commit

Permalink
Fix import issue for npm-in-browser branch (#216)
Browse files Browse the repository at this point in the history
* Load 'npm-in-browser' via ESM module

* Replace 'window as any' with 'globalThis' for simplicity
  • Loading branch information
rvanasa authored Jan 6, 2024
1 parent 74d49ae commit 679c627
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 194 deletions.
196 changes: 6 additions & 190 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
"@types/react-dom": "^17.0.0",
"@types/styled-components": "^5.1.10",
"lodash.debounce": "^4.0.8",
"memfs": "^4.6.0",
"motoko": "^3.6.10",
"npm-in-browser": "^0.1.3",
"prettier-plugin-motoko": "^0.8.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
Expand Down
4 changes: 4 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<title>Motoko Playground - DFINITY</title>
<script type="module">
window.memfs = import("https://esm.sh/[email protected]");
window.npmInBrowser = import("https://esm.sh/[email protected]");
</script>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
Expand Down
8 changes: 6 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { useCallback, useEffect, useReducer, useState } from "react";
import styled, { createGlobalStyle } from "styled-components";
import { runNpmCli } from "npm-in-browser";
import memfs from "memfs";

// @ts-ignore
// eslint-disable-next-line import/no-webpack-loader-syntax
Expand Down Expand Up @@ -233,6 +231,12 @@ export function App() {
homepage: "https://sdk.dfinity.org/docs/base-libraries/stdlib-intro.html",
};
(async () => {
while (!("npmInBrowser" in globalThis)) {
await new Promise((resolve) => setTimeout(resolve, 100));
}
const { runNpmCli } = await globalThis.npmInBrowser;
const memfs = await globalThis.memfs;

await worker.fetchPackage(baseInfo);
await workplaceDispatch({
type: "loadPackage",
Expand Down

0 comments on commit 679c627

Please sign in to comment.