Created
November 8, 2018 16:40
-
-
Save zindel/54d44221813282bd2eee49c113be9fc7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| diff --git a/Arrow/Arrow.ml b/Arrow/Arrow.ml | |
| index d0860d4..21c72a5 100644 | |
| --- a/Arrow/Arrow.ml | |
| +++ b/Arrow/Arrow.ml | |
| @@ -1,3 +1,5 @@ | |
| +module StringSet = Set.Make(String) | |
| + | |
| module type ACTION = sig | |
| type 'a t | |
| @@ -97,7 +99,11 @@ end = struct | |
| | Many builds -> List.map (run' path) builds | |
| | Bind {f; build} -> run' path build |> f |> run' path | |
| | Define (id, build) -> | |
| - cached (path @ [id]) build | |
| + begin | |
| + match build with | |
| + | Define _ -> failwith "Cannot nest define" | |
| + | _ -> cached (path @ [id]) build | |
| + end | |
| | DefineF (cache, f) -> | |
| FBuild {cache; f;} | |
| | SelectF (f, id) -> begin | |
| @@ -131,7 +137,7 @@ module Fastpack = struct | |
| type path = string | |
| type jsdep = string | |
| type jssource = string | |
| - type jsmodule = {id: string; source: jssource} | |
| + type jsmodule = {id: string; source: jssource; path: path} | |
| type jsbundle = string | |
| module Cache : CACHE = struct | |
| @@ -162,7 +168,7 @@ module Fastpack = struct | |
| Resolve {context; jsdep} | |
| (* Compiels js sources and returns js module and a list of paths to deps *) | |
| - let compile : path -> jssource -> (jsmodule * jsdep list) t = fun path jssource -> | |
| + let compile : path -> jssource -> (jsmodule * string list) t = fun path jssource -> | |
| Compile (path, jssource) | |
| (* Packs a list of js modules into a js bundle *) | |
| @@ -194,7 +200,7 @@ module Fastpack = struct | |
| |> List.filter (Pervasives.(<>) "") | |
| in | |
| (* TODO: compile should get the filename as well*) | |
| - ({id = "id+" ^ path; source}, deps) | |
| + ({id = "id+" ^ path; source; path}, deps) | |
| end | |
| | Pack modules -> | |
| modules | |
| @@ -208,50 +214,79 @@ module Fastpack = struct | |
| (** Build primitives. *) | |
| (* Wrap read action as build family, allows to cache read results by id. *) | |
| - let readSource : (string, jssource) fbuild t = | |
| - definef (fun path -> execute (Action.read path)) | |
| + let readSource = fun path -> | |
| + define path (execute (Action.read path)) | |
| (* Build which collects all module dependencies in a list. *) | |
| - let rec buildGraph : (path, jsmodule * jsdep list) fbuild t -> path -> jsmodule list t = | |
| + let rec buildGraph = | |
| fun compileModule path -> | |
| - let%bind jsmodule, deps = selectf compileModule path in | |
| + let%bind jsmodule, deps = compileModule path in | |
| (** Build which caches resolution dependencies on a jsmodule we are | |
| * resolving jsdep from and jsdep itself. *) | |
| - let resolveDep : (jsdep, path) fbuild t = | |
| - define jsmodule.id ( | |
| - definef ~cache:(fun dep -> dep) (fun dep -> | |
| + let resolveDep = fun dep -> | |
| + define dep ( | |
| execute (Action.resolve ~context:jsmodule dep) | |
| ) | |
| - ) | |
| in | |
| + define jsmodule.id ( | |
| + let%bind jsmodules = | |
| + many ( | |
| + let f dep = | |
| + let%bind path = resolveDep dep in | |
| + buildGraph compileModule path | |
| + in | |
| + List.map f deps | |
| + ) | |
| + in | |
| + | |
| + return (jsmodule :: (List.flatten jsmodules)) | |
| + ) | |
| + | |
| + let gatherDeps depRequests = | |
| + depRequests | |
| + | |
| + let resolve : jsmodule -> path -> path t = fun jsmodule dep -> | |
| + define dep ( | |
| + execute (Action.resolve ~context:jsmodule dep) | |
| + ) | |
| + | |
| + let readCompileResolve: path -> (jsmodule * path list) t = fun path -> | |
| + define path ( | |
| + let%bind source = execute (Action.read path) in | |
| + let%bind jsmodule, depRequests = define "ast" (execute (Action.compile path source)) in | |
| + pair (return jsmodule) (many(List.map (resolve jsmodule) depRequests)) | |
| + ) | |
| + | |
| + let buildGraph2 = fun entry -> | |
| + let rec buildGraph' seen entry = | |
| + let%bind jsmodule, resolvedDeps = readCompileResolve entry in | |
| let%bind jsmodules = | |
| - many ( | |
| - let f dep = | |
| - let%bind path = selectf resolveDep dep in | |
| - buildGraph compileModule path | |
| - in | |
| - List.map f deps | |
| + many( | |
| + resolvedDeps | |
| + |> List.filter(fun dep -> not(StringSet.mem dep seen)) | |
| + |> List.map (buildGraph' (StringSet.add jsmodule.path seen)) | |
| ) | |
| in | |
| - | |
| return (jsmodule :: (List.flatten jsmodules)) | |
| + in | |
| + buildGraph' StringSet.empty entry | |
| + | |
| (* Fastpack entry point / build which packs a bundle given an entry point. *) | |
| let main : path -> jsbundle t = fun entry -> | |
| - (* Build which caches module compilation. *) | |
| - let compileModule : (path, jsmodule * jsdep list) fbuild t = | |
| - definef ~cache:(fun dep -> dep) (fun path -> | |
| - let%bind jssource = selectf readSource path in | |
| - execute (Action.compile path jssource) | |
| - ) | |
| - in | |
| - let%bind graph = buildGraph compileModule entry in | |
| - List.fold_left | |
| - (fun acc m -> define m.id acc) | |
| - (define "bundle/index.js" (execute (Action.pack graph))) | |
| - graph | |
| + define "bundle" ( | |
| + let%bind graph = define "graph" (buildGraph2 entry) in | |
| + define "output.js" (execute (Action.pack graph)) | |
| + ) | |
| + (* (1* Build which caches module compilation. *1) *) | |
| + (* let compileModule = fun path -> *) | |
| + (* let%bind jssource = readSource path in *) | |
| + (* execute (Action.compile path jssource) *) | |
| + (* in *) | |
| + (* let%bind graph = buildGraph compileModule entry in *) | |
| + (* execute (Action.pack graph) *) | |
| end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment