# HG changeset patch # User cin # Date 2020-09-21 10:49:58 # Node ID a1a1ef050ecc9d2a780e308cbffc2ad915a5c13d # Parent 08ec90e5f244f542fd9ed1116eed0d867df4cdd9 fixes to css, nls to support requirejs optimizer diff --git a/src/main/ts/css.ts b/src/main/ts/css.ts --- a/src/main/ts/css.ts +++ b/src/main/ts/css.ts @@ -1,16 +1,17 @@ -import inject = require("./dom-inject"); -import { id as mid} from "module"; -import { TraceSource } from "@implab/core-amd/log/TraceSource"; -const log = TraceSource.get(mid); +import inject from "./dom-inject"; + +interface OnLoad { + (result?: any): void; + error(err: any): void; +} const plugin = { - load: async (id: string, require: Require, cb: (param: any) => void) => { - const url = require.toUrl(id); - try { - await inject.injectStylesheet(url); - cb({ url }); - } catch (e) { - log.error("CSS plugin failed to load {0} ({1}): {2}", id, url, e); + load: (id: string, require: Require, cb: OnLoad, config: { isBuild?: boolean }) => { + if (config.isBuild) { + cb(); + } else { + const url = require.toUrl(id); + inject.injectStylesheet(url).then(() => cb({ url }), e => cb.error(e)); } } }; diff --git a/src/main/ts/dom-inject.ts b/src/main/ts/dom-inject.ts --- a/src/main/ts/dom-inject.ts +++ b/src/main/ts/dom-inject.ts @@ -18,8 +18,8 @@ interface NodeLoadResult { } class DomInject { - injectionPoint = document.head; - injectBefore = document.head.firstChild; + injectionPoint?: HTMLElement; + injectBefore?: HTMLElement; _map: MapOf> = {}; @@ -48,7 +48,10 @@ class DomInject { mixin(node, attr); - this.injectionPoint.insertBefore(node, this.injectBefore); + const _injectionPoint = this.injectionPoint || document.head; + const _injectBefore = this.injectBefore || _injectionPoint.firstChild; + + _injectionPoint.insertBefore(node, _injectBefore); }); } @@ -94,4 +97,4 @@ class DomInject { }; const instance = new DomInject(); -export = instance; \ No newline at end of file +export default instance; diff --git a/src/main/ts/i18n.ts b/src/main/ts/i18n.ts --- a/src/main/ts/i18n.ts +++ b/src/main/ts/i18n.ts @@ -2,6 +2,11 @@ import { MapOf } from "@implab/core-amd/ import { NlsBundle } from "./NlsBundle"; import { isPromise } from "@implab/core-amd/safe"; +interface OnLoad { + (result?: any): void; + error(err: any): void; +} + export function bundle(nls: T, locales?: MapOf) { const nlsBundle = new NlsBundle(nls, locales); @@ -16,12 +21,16 @@ export function bundle fn.define = (pack: Partial) => pack; fn.default = nlsBundle.default; - fn.load = async (id: string, require: Require, cb: { (): void; error: (arg0: any) => void; }) => { - try { - await nlsBundle.getLocale(id); + fn.load = async (id: string, require: Require, cb: OnLoad, config: any) => { + if (config.isBuild) { cb(); - } catch (e) { - cb.error(e); + } else { + try { + await nlsBundle.getLocale(id); + cb(); + } catch (e) { + cb.error(e); + } } };