use standard polyfills (only tested with esm)

This commit is contained in:
jfh
2020-08-09 18:33:17 +02:00
parent ff98330ddf
commit 842f59b6c1
21 changed files with 57243 additions and 3282 deletions

View File

@@ -7,8 +7,6 @@
* @copyright 2013 Jo Segaert
*
*/
// Todo: Wait for Mathjax 3.0 to get ES Module/avoid global
import {importScript} from '../../external/dynamic-import-polyfill/importModule.js';
export default {
name: 'mathjax',
@@ -204,7 +202,8 @@ export default {
// We use `extIconsPath` here for now as it does not vary with
// the modular type as does `extPath`
try {
await importScript(svgEditor.curConfig.extIconsPath + mathjaxSrcSecure);
// eslint-disable-next-line node/no-unsupported-features/es-syntax
await import(svgEditor.curConfig.extIconsPath + mathjaxSrcSecure);
// When MathJax is loaded get the div where the math will be rendered.
MathJax.Hub.queue.Push(function () {
math = MathJax.Hub.getAllJax('#mathjax_creator')[0];

View File

@@ -23,19 +23,9 @@
<script src="jquery.min.js"></script>
<!--{endif}-->
<!-- Lacking browser support -->
<script nomodule="" src="redirect-on-no-module-support.js"></script>
<script type="module" src="redirect-on-lacking-support.js"></script>
<!-- As yet no ES6 Module support -->
<script src="jquery-ui/jquery-ui-1.8.17.custom.min.js"></script>
<!-- Browser polyfills -->
<script src="../external/dom-polyfill/dom-polyfill.js"></script>
<!-- ES6+ polyfills (Babel) -->
<script src="../external/core-js-bundle/minified.js"></script>
<script src="../external/regenerator-runtime/runtime.js"></script>
<!-- If you do not wish to add extensions by URL, you can add calls
within the following file to svgEditor.setConfig -->

19709
dist/editor/index-iife.js vendored

File diff suppressed because it is too large Load Diff

View File

@@ -23,19 +23,9 @@
<script src="jquery.min.js"></script>
<!--{endif}-->
<!-- Lacking browser support -->
<script nomodule="" src="redirect-on-no-module-support.js"></script>
<script type="module" src="redirect-on-lacking-support.js"></script>
<!-- As yet no ES6 Module support -->
<script src="jquery-ui/jquery-ui-1.8.17.custom.min.js"></script>
<!-- Browser polyfills -->
<script src="../external/dom-polyfill/dom-polyfill.js"></script>
<!-- ES6+ polyfills (Babel) -->
<script src="../external/core-js-bundle/minified.js"></script>
<script src="../external/regenerator-runtime/runtime.js"></script>
<!-- If you do not wish to add extensions by URL, you can add calls
within the following file to svgEditor.setConfig -->

19709
dist/editor/index-umd.js vendored

File diff suppressed because it is too large Load Diff

View File

@@ -23,19 +23,9 @@
<script src="jquery.min.js"></script>
<!--{endif}-->
<!-- Lacking browser support -->
<script nomodule="" src="redirect-on-no-module-support.js"></script>
<script type="module" src="redirect-on-lacking-support.js"></script>
<!-- As yet no ES6 Module support -->
<script src="jquery-ui/jquery-ui-1.8.17.custom.min.js"></script>
<!-- Browser polyfills -->
<script src="../external/dom-polyfill/dom-polyfill.js"></script>
<!-- ES6+ polyfills (Babel) -->
<script src="../external/core-js-bundle/minified.js"></script>
<script src="../external/regenerator-runtime/runtime.js"></script>
<!-- If you do not wish to add extensions by URL, you can add calls
within the following file to svgEditor.setConfig -->

19710
dist/editor/index.js vendored

File diff suppressed because it is too large Load Diff

1
dist/editor/index.js.map vendored Normal file

File diff suppressed because one or more lines are too long

View File

@@ -24,8 +24,6 @@
* @typedef {PlainObject<string, string>} module:locale.LocaleSelectorValue
*/
import {importSetGlobalDefault} from '../../external/dynamic-import-polyfill/importModule.js';
const $ = jQuery;
let langParam;
@@ -388,11 +386,7 @@ export const putLocale = async function (givenParam, goodLangs, conf) {
}
const url = conf.langPath + 'lang.' + langParam + '.js';
return readLang(
// Todo: Replace this with `return import(url);` when
// `import()` widely supported
await importSetGlobalDefault(url, {
global: 'svgEditorLang_' + langParam.replace(/-/g, '_')
})
);
// eslint-disable-next-line node/no-unsupported-features/es-syntax
const module = await import(url);
return readLang(module.default);
};

View File

@@ -1,176 +0,0 @@
/* eslint-disable jsdoc/require-file-overview */
/**
* Adapted from {@link https://github.com/uupaa/dynamic-import-polyfill/blob/master/importModule.js}.
* @module importModule
* @license MIT
*/
/**
* Converts a possible relative URL into an absolute one.
* @param {string} url
* @returns {string}
*/
function toAbsoluteURL (url) {
const a = document.createElement('a');
a.setAttribute('href', url); // <a href="hoge.html">
return a.cloneNode(false).href; // -> "http://example.com/hoge.html"
}
/**
* Add any of the whitelisted attributes to the script tag.
* @param {HTMLScriptElement} script
* @param {PlainObject<string, string>} atts
* @returns {void}
*/
function addScriptAtts (script, atts) {
['id', 'class', 'type'].forEach((prop) => {
if (prop in atts) {
script[prop] = atts[prop];
}
});
}
// Additions by Brett
/**
* @typedef {PlainObject} module:importModule.ImportConfig
* @property {string} global The variable name to set on `window` (when not using the modular version)
* @property {boolean} [returnDefault=false]
*/
/**
* @function module:importModule.importSetGlobalDefault
* @param {string|GenericArray<any>} url
* @param {module:importModule.ImportConfig} config
* @returns {Promise<any>} The value to which it resolves depends on the export of the targeted module.
*/
export function importSetGlobalDefault (url, config) {
return importSetGlobal(url, {...config, returnDefault: true});
}
/**
* @function module:importModule.importSetGlobal
* @param {string|string[]} url
* @param {module:importModule.ImportConfig} config
* @returns {Promise<ArbitraryModule>} The promise resolves to either an `ArbitraryModule` or
* any other value depends on the export of the targeted module.
*/
export async function importSetGlobal (url, {global: glob, returnDefault}) {
// Todo: Replace calls to this function with `import()` when supported
const modularVersion = !('svgEditor' in window) ||
!window.svgEditor ||
window.svgEditor.modules !== false;
if (modularVersion) {
return importModule(url, undefined, {returnDefault});
}
await importScript(url);
return window[glob];
}
/**
*
* @author Brett Zamir (other items are from `dynamic-import-polyfill`)
* @param {string|string[]} url
* @param {PlainObject} [atts={}]
* @returns {Promise<void|Error>} Resolves to `undefined` or rejects with an `Error` upon a
* script loading error
*/
export function importScript (url, atts = {}) {
if (Array.isArray(url)) {
return Promise.all(url.map((u) => {
return importScript(u, atts);
}));
}
return new Promise((resolve, reject) => { // eslint-disable-line promise/avoid-new
const script = document.createElement('script');
/**
*
* @returns {void}
*/
function scriptOnError () {
reject(new Error(`Failed to import: ${url}`));
destructor();
}
/**
*
* @returns {void}
*/
function scriptOnLoad () {
resolve();
destructor();
}
const destructor = () => {
script.removeEventListener('error', scriptOnError);
script.removeEventListener('load', scriptOnLoad);
script.remove();
script.src = '';
};
script.defer = 'defer';
addScriptAtts(script, atts);
script.addEventListener('error', scriptOnError);
script.addEventListener('load', scriptOnLoad);
script.src = url;
document.head.append(script);
});
}
/**
*
* @param {string|string[]} url
* @param {PlainObject} [atts={}]
* @param {PlainObject} opts
* @param {boolean} [opts.returnDefault=false} = {}]
* @returns {Promise<any>} Resolves to value of loading module or rejects with
* `Error` upon a script loading error.
*/
export function importModule (url, atts = {}, {returnDefault = false} = {}) {
if (Array.isArray(url)) {
return Promise.all(url.map((u) => {
return importModule(u, atts);
}));
}
return new Promise((resolve, reject) => { // eslint-disable-line promise/avoid-new
const vector = '$importModule$' + Math.random().toString(32).slice(2);
const script = document.createElement('script');
/**
*
* @returns {void}
*/
function scriptOnError () {
reject(new Error(`Failed to import: ${url}`));
destructor();
}
/**
*
* @returns {void}
*/
function scriptOnLoad () {
resolve(window[vector]);
destructor();
}
const destructor = () => {
delete window[vector];
script.removeEventListener('error', scriptOnError);
script.removeEventListener('load', scriptOnLoad);
script.remove();
URL.revokeObjectURL(script.src);
script.src = '';
};
addScriptAtts(script, atts);
script.defer = 'defer';
script.type = 'module';
script.addEventListener('error', scriptOnError);
script.addEventListener('load', scriptOnLoad);
const absURL = toAbsoluteURL(url);
const loader = `import * as m from '${
absURL.replace(/'/g, "\\'")
}'; window.${vector} = ${
returnDefault ? 'm.default || ' : ''
}m;`; // export Module
const blob = new Blob([loader], {type: 'text/javascript'});
script.src = URL.createObjectURL(blob);
document.head.append(script);
});
}
export default importModule;