devfab 2.0.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/devfab.js CHANGED
@@ -326,8 +326,6 @@
326
326
  }
327
327
  }
328
328
 
329
- var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
330
-
331
329
  // ======================================================
332
330
  /** vim: et:ts=4:sw=4:sts=4
333
331
  * @license RequireJS 2.3.2 Copyright jQuery Foundation and other contributors.
@@ -337,8 +335,7 @@
337
335
  // problems with requirejs.exec()/transpiler plugins that may not be strict.
338
336
  /* jslint regexp: true, nomen: true, sloppy: true */
339
337
  /* global window, navigator, document, importScripts, setTimeout, opera */
340
- var __devfab_requirejs = {};
341
- window.__devfab_requirejs = __devfab_requirejs;
338
+ var __requirejs = {};
342
339
  (function (global, setTimeout) {
343
340
  var requirejs, require, define;
344
341
  var req;
@@ -2343,22 +2340,22 @@
2343
2340
  // eslint-disable-next-line no-eval
2344
2341
  return eval(text);
2345
2342
  };
2346
- __devfab_requirejs.require = require;
2347
- __devfab_requirejs.define = define;
2343
+ __requirejs.require = require;
2344
+ __requirejs.define = define;
2348
2345
  // Set up with config info.
2349
2346
  req(cfg);
2350
- })(commonjsGlobal, typeof setTimeout === 'undefined' ? undefined : setTimeout)
2347
+ })(typeof window !== 'undefined' ? window : globalThis, typeof setTimeout === 'undefined' ? undefined : setTimeout)
2351
2348
 
2352
2349
  /* ----------------------------------------------------------------------------
2353
2350
  * 以下为本地适配层,不属于 RequireJS 原始文件内容。
2354
2351
  * -------------------------------------------------------------------------- */;
2355
2352
  (function (global) {
2356
- if (__devfab_requirejs.__requirejsAdapterApplied) {
2353
+ if (__requirejs.__requirejsAdapterApplied) {
2357
2354
  return;
2358
2355
  }
2359
- __devfab_requirejs.__requirejsAdapterApplied = true;
2360
- var rawRequire = __devfab_requirejs.require;
2361
- var rawDefine = __devfab_requirejs.define;
2356
+ __requirejs.__requirejsAdapterApplied = true;
2357
+ var rawRequire = __requirejs.require;
2358
+ var rawDefine = __requirejs.define;
2362
2359
  var require = function require(_domain, _deps, _callback, _errback) {
2363
2360
  var domain = _domain;
2364
2361
  var deps = _deps;
@@ -2420,10 +2417,15 @@
2420
2417
  require[prop] = rawRequire[prop];
2421
2418
  }
2422
2419
  }
2423
- __devfab_requirejs._require = rawRequire;
2424
- __devfab_requirejs._define = rawDefine;
2425
- __devfab_requirejs.require = require;
2426
- __devfab_requirejs.define = define;
2420
+
2421
+ // 标记本身是为了让人能**验证**适配层究竟有没有生效(测试与现场排查都用它),
2422
+ // 不然「域前缀到底拼没拼」只能靠猜。
2423
+ require.__devfabAdapterApplied = true;
2424
+ define.__devfabAdapterApplied = true;
2425
+ __requirejs._require = rawRequire;
2426
+ __requirejs._define = rawDefine;
2427
+ __requirejs.require = require;
2428
+ __requirejs.define = define;
2427
2429
  })();
2428
2430
 
2429
2431
  /**
@@ -2732,127 +2734,6 @@
2732
2734
  }]);
2733
2735
  }();
2734
2736
 
2735
- /**
2736
- * 全局污染检测:加载前后 diff window 的 key 集合。
2737
- *
2738
- * 为什么需要:二开脚本直接跑在宿主 window 上,A 客户的脚本改了一个全局变量,
2739
- * B 客户就跟着崩,而且现场无法归因。真做 iframe / Proxy 沙箱成本极高,
2740
- * 但「加载完能告诉你动了哪些全局」只需要几十行,能挡住大半这类事故。
2741
- *
2742
- * 边界(这是观测,不是隔离):
2743
- * - 事后告警,不阻止副作用——脚本已经执行过了;
2744
- * - 并发加载时窗口会重叠,归因以「最后完成的一批」结算,可能不准;
2745
- * - 只比较可枚举自有属性的**引用**,`window.foo.bar = 1` 这类深层改动检测不到。
2746
- */
2747
-
2748
- /** 默认盯防的 key:出现即说明宿主的关键设施被动过 */
2749
- var DEFAULT_WATCH = ['$', 'jQuery', 'define', 'require', 'requirejs', 'devfab', '__devfab', '__devfab_requirejs'];
2750
- var GlobalGuard = /*#__PURE__*/function () {
2751
- function GlobalGuard(options) {
2752
- _classCallCheck(this, GlobalGuard);
2753
- var o = options || {};
2754
- this.maxPollution = o.maxPollution === undefined ? 3 : Number(o.maxPollution);
2755
- this.watch = [].concat(o.watch || DEFAULT_WATCH);
2756
- this.target = o.target || (typeof window !== 'undefined' ? window : null);
2757
- this.enabled = o.enabled === undefined ? true : !!o.enabled;
2758
- this._pending = 0;
2759
- this._before = null;
2760
- this.lastWarnings = [];
2761
- }
2762
- return _createClass(GlobalGuard, [{
2763
- key: "_snapshot",
2764
- value: function _snapshot() {
2765
- var map = new Map();
2766
- var target = this.target;
2767
- if (!target) return map;
2768
- Object.keys(target).forEach(function (key) {
2769
- map.set(key, target[key]);
2770
- });
2771
- return map;
2772
- }
2773
- }, {
2774
- key: "_diff",
2775
- value: function _diff() {
2776
- var _this = this;
2777
- var before = this._before;
2778
- var after = this._snapshot();
2779
- var warnings = [];
2780
- if (!before) return warnings;
2781
- after.forEach(function (value, key) {
2782
- var watched = _this.watch.indexOf(key) !== -1;
2783
- if (!before.has(key)) {
2784
- warnings.push({
2785
- type: 'added',
2786
- key: key,
2787
- watched: watched
2788
- });
2789
- return;
2790
- }
2791
- if (before.get(key) !== value) {
2792
- warnings.push({
2793
- type: 'overwritten',
2794
- key: key,
2795
- watched: watched
2796
- });
2797
- }
2798
- });
2799
- // 命中盯防名单,或新增数量超过阈值,才认为「值得提醒」。
2800
- // 1~2 个自有全局是正常写法,全报会把信号淹掉。
2801
- var watchedCount = warnings.filter(function (item) {
2802
- return item.watched;
2803
- }).length;
2804
- var addedCount = warnings.filter(function (item) {
2805
- return item.type === 'added';
2806
- }).length;
2807
- if (!watchedCount && addedCount <= this.maxPollution) return [];
2808
- return warnings;
2809
- }
2810
-
2811
- /**
2812
- * 开始观测。返回的 `end()` 幂等,重复调用只有第一次有效,
2813
- * 这样超时分支和 errback 分支可以都调,不会把计数搞负。
2814
- */
2815
- }, {
2816
- key: "begin",
2817
- value: function begin() {
2818
- var _this2 = this;
2819
- if (!this.enabled || !this.target) {
2820
- return {
2821
- end: function end() {
2822
- return [];
2823
- }
2824
- };
2825
- }
2826
- this._pending++;
2827
- if (this._pending === 1) this._before = this._snapshot();
2828
- var done = false;
2829
- return {
2830
- end: function end() {
2831
- if (done) return [];
2832
- done = true;
2833
- _this2._pending--;
2834
- // 还有并发批次没结束,交给最后结束的那一批结算
2835
- if (_this2._pending > 0) return [];
2836
- var warnings = _this2._diff();
2837
- _this2._before = null;
2838
- _this2.lastWarnings = warnings;
2839
- return warnings;
2840
- }
2841
- };
2842
- }
2843
- }, {
2844
- key: "configure",
2845
- value: function configure(options) {
2846
- var o = options || {};
2847
- if (o.enabled !== undefined) this.enabled = !!o.enabled;
2848
- if (o.maxPollution !== undefined) this.maxPollution = Number(o.maxPollution);
2849
- if (o.watch !== undefined) this.watch = [].concat(o.watch || []);
2850
- if (o.target !== undefined) this.target = o.target || null;
2851
- return this;
2852
- }
2853
- }]);
2854
- }();
2855
-
2856
2737
  /** 加载超时:RequireJS 自己的 waitSeconds 默认 30s,这个定时器是更早的兜底 */
2857
2738
  var CODE_TIMEOUT = 504;
2858
2739
 
@@ -2875,10 +2756,8 @@
2875
2756
  var path = _path;
2876
2757
  var files = _files;
2877
2758
  var callback = _callback;
2878
- // 单参形式:(files, callback) 或 (files)。判据是「第一个是数组、第二个不是数组」,
2879
- // 放宽到非函数是因为 loadModules([url]) 这种不带回调的调用很常见。
2880
2759
  if (Array.isArray(_path) && !Array.isArray(_files)) {
2881
- path = params === null || params === void 0 ? void 0 : params.path;
2760
+ path = undefined;
2882
2761
  files = _path;
2883
2762
  callback = typeof _files === 'function' ? _files : _callback;
2884
2763
  }
@@ -2887,8 +2766,7 @@
2887
2766
  var result = {
2888
2767
  code: 0,
2889
2768
  message: '',
2890
- data: [],
2891
- warnings: []
2769
+ data: []
2892
2770
  };
2893
2771
  if (params.type === 'require' && typeof params.require !== 'function' || params.type === 'define' && typeof params.define !== 'function' //
2894
2772
  ) {
@@ -2928,99 +2806,84 @@
2928
2806
  version: '',
2929
2807
  timeout: 15000,
2930
2808
  retry: 0,
2931
- strict: false,
2932
2809
  baseUrl: ''
2933
2810
  };
2934
2811
  var BaseFileRequire = /*#__PURE__*/function () {
2935
- /**
2936
- * args 同时承担两件事,分区明确:
2937
- * - 底层加载器覆盖:`define` / `require` / `undef` / `requireConfig` / `importer`
2938
- * - 加载策略:`version` / `timeout` / `retry` / `strict` / `baseUrl` / `manifest`
2939
- * / `origins` / `lockVersion` / `verifyHash` / `pollution` / `watch` / `maxPollution`
2940
- * 其余一律走 `configure(options)` 在运行时改。
2941
- */
2942
2812
  function BaseFileRequire(args) {
2943
- var _o$__debug, _window$__devfab_requ, _window$__devfab_requ2, _this$__require;
2944
2813
  _classCallCheck(this, BaseFileRequire);
2945
- var o = args || {};
2946
- this.__debug = (_o$__debug = o.__debug) !== null && _o$__debug !== void 0 ? _o$__debug : false;
2947
- this.__rootPath = undefined;
2948
- this.__define = o.define || ((_window$__devfab_requ = window.__devfab_requirejs) === null || _window$__devfab_requ === void 0 ? void 0 : _window$__devfab_requ.define);
2949
- this.__require = o.require || ((_window$__devfab_requ2 = window.__devfab_requirejs) === null || _window$__devfab_requ2 === void 0 ? void 0 : _window$__devfab_requ2.require);
2950
- this.__undef = o.undef || (typeof ((_this$__require = this.__require) === null || _this$__require === void 0 ? void 0 : _this$__require.undef) === 'function' ? this.__require.undef : null);
2951
- this.__requireConfig = o.requireConfig || this._pickRequireConfig(this.__require);
2952
- this.__import = o.importer || null;
2953
- this._setTimer = o.setTimeout || function (fn, ms) {
2954
- return setTimeout(fn, ms);
2955
- };
2956
- this._clearTimer = o.clearTimeout || function (id) {
2957
- return clearTimeout(id);
2958
- };
2959
- this.options = {
2960
- version: o.version ? String(o.version) : DEFAULT_OPTIONS.version,
2961
- timeout: o.timeout === undefined ? DEFAULT_OPTIONS.timeout : Math.max(0, toNumber(o.timeout, 0)),
2962
- retry: o.retry === undefined ? DEFAULT_OPTIONS.retry : Math.max(0, toNumber(o.retry, 0)),
2963
- strict: !!o.strict,
2964
- baseUrl: o.baseUrl || DEFAULT_OPTIONS.baseUrl
2965
- };
2966
- this.policy = new LoadPolicy(o);
2967
- this.guard = new GlobalGuard(this._guardOptions(o));
2968
- if (this.options.version) this._applyUrlArgs();
2814
+ this.__init(args);
2969
2815
  }
2970
-
2971
- /**
2972
- * urlArgs 只能配在真正在用的那个 require 上。
2973
- * 传了自定义加载器(stub / 多 context)就取它的 config,否则取内嵌 RequireJS 的。
2974
- */
2975
2816
  return _createClass(BaseFileRequire, [{
2976
- key: "_pickRequireConfig",
2977
- value: function _pickRequireConfig(loader) {
2978
- var _window$__devfab_requ3;
2979
- var req = loader && typeof loader.config === 'function' ? loader : (_window$__devfab_requ3 = window.__devfab_requirejs) === null || _window$__devfab_requ3 === void 0 ? void 0 : _window$__devfab_requ3.require;
2980
- return req && typeof req.config === 'function' ? req.config.bind(req) : null;
2817
+ key: "__init",
2818
+ value: function __init(args) {
2819
+ var o = args || {};
2820
+ if (o.debug !== undefined) this.debug = !!o.debug;
2821
+ var require = __requirejs.require;
2822
+ this.__data = {
2823
+ /* --- 加载器句柄与通道:AMD 通道只走内嵌 RequireJS --- */
2824
+ define: __requirejs.define,
2825
+ require: require,
2826
+ undef: typeof (require === null || require === void 0 ? void 0 : require.undef) === 'function' ? require.undef : null,
2827
+ requireConfig: this._pickRequireConfig(require),
2828
+ importImpl: o.importer || null,
2829
+ setTimer: o.setTimeout || function (fn, ms) {
2830
+ return setTimeout(fn, ms);
2831
+ },
2832
+ clearTimer: o.clearTimeout || function (id) {
2833
+ return clearTimeout(id);
2834
+ },
2835
+ /* --- 配置与策略 --- */
2836
+ options: {
2837
+ version: o.version ? String(o.version) : DEFAULT_OPTIONS.version,
2838
+ timeout: o.timeout === undefined ? DEFAULT_OPTIONS.timeout : Math.max(0, toNumber(o.timeout, 0)),
2839
+ retry: o.retry === undefined ? DEFAULT_OPTIONS.retry : Math.max(0, toNumber(o.retry, 0)),
2840
+ baseUrl: o.baseUrl || DEFAULT_OPTIONS.baseUrl
2841
+ },
2842
+ policy: new LoadPolicy(o)
2843
+ };
2844
+ if (this.__data.options.version) this._applyUrlArgs();
2981
2845
  }
2982
2846
  }, {
2983
- key: "_guardOptions",
2984
- value: function _guardOptions(o) {
2985
- if (o.pollution === false) {
2986
- return {
2987
- enabled: false,
2988
- target: o.target
2989
- };
2990
- }
2991
- if (o.pollution && _typeof(o.pollution) === 'object') {
2992
- return Object.assign({
2993
- target: o.target
2994
- }, o.pollution);
2995
- }
2996
- return {
2997
- target: o.target,
2998
- watch: o.watch,
2999
- maxPollution: o.maxPollution,
3000
- enabled: o.pollution === undefined ? true : !!o.pollution
3001
- };
2847
+ key: "debug",
2848
+ get: function get() {
2849
+ var ns = typeof window !== 'undefined' ? window.__devfab : null;
2850
+ return !!(ns && ns.debug);
2851
+ },
2852
+ set: function set(value) {
2853
+ if (typeof window === 'undefined') return;
2854
+ window.__devfab = window.__devfab || {};
2855
+ window.__devfab.debug = !!value;
2856
+ }
2857
+
2858
+ /**
2859
+ * urlArgs 只能配在真正在用的那个 require 上。AMD 通道固定用内嵌 RequireJS,
2860
+ * 所以这里只从传入的加载器上取 config,不再回落到任何全局。
2861
+ */
2862
+ }, {
2863
+ key: "_pickRequireConfig",
2864
+ value: function _pickRequireConfig(loader) {
2865
+ return loader && typeof loader.config === 'function' ? loader.config.bind(loader) : null;
3002
2866
  }
3003
2867
 
3004
2868
  /* ------------------------------ 配置 ------------------------------ */
3005
2869
 
3006
2870
  /** 运行时增量配置(宿主切版本、换清单、改重试次数都走这里)
3007
- * 加载策略:版本、超时、重试、清单、白名单、全局污染检测
2871
+ * 加载策略:版本、超时、重试、清单、白名单
3008
2872
  */
3009
2873
  }, {
3010
2874
  key: "configure",
3011
2875
  value: function configure(options) {
3012
2876
  var o = options || {};
3013
- if (o.debug !== undefined) this.__debug = !!o.debug;
2877
+ var _this$__data = this.__data,
2878
+ config = _this$__data.options,
2879
+ policy = _this$__data.policy;
2880
+ if (o.debug !== undefined) this.debug = !!o.debug;
3014
2881
  if (o.version !== undefined) this.setVersion(o.version);
3015
- if (o.timeout !== undefined) this.options.timeout = Math.max(0, toNumber(o.timeout, this.options.timeout));
3016
- if (o.retry !== undefined) this.options.retry = Math.max(0, toNumber(o.retry, this.options.retry));
3017
- if (o.strict !== undefined) this.options.strict = !!o.strict;
3018
- if (o.baseUrl !== undefined) this.options.baseUrl = o.baseUrl || '';
2882
+ if (o.timeout !== undefined) config.timeout = Math.max(0, toNumber(o.timeout, config.timeout));
2883
+ if (o.retry !== undefined) config.retry = Math.max(0, toNumber(o.retry, config.retry));
2884
+ if (o.baseUrl !== undefined) config.baseUrl = o.baseUrl || '';
3019
2885
  if (o.manifest !== undefined || o.origins !== undefined || o.lockVersion !== undefined || o.verifyHash !== undefined || o.fetch !== undefined) {
3020
- this.policy.configure(o);
3021
- }
3022
- if (o.pollution !== undefined) this.guard.configure(this._guardOptions(o));else if (o.watch !== undefined || o.maxPollution !== undefined || o.target !== undefined) {
3023
- this.guard.configure(o);
2886
+ policy.configure(o);
3024
2887
  }
3025
2888
  return this;
3026
2889
  }
@@ -3035,19 +2898,22 @@
3035
2898
  }, {
3036
2899
  key: "setVersion",
3037
2900
  value: function setVersion(version) {
3038
- this.options.version = version ? String(version) : '';
2901
+ this.__data.options.version = version ? String(version) : '';
3039
2902
  this._applyUrlArgs();
3040
2903
  return this;
3041
2904
  }
3042
2905
  }, {
3043
2906
  key: "_applyUrlArgs",
3044
2907
  value: function _applyUrlArgs() {
3045
- if (typeof this.__requireConfig !== 'function') {
2908
+ var _this$__data2 = this.__data,
2909
+ requireConfig = _this$__data2.requireConfig,
2910
+ options = _this$__data2.options;
2911
+ if (typeof requireConfig !== 'function') {
3046
2912
  return false;
3047
2913
  }
3048
2914
  try {
3049
- this.__requireConfig({
3050
- urlArgs: this.options.version ? "v=".concat(encodeURIComponent(this.options.version)) : ''
2915
+ requireConfig({
2916
+ urlArgs: options.version ? "v=".concat(encodeURIComponent(options.version)) : ''
3051
2917
  });
3052
2918
  return true;
3053
2919
  } catch (err) {
@@ -3057,54 +2923,23 @@
3057
2923
  }, {
3058
2924
  key: "getVersion",
3059
2925
  value: function getVersion() {
3060
- return this.options.version;
3061
- }
3062
-
3063
- /**
3064
- * 导出本实例的配置快照,供 `create()` 派生新实例时继承。
3065
- *
3066
- * 为什么需要:`create()` 原先是彻底干净的实例,于是宿主在单例上
3067
- * `configure({ manifest, lockVersion })` 之后,`_utils/models/_extend.js` 里那句
3068
- * `FileRequire.create({ require }).require(...)` 完全不受约束 —— 扩展脚本主链路上
3069
- * 登记制与版本锁等于没开。派生时继承配置,这个缺口才闭合。
3070
- *
3071
- * 刻意**不包含加载器**(`define` / `require` / `undef` / `requireConfig` / `importer`):
3072
- * 构造函数会按当前 `window.__devfab_requirejs` 重新取,派生实例拿到的是「此刻真正
3073
- * 在用的那个加载器」,而不是父实例构造时捕获的旧引用。要指定加载器请显式传,
3074
- * `create({ require })` 会覆盖。
3075
- */
3076
- }, {
3077
- key: "_snapshotConfig",
3078
- value: function _snapshotConfig() {
3079
- return {
3080
- __debug: this.__debug,
3081
- version: this.options.version,
3082
- timeout: this.options.timeout,
3083
- retry: this.options.retry,
3084
- strict: this.options.strict,
3085
- baseUrl: this.options.baseUrl,
3086
- manifest: this.policy.manifest,
3087
- origins: this.policy.origins,
3088
- lockVersion: this.policy.lockVersion,
3089
- verifyHash: this.policy.verifyHash,
3090
- fetch: this.policy.fetch,
3091
- pollution: this.guard.enabled,
3092
- watch: this.guard.watch,
3093
- maxPollution: this.guard.maxPollution,
3094
- target: this.guard.target
3095
- };
2926
+ return this.__data.options.version;
3096
2927
  }
3097
2928
 
3098
2929
  /** 策略 + 兜底配置摘要,宿主启动时打一行日志排查效率最高 */
3099
2930
  }, {
3100
2931
  key: "describe",
3101
2932
  value: function describe() {
3102
- return Object.assign({}, this.options, {
3103
- debug: this.__debug,
3104
- policy: this.policy.describe(),
3105
- pollutionGuard: this.guard.enabled,
3106
- canUndef: typeof this.__undef === 'function',
3107
- canNativeImport: typeof (this.__import || nativeImport) === 'function'
2933
+ var _this$__data3 = this.__data,
2934
+ options = _this$__data3.options,
2935
+ policy = _this$__data3.policy,
2936
+ undef = _this$__data3.undef,
2937
+ importImpl = _this$__data3.importImpl;
2938
+ return Object.assign({}, options, {
2939
+ debug: this.debug,
2940
+ policy: policy.describe(),
2941
+ canUndef: typeof undef === 'function',
2942
+ canNativeImport: typeof (importImpl || nativeImport) === 'function'
3108
2943
  });
3109
2944
  }
3110
2945
 
@@ -3113,8 +2948,7 @@
3113
2948
  key: "_define",
3114
2949
  value: function _define(_path, _files, _callback) {
3115
2950
  var params = {
3116
- path: this.__rootPath,
3117
- define: this.__define,
2951
+ define: this.__data.define,
3118
2952
  type: 'define'
3119
2953
  };
3120
2954
  var _parseArgs = parseArgs(_path, _files, _callback, params),
@@ -3126,28 +2960,16 @@
3126
2960
  callback === null || callback === void 0 || callback(result);
3127
2961
  return;
3128
2962
  }
3129
- this.__define(path, files, callback);
3130
- }
3131
- }, {
3132
- key: "_mergeWarnings",
3133
- value: function _mergeWarnings(prev, next) {
3134
- var list = (prev || []).slice();
3135
- (next || []).forEach(function (item) {
3136
- var exists = list.some(function (one) {
3137
- return one.type === item.type && one.key === item.key;
3138
- });
3139
- if (!exists) list.push(item);
3140
- });
3141
- return list;
2963
+ this.__data.define(path, files, callback);
3142
2964
  }
3143
2965
  }, {
3144
2966
  key: "_undefModules",
3145
2967
  value: function _undefModules(moduleIds) {
3146
- var _this = this;
3147
- if (typeof this.__undef !== 'function') return;
2968
+ var undef = this.__data.undef;
2969
+ if (typeof undef !== 'function') return;
3148
2970
  moduleIds.forEach(function (id) {
3149
2971
  try {
3150
- _this.__undef(id);
2972
+ undef(id);
3151
2973
  } catch (err) {
3152
2974
  // undef 只对曾经顶层 require 过的模块有效;抛错说明它还没进注册表,忽略即可
3153
2975
  }
@@ -3155,18 +2977,22 @@
3155
2977
  }
3156
2978
 
3157
2979
  /**
3158
- * 统一加载入口:策略校验 -> 观测 -> 计时 / 重试 -> 结算。
2980
+ * 统一加载入口:策略校验 -> 计时 / 重试 -> 结算。
3159
2981
  * 默认配置(version 空、retry 0、timeout 15s)下的行为与原来一致,
3160
- * 只是多了一个「更早失败」的兜底和 warnings 字段。
2982
+ * 只是多了一个「更早失败」的兜底。
3161
2983
  */
3162
2984
  }, {
3163
2985
  key: "_load",
3164
2986
  value: function _load(_path, _files, _callback) {
3165
- var _this2 = this;
3166
- var config = this.options;
2987
+ var _this = this;
2988
+ var _this$__data4 = this.__data,
2989
+ config = _this$__data4.options,
2990
+ policy = _this$__data4.policy,
2991
+ require = _this$__data4.require,
2992
+ setTimer = _this$__data4.setTimer,
2993
+ clearTimer = _this$__data4.clearTimer;
3167
2994
  var params = {
3168
- path: this.__rootPath,
3169
- require: this.__require,
2995
+ require: require,
3170
2996
  type: 'require'
3171
2997
  };
3172
2998
  var _parseArgs2 = parseArgs(_path, _files, _callback, params),
@@ -3180,7 +3006,7 @@
3180
3006
  return;
3181
3007
  }
3182
3008
  var moduleIds = toModuleIds(path, files);
3183
- var denied = this.policy.checkModules(moduleIds);
3009
+ var denied = policy.checkModules(moduleIds);
3184
3010
  if (denied) {
3185
3011
  result.code = denied.code;
3186
3012
  result.message = denied.message;
@@ -3190,40 +3016,26 @@
3190
3016
  var settled = false;
3191
3017
  var attempt = 0;
3192
3018
  var timer = null;
3193
- var scoop = null;
3194
- var closeScoop = function closeScoop() {
3195
- if (!scoop) return;
3196
- result.warnings = _this2._mergeWarnings(result.warnings, scoop.end());
3197
- scoop = null;
3198
- };
3199
3019
  var finish = function finish() {
3200
3020
  if (settled) return;
3201
3021
  settled = true;
3202
3022
  if (timer !== null) {
3203
- _this2._clearTimer(timer);
3023
+ clearTimer(timer);
3204
3024
  timer = null;
3205
3025
  }
3206
- closeScoop();
3207
- if (config.strict && result.warnings.length) {
3208
- result.code = 500;
3209
- result.message = "Global pollution detection failed\uFF1A".concat(result.warnings.map(function (item) {
3210
- return "".concat(item.type, ":").concat(item.key);
3211
- }).join(', '));
3212
- }
3213
3026
  callback === null || callback === void 0 || callback(result);
3214
3027
  };
3215
3028
  var start = function start() {
3216
3029
  attempt++;
3217
- scoop = _this2.guard.begin();
3218
3030
  if (config.timeout > 0) {
3219
- timer = _this2._setTimer(function () {
3031
+ timer = setTimer(function () {
3220
3032
  if (settled) return;
3221
3033
  var err = new Error("load timeout after ".concat(config.timeout, "ms: ").concat(filesStr));
3222
3034
  err.__devfabTimeout = true;
3223
3035
  retryOrFail(err, "files load timeout after ".concat(config.timeout, "ms, the files: ").concat(filesStr));
3224
3036
  }, config.timeout);
3225
3037
  }
3226
- _this2.__require(path, files, function () {
3038
+ require(path, files, function () {
3227
3039
  for (var _len = arguments.length, contents = new Array(_len), _key = 0; _key < _len; _key++) {
3228
3040
  contents[_key] = arguments[_key];
3229
3041
  }
@@ -3236,13 +3048,12 @@
3236
3048
  var retryOrFail = function retryOrFail(err, message) {
3237
3049
  if (settled) return;
3238
3050
  if (timer !== null) {
3239
- _this2._clearTimer(timer);
3051
+ clearTimer(timer);
3240
3052
  timer = null;
3241
3053
  }
3242
3054
  if (attempt <= config.retry) {
3243
- closeScoop();
3244
3055
  // 不 undef 就会复用上一次的失败状态,重试形同虚设
3245
- _this2._undefModules(moduleIds);
3056
+ _this._undefModules(moduleIds);
3246
3057
  start();
3247
3058
  return;
3248
3059
  }
@@ -3275,7 +3086,8 @@
3275
3086
  value: function resolveUrl(rel) {
3276
3087
  var raw = String(rel == null ? '' : rel);
3277
3088
  if (/^[a-z][a-z0-9+.-]*:/i.test(raw) || /^\/\//.test(raw)) return raw;
3278
- var base = this.options.baseUrl || (typeof document !== 'undefined' && document.baseURI ? document.baseURI : '');
3089
+ var baseUrl = this.__data.options.baseUrl;
3090
+ var base = baseUrl || (typeof document !== 'undefined' && document.baseURI ? document.baseURI : '');
3279
3091
  if (!base) return raw;
3280
3092
  try {
3281
3093
  return new URL(raw, base).href;
@@ -3286,9 +3098,11 @@
3286
3098
  }, {
3287
3099
  key: "_importOne",
3288
3100
  value: function _importOne(moduleId) {
3289
- var _this3 = this;
3101
+ var _this$__data5 = this.__data,
3102
+ policy = _this$__data5.policy,
3103
+ importImpl = _this$__data5.importImpl;
3290
3104
  var url = this.resolveUrl(moduleId);
3291
- var originDenied = this.policy.checkOrigin(url);
3105
+ var originDenied = policy.checkOrigin(url);
3292
3106
  if (originDenied) {
3293
3107
  return Promise.resolve({
3294
3108
  ok: false,
@@ -3296,10 +3110,10 @@
3296
3110
  message: originDenied.message
3297
3111
  });
3298
3112
  }
3299
- var entry = this.policy.entryOf(moduleId);
3113
+ var entry = policy.entryOf(moduleId);
3300
3114
  var expected = entry && entry.sha256;
3301
3115
  var run = function run() {
3302
- var importer = _this3.__import || nativeImport;
3116
+ var importer = importImpl || nativeImport;
3303
3117
  if (typeof importer !== 'function') {
3304
3118
  return Promise.resolve({
3305
3119
  ok: false,
@@ -3324,7 +3138,7 @@
3324
3138
  });
3325
3139
  };
3326
3140
  if (!expected) return run();
3327
- return this.policy.verify(url, expected).then(function (verified) {
3141
+ return policy.verify(url, expected).then(function (verified) {
3328
3142
  if (!verified.ok) {
3329
3143
  return {
3330
3144
  ok: false,
@@ -3346,9 +3160,9 @@
3346
3160
  }, {
3347
3161
  key: "loadModules",
3348
3162
  value: function loadModules(_path, _files, _callback) {
3349
- var _this4 = this;
3163
+ var _this2 = this;
3164
+ var policy = this.__data.policy;
3350
3165
  var params = {
3351
- path: this.__rootPath,
3352
3166
  type: 'module'
3353
3167
  };
3354
3168
  var _parseArgs3 = parseArgs(_path, _files, _callback, params),
@@ -3362,7 +3176,7 @@
3362
3176
  callback === null || callback === void 0 || callback(r);
3363
3177
  return r;
3364
3178
  };
3365
- var denied = this.policy.checkModules(moduleIds);
3179
+ var denied = policy.checkModules(moduleIds);
3366
3180
  if (denied) {
3367
3181
  result.code = denied.code;
3368
3182
  result.message = denied.message;
@@ -3371,11 +3185,9 @@
3371
3185
  if (!files.length) {
3372
3186
  return Promise.resolve(settle(result));
3373
3187
  }
3374
- var scoop = this.guard.begin();
3375
3188
  return Promise.all(moduleIds.map(function (id) {
3376
- return _this4._importOne(id);
3189
+ return _this2._importOne(id);
3377
3190
  })).then(function (list) {
3378
- result.warnings = scoop.end();
3379
3191
  var failed = list.filter(function (item) {
3380
3192
  return !item.ok;
3381
3193
  });
@@ -3408,15 +3220,10 @@
3408
3220
  }
3409
3221
  _inherits(FileRequireClass, _BaseFileRequire);
3410
3222
  return _createClass(FileRequireClass, [{
3411
- key: "define",
3412
- value: function define(path, files, callback) {
3223
+ key: "provide",
3224
+ value: function provide(path, files, callback) {
3413
3225
  this._define(path, files, callback);
3414
3226
  }
3415
- }, {
3416
- key: "defineScript",
3417
- value: function defineScript(o) {
3418
- this.provideScript(o);
3419
- }
3420
3227
  }, {
3421
3228
  key: "provideScript",
3422
3229
  value: function provideScript(o) {
@@ -3426,11 +3233,11 @@
3426
3233
  return o;
3427
3234
  };
3428
3235
  }
3429
- this.define([], callback);
3236
+ this.provide([], callback);
3430
3237
  }
3431
3238
  }, {
3432
- key: "require",
3433
- value: function require(path, files, callback) {
3239
+ key: "inject",
3240
+ value: function inject(path, files, callback) {
3434
3241
  this._load(path, files, callback);
3435
3242
  }
3436
3243
  }, {
@@ -3458,35 +3265,24 @@
3458
3265
  if (typeof error === 'function') {
3459
3266
  error(result);
3460
3267
  } else {
3461
- self.__debug && console.error('[devfab][FileRequire][injectScript] error: ', result);
3268
+ self.debug && console.error('[devfab][FileRequire][injectScript] error: ', result);
3462
3269
  }
3463
3270
  }
3464
3271
  }
3465
- this._load(path, files, onCallback);
3466
- }
3467
- }, {
3468
- key: "requireScript",
3469
- value: function requireScript(_path, _files, _callback) {
3470
- this.injectScript(_path, _files, _callback);
3272
+ this.inject(path, files, onCallback);
3471
3273
  }
3472
3274
  }]);
3473
3275
  }(BaseFileRequire);
3276
+ /**
3277
+ * 全局唯一的加载器实例。
3278
+ *
3279
+ * 加载策略(清单 / 白名单 / 版本锁)、全局污染观测、超时重试
3280
+ * 都是**部署级**约定,一个页面只应该有一套。
3281
+ */
3474
3282
  var FileRequire = new FileRequireClass();
3475
- FileRequire.create = function (args) {
3476
- return new FileRequireClass(_objectSpread2(_objectSpread2({}, this._snapshotConfig()), args));
3477
- };
3478
-
3479
- window.__devfab = Object.assign({}, window.__devfab);
3480
- window.__devfab.FileRequire = FileRequire;
3481
- window.__devfab.filePolicy = {
3482
- LoadPolicy: LoadPolicy,
3483
- GlobalGuard: GlobalGuard,
3484
- CODE_POLICY: CODE_POLICY,
3485
- CODE_TIMEOUT: CODE_TIMEOUT
3486
- };
3487
3283
 
3488
3284
  function isDebug(debug) {
3489
- return debug || window.devfab && window.devfab.debug;
3285
+ return debug || !!(typeof window !== 'undefined' && window.__devfab && window.__devfab.debug);
3490
3286
  }
3491
3287
  function QueryString(_url) {
3492
3288
  this.pathname = '';
@@ -3553,12 +3349,12 @@
3553
3349
  };
3554
3350
  this.init();
3555
3351
  }
3556
- String.prototype['equalsIgnoreCase'] = function (str) {
3557
- if (str == null) {
3352
+ function equalsIgnoreCase(str1, str2) {
3353
+ if (!str1 || !str2) {
3558
3354
  return false;
3559
3355
  }
3560
- return this.toLowerCase() === str.toLowerCase();
3561
- };
3356
+ return str1.toLowerCase() === str2.toLowerCase();
3357
+ }
3562
3358
  function _appendUrl(restUrl, params) {
3563
3359
  if (!params) {
3564
3360
  return restUrl;
@@ -3805,9 +3601,9 @@
3805
3601
  isDebug() && console.log('[devfab][ajax]: [doRequest]: options.params["requestHeaders"] ERR : ', e);
3806
3602
  }
3807
3603
  var queryJson = null;
3808
- if (method.equalsIgnoreCase('get') || method.equalsIgnoreCase('delete')) {
3604
+ if (equalsIgnoreCase(method, 'get') || equalsIgnoreCase(method, 'delete')) {
3809
3605
  url = _appendUrl(url, options.params);
3810
- } else if (method.equalsIgnoreCase('post') || method.equalsIgnoreCase('put')) {
3606
+ } else if (equalsIgnoreCase(method, 'post') || equalsIgnoreCase(method, 'put')) {
3811
3607
  queryJson = JSON.stringify(options.params);
3812
3608
  }
3813
3609
  if (options.jsonp) {
@@ -4831,7 +4627,7 @@
4831
4627
  config[attr].options.requestHeaders = requestHeaders;
4832
4628
  }
4833
4629
  if ((_this$getProxy = this.getProxy()) !== null && _this$getProxy !== void 0 && _this$getProxy[attr]) {
4834
- window.devfab.debug && console.warn("[devfab][baseModel.prototype.setProxy]: Proxy ".concat(attr, " already exists on the model. Please change the name of ").concat(attr, " and try again"));
4630
+ isDebug() && console.warn("[devfab][baseModel.prototype.setProxy]: Proxy ".concat(attr, " already exists on the model. Please change the name of ").concat(attr, " and try again"));
4835
4631
  }
4836
4632
  }
4837
4633
  this._set_data('proxy', DynamicProxy.create(_objectSpread2(_objectSpread2({}, ((_this$getProxy2 = this.getProxy()) === null || _this$getProxy2 === void 0 ? void 0 : _this$getProxy2.config) || {}), config)));
@@ -5443,7 +5239,15 @@
5443
5239
  }]);
5444
5240
  }(BaseModel$1);
5445
5241
 
5446
- function loadScripts(_require, _path, _files, _callback, _params) {
5242
+ function missingLoaderResult(filesStr) {
5243
+ return {
5244
+ code: 404,
5245
+ message: "params.fileRequire is required, the files: ".concat(filesStr),
5246
+ data: [],
5247
+ warnings: []
5248
+ };
5249
+ }
5250
+ function loadScripts(_fileRequire, _path, _files, _callback, _params) {
5447
5251
  var _params2;
5448
5252
  var path = _path;
5449
5253
  var files = _files;
@@ -5458,10 +5262,17 @@
5458
5262
  var viewModel = ((_params2 = params) === null || _params2 === void 0 ? void 0 : _params2.viewModel) || this;
5459
5263
  files = Array.isArray(files) ? files : [];
5460
5264
  var filesStr = files.join(',');
5461
- return FileRequire.create({
5462
- require: _require
5463
- }).require(path, files, function (result) {
5265
+ if (!_fileRequire || typeof _fileRequire.inject !== 'function') {
5464
5266
  var _callback2;
5267
+ var result = missingLoaderResult(filesStr);
5268
+ if (isDebug()) {
5269
+ console.error("[devfab][models][extend][loadScripts] ".concat(result.message));
5270
+ }
5271
+ (_callback2 = callback) === null || _callback2 === void 0 || _callback2(result);
5272
+ return result;
5273
+ }
5274
+ return _fileRequire.inject(path, files, function (result) {
5275
+ var _callback3;
5465
5276
  if (result.code == 0 && Array.isArray(result.data)) {
5466
5277
  for (var i = 0, len = result.data.length; i < len; i++) {
5467
5278
  var content = result.data[i];
@@ -5488,10 +5299,10 @@
5488
5299
  }
5489
5300
  }
5490
5301
  }
5491
- if (result.code != 0 && window.__devfab.debug) {
5302
+ if (result.code != 0 && isDebug()) {
5492
5303
  console.info("[devfab][models][extend][loadScripts] ".concat(filesStr, " \n [message]: ").concat(result.message, " \n").concat(result.error ? ' \n [error]: ' : ''), result.error);
5493
5304
  }
5494
- (_callback2 = callback) === null || _callback2 === void 0 || _callback2(result);
5305
+ (_callback3 = callback) === null || _callback3 === void 0 || _callback3(result);
5495
5306
  });
5496
5307
  }
5497
5308
  function initExtendScripts(params) {
@@ -5500,7 +5311,7 @@
5500
5311
  // eslint-disable-next-line no-debugger
5501
5312
  debugger;
5502
5313
  }
5503
- var requirejs = params.requirejs,
5314
+ var fileRequire = params.fileRequire,
5504
5315
  extendScriptParams = params.extendScriptParams,
5505
5316
  _params$readyEventNam = params.readyEventName,
5506
5317
  readyEventName = _params$readyEventNam === void 0 ? 'extendReady' : _params$readyEventNam;
@@ -5510,12 +5321,12 @@
5510
5321
  filepath = _ref.filepath,
5511
5322
  scriptUrl = _ref.scriptUrl;
5512
5323
  if (!enableJs) {
5513
- window.__devfab.debug && console.info("[devfab][extend][js] not enable extend script");
5324
+ isDebug() && console.info("[devfab][extend][js] not enable extend script");
5514
5325
  self.execute(readyEventName, self);
5515
5326
  return;
5516
5327
  }
5517
5328
  if (!scriptUrl) {
5518
- window.__devfab.debug && console.info("[devfab][extend][js] extend script is not defined");
5329
+ isDebug() && console.info("[devfab][extend][js] extend script is not defined");
5519
5330
  self.execute(readyEventName, self);
5520
5331
  return;
5521
5332
  }
@@ -5525,14 +5336,14 @@
5525
5336
  if (self.getEnv && self.getEnv('everyCommonExtend')) {
5526
5337
  extendFiles.push(self.getEnv('everyCommonExtend'));
5527
5338
  }
5528
- loadScripts.call(self, requirejs === null || requirejs === void 0 ? void 0 : requirejs.require, extendFilePath, extendFiles, function (result) {
5339
+ loadScripts.call(self, fileRequire, extendFilePath, extendFiles, function (result) {
5529
5340
  self.execute(readyEventName, self, result);
5530
5341
  });
5531
5342
  }
5532
5343
  var initExtScripts = /*#__PURE__*/function () {
5533
5344
  var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(data, viewModel, params) {
5534
5345
  var _viewModel$getParams, _exturls;
5535
- var requirejs, extendScriptParams, _ref3, _ref3$enableExtJs, enableExtJs, _ref3$exturlsField, exturlsField, extscripturls, exturls;
5346
+ var fileRequire, extendScriptParams, _ref3, _ref3$enableExtJs, enableExtJs, _ref3$exturlsField, exturlsField, extscripturls, exturls;
5536
5347
  return _regenerator().w(function (_context2) {
5537
5348
  while (1) switch (_context2.n) {
5538
5349
  case 0:
@@ -5540,20 +5351,20 @@
5540
5351
  // eslint-disable-next-line no-debugger
5541
5352
  debugger;
5542
5353
  }
5543
- requirejs = params.requirejs, extendScriptParams = params.extendScriptParams;
5354
+ fileRequire = params.fileRequire, extendScriptParams = params.extendScriptParams;
5544
5355
  _ref3 = extendScriptParams || {}, _ref3$enableExtJs = _ref3.enableExtJs, enableExtJs = _ref3$enableExtJs === void 0 ? false : _ref3$enableExtJs, _ref3$exturlsField = _ref3.exturlsField, exturlsField = _ref3$exturlsField === void 0 ? 'extscripturls' : _ref3$exturlsField;
5545
5356
  if (enableExtJs) {
5546
5357
  _context2.n = 1;
5547
5358
  break;
5548
5359
  }
5549
- window.__devfab.debug && console.info("[devfab][extend][exturls] ext script is not enable");
5360
+ isDebug() && console.info("[devfab][extend][exturls] ext script is not enable");
5550
5361
  return _context2.a(2, Promise.resolve(true));
5551
5362
  case 1:
5552
5363
  if (data) {
5553
5364
  _context2.n = 2;
5554
5365
  break;
5555
5366
  }
5556
- window.__devfab.debug && console.info("[devfab][extend][exturls] script data is null");
5367
+ isDebug() && console.info("[devfab][extend][exturls] script data is null");
5557
5368
  return _context2.a(2, Promise.resolve(true));
5558
5369
  case 2:
5559
5370
  extscripturls = data === null || data === void 0 ? void 0 : data[exturlsField];
@@ -5593,7 +5404,7 @@
5593
5404
  case 4:
5594
5405
  data[exturlsField] = exturls;
5595
5406
  return _context2.a(2, new Promise(function (resolve, reject) {
5596
- loadScripts.call(viewModel, requirejs === null || requirejs === void 0 ? void 0 : requirejs.require, exturls, function (result) {
5407
+ loadScripts.call(viewModel, fileRequire, exturls, function (result) {
5597
5408
  resolve(result);
5598
5409
  });
5599
5410
  }));
@@ -5696,7 +5507,7 @@
5696
5507
  var _vm, _vm$destroyVM;
5697
5508
  (_vm = vm) === null || _vm === void 0 || (_vm$destroyVM = _vm.destroyVM) === null || _vm$destroyVM === void 0 || _vm$destroyVM.call(_vm);
5698
5509
  vm = null;
5699
- window.devfab.debug && console.info("[devfab][models][initVM]: destory vm ".concat(new Date().toString()));
5510
+ isDebug() && console.info("[devfab][models][initVM]: destory vm ".concat(new Date().toString()));
5700
5511
  });
5701
5512
  vm.on('extendReady', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
5702
5513
  var promises;
@@ -5708,7 +5519,7 @@
5708
5519
  _context.n = 1;
5709
5520
  return Promise.all(promises);
5710
5521
  case 1:
5711
- window.devfab.debug && console.info("[devfab][models][initVM]: after vm init ".concat(new Date().toString()));
5522
+ isDebug() && console.info("[devfab][models][initVM]: after vm init ".concat(new Date().toString()));
5712
5523
  callback === null || callback === void 0 || callback(vm, meta);
5713
5524
  case 2:
5714
5525
  return _context.a(2);
@@ -5738,13 +5549,24 @@
5738
5549
  };
5739
5550
 
5740
5551
  var devfab = {
5741
- debug: false,
5742
- FileRequire: FileRequire,
5552
+ get debug() {
5553
+ return !!(window.__devfab && window.__devfab.debug);
5554
+ },
5555
+ set debug(value) {
5556
+ window.__devfab = window.__devfab || {};
5557
+ window.__devfab.debug = !!value;
5558
+ },
5743
5559
  define: function define() {
5744
- FileRequire.define.apply(FileRequire, arguments);
5560
+ FileRequire.provide.apply(FileRequire, arguments);
5745
5561
  },
5746
5562
  require: function require() {
5747
- FileRequire.require.apply(FileRequire, arguments);
5563
+ FileRequire.inject.apply(FileRequire, arguments);
5564
+ },
5565
+ provide: function provide() {
5566
+ FileRequire.provide.apply(FileRequire, arguments);
5567
+ },
5568
+ inject: function inject() {
5569
+ FileRequire.inject.apply(FileRequire, arguments);
5748
5570
  },
5749
5571
  provideScript: function provideScript() {
5750
5572
  FileRequire.provideScript.apply(FileRequire, arguments);
@@ -5752,6 +5574,7 @@
5752
5574
  injectScript: function injectScript() {
5753
5575
  FileRequire.injectScript.apply(FileRequire, arguments);
5754
5576
  },
5577
+ FileRequire: FileRequire,
5755
5578
  utils: _objectSpread2(_objectSpread2({}, utils), {}, {
5756
5579
  builder: utils.Builder.create(),
5757
5580
  cache: utils.Cache.create(),