@@ -1,16 +1,17 | |||
|
1 |
import inject |
|
|
2 | import { id as mid} from "module"; | |
|
3 | import { TraceSource } from "@implab/core-amd/log/TraceSource"; | |
|
4 | const log = TraceSource.get(mid); | |
|
1 | import inject from "./dom-inject"; | |
|
2 | ||
|
3 | interface OnLoad { | |
|
4 | (result?: any): void; | |
|
5 | error(err: any): void; | |
|
6 | } | |
|
5 | 7 | |
|
6 | 8 | const plugin = { |
|
7 |
load: |
|
|
8 | const url = require.toUrl(id); | |
|
9 |
|
|
|
10 | await inject.injectStylesheet(url); | |
|
11 |
c |
|
|
12 | } catch (e) { | |
|
13 | log.error("CSS plugin failed to load {0} ({1}): {2}", id, url, e); | |
|
9 | load: (id: string, require: Require, cb: OnLoad, config: { isBuild?: boolean }) => { | |
|
10 | if (config.isBuild) { | |
|
11 | cb(); | |
|
12 | } else { | |
|
13 | const url = require.toUrl(id); | |
|
14 | inject.injectStylesheet(url).then(() => cb({ url }), e => cb.error(e)); | |
|
14 | 15 | } |
|
15 | 16 | } |
|
16 | 17 | }; |
@@ -18,8 +18,8 interface NodeLoadResult { | |||
|
18 | 18 | } |
|
19 | 19 | |
|
20 | 20 | class DomInject { |
|
21 |
injectionPoint |
|
|
22 | injectBefore = document.head.firstChild; | |
|
21 | injectionPoint?: HTMLElement; | |
|
22 | injectBefore?: HTMLElement; | |
|
23 | 23 | |
|
24 | 24 | _map: MapOf<Promise<NodeLoadResult>> = {}; |
|
25 | 25 | |
@@ -48,7 +48,10 class DomInject { | |||
|
48 | 48 | |
|
49 | 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 | 99 | const instance = new DomInject(); |
|
97 |
export |
|
|
100 | export default instance; |
@@ -2,6 +2,11 import { MapOf } from "@implab/core-amd/ | |||
|
2 | 2 | import { NlsBundle } from "./NlsBundle"; |
|
3 | 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 | 10 | export function bundle<T extends object>(nls: T, locales?: MapOf<any>) { |
|
6 | 11 | const nlsBundle = new NlsBundle(nls, locales); |
|
7 | 12 | |
@@ -16,12 +21,16 export function bundle<T extends object> | |||
|
16 | 21 | |
|
17 | 22 | fn.define = (pack: Partial<T>) => pack; |
|
18 | 23 | fn.default = nlsBundle.default; |
|
19 |
fn.load = async (id: string, require: Require, cb: |
|
|
20 | try { | |
|
21 | await nlsBundle.getLocale(id); | |
|
24 | fn.load = async (id: string, require: Require, cb: OnLoad, config: any) => { | |
|
25 | if (config.isBuild) { | |
|
22 | 26 | cb(); |
|
23 |
} |
|
|
24 |
|
|
|
27 | } else { | |
|
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