markmap-autoloader 0.14.5-alpha.21 → 0.14.5-alpha.24

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.
Files changed (2) hide show
  1. package/dist/index.js +69 -29
  2. package/package.json +6 -6
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- /*! markmap-autoloader v0.14.5-alpha.21+fb81f2c | MIT License */
1
+ /*! markmap-autoloader v0.14.5-alpha.24+08dd40c | MIT License */
2
2
  this.markmap = this.markmap || {};
3
3
  (function (exports) {
4
4
  'use strict';
@@ -18,7 +18,7 @@ function _extends$1() {
18
18
  return _extends$1.apply(this, arguments);
19
19
  }
20
20
 
21
- /*! markmap-common v0.14.5-alpha.21+fb81f2c | MIT License */
21
+ /*! markmap-common v0.14.5-alpha.24+08dd40c | MIT License */
22
22
  function _extends() {
23
23
  _extends = Object.assign ? Object.assign.bind() : function (target) {
24
24
  for (var i = 1; i < arguments.length; i++) {
@@ -33,6 +33,49 @@ function _extends() {
33
33
  };
34
34
  return _extends.apply(this, arguments);
35
35
  }
36
+ const testPath = "npm2url/dist/index.cjs";
37
+ const defaultProviders = {
38
+ jsdelivr: path => `https://cdn.jsdelivr.net/npm/${path}`,
39
+ unpkg: path => `https://unpkg.com/${path}`
40
+ };
41
+ class UrlBuilder {
42
+ constructor() {
43
+ this.providers = _extends({}, defaultProviders);
44
+ this.provider = "jsdelivr";
45
+ }
46
+ getFastestProvider(path = testPath) {
47
+ return Promise.any(Object.entries(this.providers).map(async ([name, factory]) => {
48
+ const res = await fetch(factory(path));
49
+ if (!res.ok) {
50
+ throw res;
51
+ }
52
+ await res.text();
53
+ return name;
54
+ }));
55
+ }
56
+ async findFastestProvider() {
57
+ this.provider = await this.getFastestProvider();
58
+ return this.provider;
59
+ }
60
+ setProvider(name, factory) {
61
+ if (factory) {
62
+ this.providers[name] = factory;
63
+ } else {
64
+ delete this.providers[name];
65
+ }
66
+ }
67
+ getFullUrl(path, provider = this.provider) {
68
+ if (path.includes("://")) {
69
+ return path;
70
+ }
71
+ const factory = this.providers[provider];
72
+ if (!factory) {
73
+ throw new Error(`Provider ${provider} not found`);
74
+ }
75
+ return factory(path);
76
+ }
77
+ }
78
+ const urlBuilder = new UrlBuilder();
36
79
  Math.random().toString(36).slice(2, 8);
37
80
  function memoize(fn) {
38
81
  const cache = {};
@@ -203,17 +246,6 @@ function mountDom(vnode) {
203
246
  function hm(...args) {
204
247
  return mountDom(h(...args));
205
248
  }
206
- const providers = {
207
- jsdelivr: path => `https://cdn.jsdelivr.net/npm/${path}`,
208
- unpkg: path => `https://unpkg.com/${path}`
209
- };
210
- function cdnUrl(provider, path) {
211
- const factory = providers[provider];
212
- if (!factory) {
213
- throw new Error(`Provider ${provider} not found`);
214
- }
215
- return factory(path);
216
- }
217
249
  const memoizedPreloadJS = memoize(url => {
218
250
  document.head.append(hm('link', {
219
251
  rel: 'preload',
@@ -221,20 +253,27 @@ const memoizedPreloadJS = memoize(url => {
221
253
  href: url
222
254
  }));
223
255
  });
256
+ const jsCache = {};
257
+ const cssCache = {};
224
258
  async function loadJSItem(item, context) {
259
+ var _item$data;
260
+ const src = item.type === 'script' && ((_item$data = item.data) == null ? void 0 : _item$data.src) || '';
261
+ item.loaded || (item.loaded = jsCache[src]);
225
262
  if (!item.loaded) {
226
263
  if (item.type === 'script') {
227
264
  item.loaded = new Promise((resolve, reject) => {
228
- var _item$data;
229
265
  document.head.append(hm('script', _extends({}, item.data, {
230
266
  onLoad: resolve,
231
267
  onError: reject
232
268
  })));
233
- // Run inline script synchronously
234
- if (!((_item$data = item.data) != null && _item$data.src)) resolve(undefined);
269
+ if (!src) {
270
+ // Run inline script synchronously
271
+ resolve(undefined);
272
+ }
235
273
  }).then(() => {
236
274
  item.loaded = true;
237
275
  });
276
+ if (src) jsCache[src] = item.loaded;
238
277
  }
239
278
  if (item.type === 'iife') {
240
279
  const {
@@ -248,8 +287,11 @@ async function loadJSItem(item, context) {
248
287
  await item.loaded;
249
288
  }
250
289
  function loadCSSItem(item) {
290
+ const url = item.type === 'stylesheet' && item.data.href || '';
291
+ item.loaded || (item.loaded = cssCache[url]);
251
292
  if (item.loaded) return;
252
293
  item.loaded = true;
294
+ if (url) cssCache[url] = true;
253
295
  if (item.type === 'style') {
254
296
  document.head.append(hm('style', {
255
297
  textContent: item.data
@@ -279,24 +321,19 @@ function loadCSS(items) {
279
321
  loadCSSItem(item);
280
322
  }
281
323
  }
282
- let provider = 'jsdelivr';
283
- function getFullUrl(path, overrideProvider = provider) {
284
- if (path.includes('://')) return path;
285
- return cdnUrl(overrideProvider, path);
286
- }
287
- function buildJSItem(path, overrideProvider) {
324
+ function buildJSItem(path) {
288
325
  return {
289
326
  type: 'script',
290
327
  data: {
291
- src: getFullUrl(path, overrideProvider)
328
+ src: path
292
329
  }
293
330
  };
294
331
  }
295
- function buildCSSItem(path, overrideProvider) {
332
+ function buildCSSItem(path) {
296
333
  return {
297
334
  type: 'stylesheet',
298
335
  data: {
299
- href: getFullUrl(path, overrideProvider)
336
+ href: path
300
337
  }
301
338
  };
302
339
  }
@@ -304,12 +341,14 @@ function buildCSSItem(path, overrideProvider) {
304
341
  var _window$markmap;
305
342
  const enabled = {};
306
343
  const autoLoaderOptions = _extends$1({
307
- baseJs: [`d3@${"7.8.5"}`, `markmap-lib@${"0.14.5-alpha.21"}`, `markmap-view@${"0.14.5-alpha.21"}`, `markmap-toolbar@${"0.14.5-alpha.21"}`],
308
- baseCss: [`markmap-toolbar@${"0.14.5-alpha.21"}/dist/style.css`],
344
+ baseJs: [`d3@${"7.8.5"}`, `markmap-lib@${"0.14.5-alpha.24"}`, `markmap-view@${"0.14.5-alpha.24"}`, `markmap-toolbar@${"0.14.5-alpha.24"}`],
345
+ baseCss: [`markmap-toolbar@${"0.14.5-alpha.24"}/dist/style.css`],
309
346
  manual: false,
310
347
  toolbar: false
311
348
  }, (_window$markmap = window.markmap) == null ? void 0 : _window$markmap.autoLoader);
312
- const ready = Promise.all([loadJS(autoLoaderOptions.baseJs.map(item => typeof item === 'string' ? buildJSItem(item) : item)), loadCSS(autoLoaderOptions.baseCss.map(item => typeof item === 'string' ? buildCSSItem(item) : item))]).then(() => {
349
+ async function initialize() {
350
+ await urlBuilder.findFastestProvider();
351
+ await Promise.all([loadJS(autoLoaderOptions.baseJs.map(item => typeof item === 'string' ? buildJSItem(urlBuilder.getFullUrl(item)) : item)), loadCSS(autoLoaderOptions.baseCss.map(item => typeof item === 'string' ? buildCSSItem(urlBuilder.getFullUrl(item)) : item))]);
313
352
  const {
314
353
  markmap
315
354
  } = window;
@@ -318,7 +357,8 @@ const ready = Promise.all([loadJS(autoLoaderOptions.baseJs.map(item => typeof it
318
357
  // Insert global CSS to body so it has higher priority than prism.css, etc.
319
358
  document.body.prepend(style);
320
359
  autoLoaderOptions.onReady == null ? void 0 : autoLoaderOptions.onReady();
321
- });
360
+ }
361
+ const ready = initialize();
322
362
  function transform(transformer, content) {
323
363
  const result = transformer.transform(content);
324
364
  const keys = Object.keys(result.features).filter(key => !enabled[key]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "markmap-autoloader",
3
- "version": "0.14.5-alpha.21+fb81f2c",
3
+ "version": "0.14.5-alpha.24+08dd40c",
4
4
  "description": "Load markmaps automatically in HTML",
5
5
  "author": "Gerald <gera2ld@live.com>",
6
6
  "license": "MIT",
@@ -38,10 +38,10 @@
38
38
  "markmap-common": "*"
39
39
  },
40
40
  "devDependencies": {
41
- "markmap-common": "0.14.5-alpha.21+fb81f2c",
42
- "markmap-lib": "0.14.5-alpha.21+fb81f2c",
43
- "markmap-toolbar": "0.14.5-alpha.21+fb81f2c",
44
- "markmap-view": "0.14.5-alpha.21+fb81f2c"
41
+ "markmap-common": "0.14.5-alpha.24+08dd40c",
42
+ "markmap-lib": "0.14.5-alpha.24+08dd40c",
43
+ "markmap-toolbar": "0.14.5-alpha.24+08dd40c",
44
+ "markmap-view": "0.14.5-alpha.24+08dd40c"
45
45
  },
46
- "gitHead": "fb81f2cb688f400f17f911774539267219f639ce"
46
+ "gitHead": "08dd40c9353058928a148f6b97d11e98ec1d328b"
47
47
  }