@@ -1,16 +1,17 | |||||
1 |
import inject |
|
1 | import inject from "./dom-inject"; | |
2 | import { id as mid} from "module"; |
|
2 | ||
3 | import { TraceSource } from "@implab/core-amd/log/TraceSource"; |
|
3 | interface OnLoad { | |
4 | const log = TraceSource.get(mid); |
|
4 | (result?: any): void; | |
|
5 | error(err: any): void; | |||
|
6 | } | |||
5 |
|
7 | |||
6 | const plugin = { |
|
8 | const plugin = { | |
7 |
load: |
|
9 | load: (id: string, require: Require, cb: OnLoad, config: { isBuild?: boolean }) => { | |
8 | const url = require.toUrl(id); |
|
10 | if (config.isBuild) { | |
9 |
|
|
11 | cb(); | |
10 | await inject.injectStylesheet(url); |
|
12 | } else { | |
11 |
c |
|
13 | const url = require.toUrl(id); | |
12 | } catch (e) { |
|
14 | inject.injectStylesheet(url).then(() => cb({ url }), e => cb.error(e)); | |
13 | log.error("CSS plugin failed to load {0} ({1}): {2}", id, url, e); |
|
|||
14 | } |
|
15 | } | |
15 | } |
|
16 | } | |
16 | }; |
|
17 | }; |
@@ -18,8 +18,8 interface NodeLoadResult { | |||||
18 | } |
|
18 | } | |
19 |
|
19 | |||
20 | class DomInject { |
|
20 | class DomInject { | |
21 |
injectionPoint |
|
21 | injectionPoint?: HTMLElement; | |
22 | injectBefore = document.head.firstChild; |
|
22 | injectBefore?: HTMLElement; | |
23 |
|
23 | |||
24 | _map: MapOf<Promise<NodeLoadResult>> = {}; |
|
24 | _map: MapOf<Promise<NodeLoadResult>> = {}; | |
25 |
|
25 | |||
@@ -48,7 +48,10 class DomInject { | |||||
48 |
|
48 | |||
49 | mixin(node, attr); |
|
49 | mixin(node, attr); | |
50 |
|
50 | |||
51 | this.injectionPoint.insertBefore(node, this.injectBefore); |
|
51 | const _injectionPoint = this.injectionPoint || document.head; | |
|
52 | const _injectBefore = this.injectBefore || _injectionPoint.firstChild; | |||
|
53 | ||||
|
54 | _injectionPoint.insertBefore(node, _injectBefore); | |||
52 | }); |
|
55 | }); | |
53 | } |
|
56 | } | |
54 |
|
57 | |||
@@ -94,4 +97,4 class DomInject { | |||||
94 | }; |
|
97 | }; | |
95 |
|
98 | |||
96 | const instance = new DomInject(); |
|
99 | const instance = new DomInject(); | |
97 |
export |
|
100 | export default instance; |
@@ -2,6 +2,11 import { MapOf } from "@implab/core-amd/ | |||||
2 | import { NlsBundle } from "./NlsBundle"; |
|
2 | import { NlsBundle } from "./NlsBundle"; | |
3 | import { isPromise } from "@implab/core-amd/safe"; |
|
3 | import { isPromise } from "@implab/core-amd/safe"; | |
4 |
|
4 | |||
|
5 | interface OnLoad { | |||
|
6 | (result?: any): void; | |||
|
7 | error(err: any): void; | |||
|
8 | } | |||
|
9 | ||||
5 | export function bundle<T extends object>(nls: T, locales?: MapOf<any>) { |
|
10 | export function bundle<T extends object>(nls: T, locales?: MapOf<any>) { | |
6 | const nlsBundle = new NlsBundle(nls, locales); |
|
11 | const nlsBundle = new NlsBundle(nls, locales); | |
7 |
|
12 | |||
@@ -16,12 +21,16 export function bundle<T extends object> | |||||
16 |
|
21 | |||
17 | fn.define = (pack: Partial<T>) => pack; |
|
22 | fn.define = (pack: Partial<T>) => pack; | |
18 | fn.default = nlsBundle.default; |
|
23 | fn.default = nlsBundle.default; | |
19 |
fn.load = async (id: string, require: Require, cb: |
|
24 | fn.load = async (id: string, require: Require, cb: OnLoad, config: any) => { | |
20 | try { |
|
25 | if (config.isBuild) { | |
21 | await nlsBundle.getLocale(id); |
|
|||
22 | cb(); |
|
26 | cb(); | |
23 |
} |
|
27 | } else { | |
24 |
|
|
28 | try { | |
|
29 | await nlsBundle.getLocale(id); | |||
|
30 | cb(); | |||
|
31 | } catch (e) { | |||
|
32 | cb.error(e); | |||
|
33 | } | |||
25 | } |
|
34 | } | |
26 | }; |
|
35 | }; | |
27 |
|
36 |
General Comments 0
You need to be logged in to leave comments.
Login now