@worktile/planet 17.0.0-next.1 → 17.0.0-next.3

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.
@@ -106,7 +106,7 @@ function getExtName(name) {
106
106
  function buildResourceFilePath(resourceFilePath, manifestResult) {
107
107
  const fileName = getResourceFileName(resourceFilePath);
108
108
  if (manifestResult[fileName]) {
109
- return resourceFilePath.replace(fileName, manifestResult[fileName]);
109
+ return resourceFilePath.replace(fileName, manifestResult[fileName].src);
110
110
  }
111
111
  else {
112
112
  return resourceFilePath;
@@ -148,56 +148,56 @@ function getAssetsBasePath(app) {
148
148
  }
149
149
  return basePath || app.resourcePathPrefix;
150
150
  }
151
- /**
152
- * Get static resource full path
153
- * @param app PlanetApplication
154
- * @param manifestResult manifest
155
- */
156
- function getScriptsAndStylesFullPaths(app, basePath, manifestResult) {
151
+ function getAssetsByDefined(definedPaths, manifest, ext) {
152
+ if (definedPaths) {
153
+ return definedPaths.map(definedPath => {
154
+ const fileName = getResourceFileName(definedPath);
155
+ const assetsTagItem = manifest[fileName];
156
+ return {
157
+ ...assetsTagItem,
158
+ src: definedPath.replace(fileName, assetsTagItem.src)
159
+ };
160
+ });
161
+ }
162
+ else {
163
+ return Object.keys(manifest)
164
+ .filter(key => {
165
+ return getExtName(key) === ext;
166
+ })
167
+ .map(key => {
168
+ return manifest[key];
169
+ });
170
+ }
171
+ }
172
+ function toAssetsTagItem(src) {
173
+ return {
174
+ src: src
175
+ };
176
+ }
177
+ function toAssetsTagItems(src) {
178
+ return src.map(item => toAssetsTagItem(item));
179
+ }
180
+ function getScriptsAndStylesAssets(app, basePath, manifestResult) {
181
+ const result = { scripts: [], styles: [] };
157
182
  let { scripts, styles } = getDefinedAssets(app);
158
183
  // combine resource path by manifest
159
184
  if (manifestResult) {
160
- if (scripts) {
161
- scripts = scripts.map(script => {
162
- return buildResourceFilePath(script, manifestResult);
163
- });
164
- }
165
- else {
166
- scripts = Object.keys(manifestResult)
167
- .filter(key => {
168
- return getExtName(key) === 'js';
169
- })
170
- .map(key => {
171
- return manifestResult[key];
172
- });
173
- }
174
- if (styles) {
175
- styles = styles.map(style => {
176
- return buildResourceFilePath(style, manifestResult);
177
- });
178
- }
179
- else {
180
- styles = Object.keys(manifestResult)
181
- .filter(key => {
182
- return getExtName(key) === 'css';
183
- })
184
- .map(key => {
185
- return manifestResult[key];
186
- });
187
- }
185
+ result.scripts = getAssetsByDefined(scripts, manifestResult, 'js');
186
+ result.styles = getAssetsByDefined(styles, manifestResult, 'css');
187
+ }
188
+ else {
189
+ result.scripts = toAssetsTagItems(scripts);
190
+ result.styles = toAssetsTagItems(styles);
188
191
  }
189
192
  if (basePath) {
190
- scripts = scripts.map(script => {
191
- return buildFullPath(script, basePath);
193
+ result.scripts.forEach(item => {
194
+ item.src = buildFullPath(item.src, basePath);
192
195
  });
193
- styles = styles.map(style => {
194
- return buildFullPath(style, basePath);
196
+ result.styles.forEach(item => {
197
+ item.src = buildFullPath(item.src, basePath);
195
198
  });
196
199
  }
197
- return {
198
- scripts: scripts || [],
199
- styles: styles || []
200
- };
200
+ return result;
201
201
  }
202
202
 
203
203
  const PLANET_SANDBOX_WINDOW_WHITELIST = window['___PLANET_SANDBOX_WINDOW_WHITELIST___'];
@@ -850,14 +850,15 @@ function getSandboxInstance() {
850
850
  return window[SANDBOX_INSTANCE];
851
851
  }
852
852
 
853
- const STYLE_LINK_OR_SCRIPT_REG = /<[script|link].*?">/gi;
854
- const LINK_OR_SRC_REG = /(src|href)=["'](.*?)["']/i;
853
+ const STYLE_LINK_OR_SCRIPT_REG = /<[script|link].*?>/gi;
854
+ const LINK_OR_SRC_REG = /(src|href)=["'](.*?[\.js|\.css])["']/i;
855
+ const TAG_ATTRS_REG = /(type|defer|async)((=["'].*?["'])|\s|\>)/gi;
855
856
  class AssetsLoader {
856
857
  constructor(http) {
857
858
  this.http = http;
858
859
  this.loadedSources = [];
859
860
  }
860
- loadScript(src) {
861
+ loadScript(src, tagAttributes) {
861
862
  const id = hashCode(src);
862
863
  if (this.loadedSources.includes(id)) {
863
864
  return of({
@@ -869,9 +870,14 @@ class AssetsLoader {
869
870
  }
870
871
  return new Observable((observer) => {
871
872
  const script = document.createElement('script');
872
- script.type = 'text/javascript';
873
+ script.type = tagAttributes?.type || 'text/javascript';
873
874
  script.src = src;
874
- script.async = true;
875
+ if (!tagAttributes?.defer || tagAttributes?.defer !== 'false') {
876
+ script.defer = true;
877
+ }
878
+ if (!tagAttributes?.async && tagAttributes?.async === 'false') {
879
+ script.async = true;
880
+ }
875
881
  if (script['readyState']) {
876
882
  // IE
877
883
  script['onreadystatechange'] = () => {
@@ -995,13 +1001,13 @@ class AssetsLoader {
995
1001
  if (isEmpty(sources)) {
996
1002
  return of(null);
997
1003
  }
998
- const observables = sources.map(src => {
1004
+ const observables = sources.map(source => {
999
1005
  // TODO: 暂时只支持Proxy沙箱
1000
1006
  if (options.sandbox && window.Proxy) {
1001
- return this.loadScriptWithSandbox(options.app, src);
1007
+ return this.loadScriptWithSandbox(options.app, source.src);
1002
1008
  }
1003
1009
  else {
1004
- return this.loadScript(src);
1010
+ return this.loadScript(source.src, source.attributes);
1005
1011
  }
1006
1012
  });
1007
1013
  if (options.serial) {
@@ -1018,19 +1024,46 @@ class AssetsLoader {
1018
1024
  if (isEmpty(sources)) {
1019
1025
  return of(null);
1020
1026
  }
1021
- return forkJoin(sources.map(src => {
1022
- return this.loadStyle(src);
1027
+ return forkJoin(sources.map(source => {
1028
+ return this.loadStyle(source.src);
1023
1029
  }));
1024
1030
  }
1025
1031
  loadScriptsAndStyles(scripts = [], styles = [], options) {
1026
1032
  return forkJoin([this.loadScripts(scripts, options), this.loadStyles(styles)]);
1027
1033
  }
1034
+ /**
1035
+ * <script type="module" src="http://127.0.0.1:3001/main.js" async defer></script>
1036
+ * => [`type="module"`, "async ", "defer>"] as attributeStrMatchArr
1037
+ * => { type: "module", async: "async", defer: "defer" } as attributes
1038
+ */
1039
+ parseTagAttributes(tag) {
1040
+ const attributeStrMatchArr = tag.match(TAG_ATTRS_REG);
1041
+ if (attributeStrMatchArr) {
1042
+ const attributes = {};
1043
+ attributeStrMatchArr.forEach(item => {
1044
+ const equalSignIndex = item.indexOf('=');
1045
+ if (equalSignIndex > 0) {
1046
+ // 'type="module"' => { type: "module" }
1047
+ const key = item.slice(0, equalSignIndex);
1048
+ attributes[key] = item.slice(equalSignIndex + 2, item.length - 1);
1049
+ }
1050
+ else {
1051
+ // 'async ' => 'async'
1052
+ // 'defer>' => 'defer'
1053
+ const key = item.slice(0, item.length - 1);
1054
+ attributes[key] = key;
1055
+ }
1056
+ });
1057
+ return attributes;
1058
+ }
1059
+ return undefined;
1060
+ }
1028
1061
  parseManifestFromHTML(html) {
1029
1062
  const result = {};
1030
1063
  const matchResult = html.match(STYLE_LINK_OR_SCRIPT_REG);
1031
1064
  matchResult.forEach(item => {
1032
1065
  const linkOrSrcResult = item.match(LINK_OR_SRC_REG);
1033
- if (linkOrSrcResult[2]) {
1066
+ if (linkOrSrcResult && linkOrSrcResult[2]) {
1034
1067
  const src = linkOrSrcResult[2];
1035
1068
  const hashName = getResourceFileName(src);
1036
1069
  let barSplitIndex = hashName.indexOf('-');
@@ -1039,7 +1072,20 @@ class AssetsLoader {
1039
1072
  if (splitIndex > -1) {
1040
1073
  const name = hashName.slice(0, splitIndex);
1041
1074
  const ext = getExtName(hashName);
1042
- result[ext ? `${name}.${ext}` : name] = src;
1075
+ const assetsTag = {
1076
+ src: src
1077
+ };
1078
+ result[ext ? `${name}.${ext}` : name] = assetsTag;
1079
+ const attributes = this.parseTagAttributes(item);
1080
+ if (attributes) {
1081
+ assetsTag.attributes = attributes;
1082
+ }
1083
+ // const typeTagResult = item.match(TAG_TYPE_REG);
1084
+ // if (typeTagResult && typeTagResult[1]) {
1085
+ // assetsTag.attributes = {
1086
+ // type: typeTagResult[1]
1087
+ // };
1088
+ // }
1043
1089
  }
1044
1090
  }
1045
1091
  });
@@ -1053,10 +1099,7 @@ class AssetsLoader {
1053
1099
  const isHtml = manifestExt === 'html';
1054
1100
  const responseType = isHtml ? 'text' : 'json';
1055
1101
  return this.loadManifest(`${manifest}?t=${new Date().getTime()}`, responseType).pipe(switchMap(manifestResult => {
1056
- if (responseType === 'text') {
1057
- manifestResult = this.parseManifestFromHTML(manifestResult);
1058
- }
1059
- const { scripts, styles } = getScriptsAndStylesFullPaths(app, basePath, manifestResult);
1102
+ const { scripts, styles } = getScriptsAndStylesAssets(app, basePath, manifestResult);
1060
1103
  return this.loadScriptsAndStyles(scripts, styles, {
1061
1104
  app: app.name,
1062
1105
  sandbox: app.sandbox,
@@ -1065,7 +1108,7 @@ class AssetsLoader {
1065
1108
  }));
1066
1109
  }
1067
1110
  else {
1068
- const { scripts, styles } = getScriptsAndStylesFullPaths(app, basePath);
1111
+ const { scripts, styles } = getScriptsAndStylesAssets(app, basePath);
1069
1112
  return this.loadScriptsAndStyles(scripts, styles, {
1070
1113
  app: app.name,
1071
1114
  sandbox: app.sandbox,
@@ -1079,7 +1122,18 @@ class AssetsLoader {
1079
1122
  responseType: responseType
1080
1123
  })
1081
1124
  .pipe(map((response) => {
1082
- return response;
1125
+ if (responseType === 'text') {
1126
+ return this.parseManifestFromHTML(response);
1127
+ }
1128
+ else {
1129
+ const result = {};
1130
+ Object.keys(response).forEach(key => {
1131
+ result[key] = {
1132
+ src: response[key]
1133
+ };
1134
+ });
1135
+ return result;
1136
+ }
1083
1137
  }));
1084
1138
  }
1085
1139
  static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.2.3", ngImport: i0, type: AssetsLoader, deps: [{ token: i1.HttpClient }], target: i0.ɵɵFactoryTarget.Injectable }); }