Skip to content

Refer to ESM modules, not CJS ones #277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,6 @@ jobs:
npm run build
npm run test
npm run build:production
if [ "/output/ignored/" != "$(cat public/frame.html | grep '<base' | sed -E 's/ +<base href="([^"]+)">/\1/;')" ]; then
echo "client/public/frame.html's 'base' element refers to the wrong path"
echo "Run 'npm run base:production' in the 'client' dir, and push the update as a commit"
echo "CI will fail until then"
exit 1
fi

- name: Build client assets
if: github.event_name == 'release'
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ npm install
# Use `build:production` and `base:production` if you would like
# to test the client against the production Try PureScript server.
npm run build:(dev|production)
npm run base:(dev|production)

npm run serve # Try PureScript is now available on localhost:8080
```
Expand Down
3 changes: 3 additions & 0 deletions client/config/dev/Try.Config.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ module Try.Config where

import Prelude

loaderUrl :: String
loaderUrl = "/js/output"

compileUrl :: String
compileUrl = "http://localhost:8081"

Expand Down
3 changes: 3 additions & 0 deletions client/config/prod/Try.Config.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ module Try.Config where

import Prelude

loaderUrl :: String
loaderUrl = "https://compile.purescript.org/output"

compileUrl :: String
compileUrl = "https://compile.purescript.org"

Expand Down
6 changes: 2 additions & 4 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
"private": true,
"scripts": {
"clean": "rimraf output",
"base:dev": "node setBase.mjs \"dev\"",
"base:production": "node setBase.mjs \"prod\"",
"test": "spago test --path config/dev/Try.Config.purs",
"build": "spago build --path config/dev/Try.Config.purs",
"build:dev": "spago bundle-app --path config/dev/Try.Config.purs --to public/js/index.js",
"build:production": "spago bundle-app --path config/prod/Try.Config.purs --purs-args '--censor-lib --strict' --to public/js/index.js",
"serve": "http-server public/ -o / --cors=\"Access-Control-Allow-Origin: *\" -c-1",
"serve:dev": "npm run build:dev && npm run base:dev && npm run serve",
"serve:production": "npm run build:production && npm run base:production && npm run serve"
"serve:dev": "npm run build:dev && npm run serve",
"serve:production": "npm run build:production && npm run serve"
},
"devDependencies": {
"http-server": "^14.1.0",
Expand Down
4 changes: 1 addition & 3 deletions client/public/frame.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Do not edit the base element's 'href' manually. Use the 'base:dev' or 'base:production' script -->
<base href="/output/ignored/">
<script>
window.esmsInitOptions = {
// -- Hooks --
Expand Down Expand Up @@ -45,7 +43,7 @@
<!-- ES Module Shims: Import maps polyfill for modules browsers without import maps support (all except Chrome 89+) -->
<script async src="https://ga.jspm.io/npm:[email protected]/dist/es-module-shims.js" integrity="sha384-Zt+0efULC2q2dftjz0uNzXeTpPVuSLLekXQv9HoRuigkAyLPaUFvPVpYYhu2Xc/t" crossorigin="anonymous"></script>

<script src="/js/frame.js"></script>
<script src="js/frame.js"></script>
</head>
<body>
<main id="main"></main>
Expand Down
5 changes: 1 addition & 4 deletions client/public/js/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
window.addEventListener("message", function(event) {
parent = event.source;
parent.postMessage("trypurescript", "*");
const code = `
${event.data.code}
main();
`;
const code = event.data.code;
const scriptEl = document.createElement("script");
scriptEl.type = "module";
scriptEl.appendChild(document.createTextNode(code));
Expand Down
28 changes: 0 additions & 28 deletions client/setBase.mjs

This file was deleted.

19 changes: 17 additions & 2 deletions client/src/Try/Container.purs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@ import Ace (Annotation)
import Control.Monad.Except (runExceptT)
import Data.Array (fold)
import Data.Array as Array
import Data.Either (Either(..))
import Data.Either (Either(..), either)
import Data.Foldable (for_, oneOf)
import Data.FoldableWithIndex (foldMapWithIndex)
import Data.Maybe (Maybe(..), fromMaybe, isNothing)
import Data.Symbol (SProxy(..))
import Data.String as String
import Data.String (Pattern(..))
import Data.String.Regex as Regex
import Data.String.Regex.Flags as RegexFlags
import Effect (Effect)
import Effect.Aff (Aff, makeAff)
import Effect.Aff as Aff
import Effect.Class.Console (error)
import Effect.Uncurried (EffectFn3, runEffectFn3)
import Partial.Unsafe (unsafeCrashWith)
import Halogen as H
import Halogen.HTML as HH
import Halogen.HTML.Events as HE
Expand Down Expand Up @@ -187,7 +192,17 @@ component = H.mkComponent
_ <- H.query _editor unit $ H.tell $ Editor.SetAnnotations anns
pure unit
let
eventData = { code: js }
importRegex :: Regex.Regex
importRegex = either (\_ -> unsafeCrashWith "Invalid regex") identity
$ Regex.regex """^import (.+) from "../([^"]+)";$""" RegexFlags.noFlags
replacement = "import $1 from \"" <> Config.loaderUrl <> "/$2\";"
codeWithRemappedImports = js
# String.split (Pattern "\n")
# map (Regex.replace importRegex replacement)
# String.joinWith "\n"

-- Actually call the `main` function
eventData = { code: codeWithRemappedImports <> "\n\n" <> "main();" }
H.liftEffect teardownIFrame
H.liftAff $ makeAff \f -> do
runEffectFn3 setupIFrame eventData (f (Right unit)) (f (Left $ Aff.error "Could not load iframe"))
Expand Down
2 changes: 1 addition & 1 deletion deploy/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,6 @@ server {
# match to ensure that we only serve JS files.
location ~ ^/output/(.+\.js)$ {
add_header Access-Control-Allow-Origin *;
alias /var/www/trypurescript/staging/.psci_modules/node_modules/$1;
alias /var/www/trypurescript/staging/.psci_modules/$1;
}
}