Skip to content

Commit f34b356

Browse files
committed
Cleanup module resolution
1 parent 1369d84 commit f34b356

File tree

3 files changed

+126
-64
lines changed

3 files changed

+126
-64
lines changed

frame.html

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,25 @@
1313
(function() {
1414
function evalPureScript(sources) {
1515
var modules = {};
16+
function dirname(str) {
17+
var ix = str.lastIndexOf("/");
18+
return ix < 0 ? "" : str.slice(0, ix);
19+
}
20+
function resolvePath(a, b) {
21+
if (b[0] === "." && b[1] === "/") {
22+
return dirname(a) + b.slice(1);
23+
}
24+
if (b[0] === "." && b[1] === "." && b[2] === "/") {
25+
return dirname(dirname(a)) + b.slice(2);
26+
}
27+
return b;
28+
}
1629
return function load(name) {
1730
if (modules[name]) {
1831
return modules[name].exports;
1932
}
2033
function require(path) {
21-
if (path === "./foreign.js") {
22-
return load(name + "$Foreign");
23-
}
24-
if (path.indexOf("../") === 0) {
25-
return load(path.slice(3).replace(/\/index\.js$/, ""));
26-
}
27-
return load(path);
34+
return load(resolvePath(name, path));
2835
}
2936
var module = modules[name] = { exports: {} };
3037
new Function("module", "exports", "require", sources[name])(module, module.exports, require);

js/index.js

Lines changed: 95 additions & 47 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Try/Loader.purs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import Control.Monad.Except (ExceptT)
1212
import Control.Parallel (parTraverse)
1313
import Data.Array as Array
1414
import Data.Array.NonEmpty as NonEmpty
15-
import Data.Maybe (Maybe(..))
15+
import Data.Maybe (Maybe(..), fromMaybe)
1616
import Data.Newtype (unwrap)
1717
import Data.String (Pattern(..))
1818
import Data.String as String
@@ -45,7 +45,18 @@ type Dependency =
4545
}
4646

4747
requireRegex :: Regex
48-
requireRegex = unsafeRegex """^var\s+\S+\s*=\s*require\(["']([^"']*)["']\);""" noFlags
48+
requireRegex = unsafeRegex """^var\s+\S+\s*=\s*require\(["']([^"']*)["']\)""" noFlags
49+
50+
dirname :: String -> String
51+
dirname path = fromMaybe "" do
52+
ix <- String.lastIndexOf (Pattern "/") path
53+
pure $ String.take ix path
54+
55+
resolvePath :: String -> String -> Maybe String
56+
resolvePath a b
57+
| String.take 2 b == "./" = Just $ dirname a <> String.drop 1 b
58+
| String.take 3 b == "../" = Just $ dirname (dirname a) <> String.drop 2 b
59+
| otherwise = Nothing
4960

5061
parseDeps :: String -> JS -> Array Dependency
5162
parseDeps current = Array.mapMaybe go <<< String.split (Pattern "\n") <<< unwrap
@@ -54,14 +65,10 @@ parseDeps current = Array.mapMaybe go <<< String.split (Pattern "\n") <<< unwrap
5465
go line = do
5566
match <- Regex.match requireRegex line
5667
requirePath <- join $ NonEmpty.index match 1
57-
pure $ case String.split (Pattern "/") requirePath of
58-
[ ".", "foreign.js" ] ->
59-
{ name: current <> "$Foreign"
60-
, path: Just $ current <> "/foreign.js"
61-
}
62-
[ "..", name, "index.js" ] ->
63-
{ name
64-
, path: Just $ name <> "/index.js"
68+
pure $ case resolvePath current requirePath of
69+
Just path ->
70+
{ name: path
71+
, path: String.stripPrefix (Pattern "/") path
6572
}
6673
_ ->
6774
{ name: requirePath

0 commit comments

Comments
 (0)