Skip to content

Commit 69ee71a

Browse files
Refer to ESM modules, not CJS ones (#277)
* Refer to ESM modules, not CJS ones * Return to URL remapping approach * Drop setBase.mjs; update readme
1 parent 051d7ef commit 69ee71a

File tree

10 files changed

+28
-49
lines changed

10 files changed

+28
-49
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,6 @@ jobs:
7272
npm run build
7373
npm run test
7474
npm run build:production
75-
if [ "/output/ignored/" != "$(cat public/frame.html | grep '<base' | sed -E 's/ +<base href="([^"]+)">/\1/;')" ]; then
76-
echo "client/public/frame.html's 'base' element refers to the wrong path"
77-
echo "Run 'npm run base:production' in the 'client' dir, and push the update as a commit"
78-
echo "CI will fail until then"
79-
exit 1
80-
fi
8175
8276
- name: Build client assets
8377
if: github.event_name == 'release'

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ npm install
102102
# Use `build:production` and `base:production` if you would like
103103
# to test the client against the production Try PureScript server.
104104
npm run build:(dev|production)
105-
npm run base:(dev|production)
106105

107106
npm run serve # Try PureScript is now available on localhost:8080
108107
```

client/config/dev/Try.Config.purs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ module Try.Config where
22

33
import Prelude
44

5+
loaderUrl :: String
6+
loaderUrl = "/js/output"
7+
58
compileUrl :: String
69
compileUrl = "http://localhost:8081"
710

client/config/prod/Try.Config.purs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ module Try.Config where
22

33
import Prelude
44

5+
loaderUrl :: String
6+
loaderUrl = "https://compile.purescript.org/output"
7+
58
compileUrl :: String
69
compileUrl = "https://compile.purescript.org"
710

client/package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@
33
"private": true,
44
"scripts": {
55
"clean": "rimraf output",
6-
"base:dev": "node setBase.mjs \"dev\"",
7-
"base:production": "node setBase.mjs \"prod\"",
86
"test": "spago test --path config/dev/Try.Config.purs",
97
"build": "spago build --path config/dev/Try.Config.purs",
108
"build:dev": "spago bundle-app --path config/dev/Try.Config.purs --to public/js/index.js",
119
"build:production": "spago bundle-app --path config/prod/Try.Config.purs --purs-args '--censor-lib --strict' --to public/js/index.js",
1210
"serve": "http-server public/ -o / --cors=\"Access-Control-Allow-Origin: *\" -c-1",
13-
"serve:dev": "npm run build:dev && npm run base:dev && npm run serve",
14-
"serve:production": "npm run build:production && npm run base:production && npm run serve"
11+
"serve:dev": "npm run build:dev && npm run serve",
12+
"serve:production": "npm run build:production && npm run serve"
1513
},
1614
"devDependencies": {
1715
"http-server": "^14.1.0",

client/public/frame.html

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
66
<meta content="utf-8" http-equiv="encoding">
77
<meta name="viewport" content="width=device-width, initial-scale=1.0">
8-
<!-- Do not edit the base element's 'href' manually. Use the 'base:dev' or 'base:production' script -->
9-
<base href="/output/ignored/">
108
<script>
119
window.esmsInitOptions = {
1210
// -- Hooks --
@@ -45,7 +43,7 @@
4543
<!-- ES Module Shims: Import maps polyfill for modules browsers without import maps support (all except Chrome 89+) -->
4644
<script async src="https://ga.jspm.io/npm:[email protected]/dist/es-module-shims.js" integrity="sha384-Zt+0efULC2q2dftjz0uNzXeTpPVuSLLekXQv9HoRuigkAyLPaUFvPVpYYhu2Xc/t" crossorigin="anonymous"></script>
4745

48-
<script src="/js/frame.js"></script>
46+
<script src="js/frame.js"></script>
4947
</head>
5048
<body>
5149
<main id="main"></main>

client/public/js/frame.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
window.addEventListener("message", function(event) {
66
parent = event.source;
77
parent.postMessage("trypurescript", "*");
8-
const code = `
9-
${event.data.code}
10-
main();
11-
`;
8+
const code = event.data.code;
129
const scriptEl = document.createElement("script");
1310
scriptEl.type = "module";
1411
scriptEl.appendChild(document.createTextNode(code));

client/setBase.mjs

Lines changed: 0 additions & 28 deletions
This file was deleted.

client/src/Try/Container.purs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,21 @@ import Ace (Annotation)
66
import Control.Monad.Except (runExceptT)
77
import Data.Array (fold)
88
import Data.Array as Array
9-
import Data.Either (Either(..))
9+
import Data.Either (Either(..), either)
1010
import Data.Foldable (for_, oneOf)
1111
import Data.FoldableWithIndex (foldMapWithIndex)
1212
import Data.Maybe (Maybe(..), fromMaybe, isNothing)
1313
import Data.Symbol (SProxy(..))
14+
import Data.String as String
15+
import Data.String (Pattern(..))
16+
import Data.String.Regex as Regex
17+
import Data.String.Regex.Flags as RegexFlags
1418
import Effect (Effect)
1519
import Effect.Aff (Aff, makeAff)
1620
import Effect.Aff as Aff
1721
import Effect.Class.Console (error)
1822
import Effect.Uncurried (EffectFn3, runEffectFn3)
23+
import Partial.Unsafe (unsafeCrashWith)
1924
import Halogen as H
2025
import Halogen.HTML as HH
2126
import Halogen.HTML.Events as HE
@@ -187,7 +192,17 @@ component = H.mkComponent
187192
_ <- H.query _editor unit $ H.tell $ Editor.SetAnnotations anns
188193
pure unit
189194
let
190-
eventData = { code: js }
195+
importRegex :: Regex.Regex
196+
importRegex = either (\_ -> unsafeCrashWith "Invalid regex") identity
197+
$ Regex.regex """^import (.+) from "../([^"]+)";$""" RegexFlags.noFlags
198+
replacement = "import $1 from \"" <> Config.loaderUrl <> "/$2\";"
199+
codeWithRemappedImports = js
200+
# String.split (Pattern "\n")
201+
# map (Regex.replace importRegex replacement)
202+
# String.joinWith "\n"
203+
204+
-- Actually call the `main` function
205+
eventData = { code: codeWithRemappedImports <> "\n\n" <> "main();" }
191206
H.liftEffect teardownIFrame
192207
H.liftAff $ makeAff \f -> do
193208
runEffectFn3 setupIFrame eventData (f (Right unit)) (f (Left $ Aff.error "Could not load iframe"))

deploy/nginx.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,6 @@ server {
8686
# match to ensure that we only serve JS files.
8787
location ~ ^/output/(.+\.js)$ {
8888
add_header Access-Control-Allow-Origin *;
89-
alias /var/www/trypurescript/staging/.psci_modules/node_modules/$1;
89+
alias /var/www/trypurescript/staging/.psci_modules/$1;
9090
}
9191
}

0 commit comments

Comments
 (0)