Skip to content

Commit 364c2ff

Browse files
committed
CLJS-2269: Warn on top level code split loads
cljs.loader/load is now a macro that passes in the module that initiated the load. This is validated to check that it is already loaded to avoid common misunderstanding about how loader is intended to be used.
1 parent a1c6c2e commit 364c2ff

File tree

2 files changed

+58
-14
lines changed

2 files changed

+58
-14
lines changed

src/main/cljs/cljs/loader.clj

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
; Copyright (c) Rich Hickey. All rights reserved.
2+
; The use and distribution terms for this software are covered by the
3+
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
4+
; which can be found in the file epl-v10.html at the root of this distribution.
5+
; By using this software in any fashion, you are agreeing to be bound by
6+
; the terms of this license.
7+
; You must not remove this notice, or any other, from this software
8+
9+
(ns cljs.loader
10+
(:refer-clojure :exclude [load])
11+
(:require [cljs.module-graph :as mg]
12+
[cljs.analyzer :as ana]
13+
[cljs.analyzer.api :as ana-api]))
14+
15+
(defn load-expr
16+
([env module-name]
17+
(load-expr env module-name nil))
18+
([env module-name cb]
19+
(let [sinfo (ana/srce-info env)
20+
loader (mg/module-for (-> env :ns :name)
21+
(:modules (ana-api/get-options)))]
22+
`(cljs.loader/load* ~module-name ~loader ~cb))))
23+
24+
(defmacro load
25+
"Load a module. module-name should be a keyword matching a :modules module
26+
definition."
27+
([module-name]
28+
(load-expr &env module-name))
29+
([module-name cb]
30+
(load-expr &env module-name cb)))

src/main/cljs/cljs/loader.cljs

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1+
; Copyright (c) Rich Hickey. All rights reserved.
2+
; The use and distribution terms for this software are covered by the
3+
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
4+
; which can be found in the file epl-v10.html at the root of this distribution.
5+
; By using this software in any fashion, you are agreeing to be bound by
6+
; the terms of this license.
7+
; You must not remove this notice, or any other, from this software
8+
19
(ns cljs.loader
10+
(:require-macros cljs.loader)
211
(:require [goog.object :as gobj])
312
(:import [goog.module ModuleLoader]
413
[goog.module ModuleManager]))
@@ -33,14 +42,30 @@
3342
(.setAllModuleInfo *module-manager* (to-js module-infos))
3443
(.setModuleUris *module-manager* (to-js module-uris))
3544

36-
(defn load
45+
(defn loaded?
46+
"Return true if modules is loaded. module-name should be a keyword matching
47+
a :modules module definition."
48+
[module-name]
49+
(assert (contains? module-infos module-name)
50+
(str "Module " module-name " does not exist"))
51+
(let [mname (-> module-name name munge)
52+
module (.getModuleInfo *module-manager* mname)]
53+
(when (some? module)
54+
(.isLoaded module))))
55+
56+
(defn load*
3757
"Load a module. module-name should be a keyword matching a :modules module
3858
definition."
3959
([module-name]
40-
(load module-name nil))
41-
([module-name cb]
60+
(throw (js/Error. "Invalid load call, must provide loader argument")))
61+
([module-name loader]
62+
(load* module-name loader nil))
63+
([module-name loader cb]
4264
(assert (contains? module-infos module-name)
4365
(str "Module " module-name " does not exist"))
66+
(assert (loaded? loader)
67+
(str "Module " loader " not fully loaded, but attempted to "
68+
"load module " module-name))
4469
(let [mname (-> module-name name munge)]
4570
(if-not (nil? cb)
4671
(.execOnLoad *module-manager* mname cb)
@@ -60,17 +85,6 @@
6085
(.setLoaded *module-manager* (munge-kw x)))
6186
(.setLoaded *module-manager* (munge-kw module-name))))
6287

63-
(defn loaded?
64-
"Return true if modules is loaded. module-name should be a keyword matching
65-
a :modules module definition."
66-
[module-name]
67-
(assert (contains? module-infos module-name)
68-
(str "Module " module-name " does not exist"))
69-
(let [mname (-> module-name name munge)
70-
module (.getModuleInfo *module-manager* mname)]
71-
(when (some? module)
72-
(.isLoaded module))))
73-
7488
(defn prefetch
7589
"Prefetch a module. module-name should be a keyword matching a :modules
7690
module definition. Will download the module but not evaluate it. To

0 commit comments

Comments
 (0)