refactor(vite): use turbo-polyfills

This commit is contained in:
Paul Chavard 2022-07-19 13:06:45 +02:00
parent 35e3dcd848
commit c2091e0978
8 changed files with 7 additions and 376 deletions

View file

@ -1,4 +1,3 @@
import '../shared/polyfills';
import Rails from '@rails/ujs';
import * as ActiveStorage from '@rails/activestorage';
import * as Turbo from '@hotwired/turbo';

View file

@ -1,39 +0,0 @@
import './polyfills/dataset';
// IE 11 has no baseURI (required by turbo)
if (document.baseURI == undefined) {
document.baseURI = document.URL;
}
// IE 11 has no children on DocumentFragment (required by turbo)
function polyfillChildren(proto) {
Object.defineProperty(proto, 'children', {
get: function () {
const children = [];
for (const node of this.childNodes) {
if (node.nodeType == 1) {
children.push(node);
}
}
return children;
}
});
}
const fragment = document.createDocumentFragment();
if (fragment.children == undefined) {
polyfillChildren(DocumentFragment.prototype);
}
// IE 11 has no isConnected on Node
function polyfillIsConnected(proto) {
Object.defineProperty(proto, 'isConnected', {
get: function () {
return document.documentElement.contains(this);
}
});
}
if (!('isConnected' in Node.prototype)) {
polyfillIsConnected(Node.prototype);
}

View file

@ -14,16 +14,11 @@
"@sentry/browser": "6.12.0",
"@stimulus/polyfills": "^2.0.0",
"@tmcw/togeojson": "^4.3.0",
"@webcomponents/custom-elements": "^1.5.0",
"@webcomponents/template": "^1.5.0",
"chartkick": "^4.2.0",
"core-js": "^3.23.3",
"custom-event-polyfill": "^1.0.7",
"debounce": "^1.2.1",
"dom4": "^2.1.6",
"email-butler": "^1.0.13",
"event-target-polyfill": "^0.0.3",
"formdata-polyfill": "^4.0.10",
"geojson": "^0.5.0",
"highcharts": "^10.1.0",
"intersection-observer": "^0.12.2",
@ -41,6 +36,7 @@
"stimulus-use": "^0.50.0",
"tiny-invariant": "^1.2.0",
"trix": "^1.2.3",
"turbo-polyfills": "^0.3.0",
"whatwg-fetch": "^3.0.0",
"yet-another-abortcontroller-polyfill": "^0.0.4",
"zod": "^3.17.3"

View file

@ -1,13 +0,0 @@
diff --git a/node_modules/@webcomponents/template/template.js b/node_modules/@webcomponents/template/template.js
index cebfac4..96e03e7 100644
--- a/node_modules/@webcomponents/template/template.js
+++ b/node_modules/@webcomponents/template/template.js
@@ -323,7 +323,7 @@
var defineOuterHTML = function defineOuterHTML(obj) {
Object.defineProperty(obj, 'outerHTML', {
get: function () {
- return `<${TEMPLATE_TAG}>${this.innerHTML}</${TEMPLATE_TAG}>`;
+ return "<" + TEMPLATE_TAG +">" + this.innerHTML + "</" + TEMPLATE_TAG + ">";
},
set: function (innerHTML) {
if (this.parentNode) {

View file

@ -1,208 +0,0 @@
diff --git a/node_modules/event-target-polyfill/index.js b/node_modules/event-target-polyfill/index.js
index 82f7f42..8e29c96 100644
--- a/node_modules/event-target-polyfill/index.js
+++ b/node_modules/event-target-polyfill/index.js
@@ -1,7 +1,20 @@
-const root =
- (typeof globalThis !== "undefined" && globalThis) ||
- (typeof self !== "undefined" && self) ||
- (typeof global !== "undefined" && global);
+"use strict";
+
+function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
+
+function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
+
+function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
+
+function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
+
+function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e2) { throw _e2; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e3) { didErr = true; err = _e3; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
+
+function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
+
+function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
+
+var root = typeof globalThis !== "undefined" && globalThis || typeof self !== "undefined" && self || typeof global !== "undefined" && global;
function isConstructor(fn) {
try {
@@ -9,66 +22,49 @@ function isConstructor(fn) {
} catch (error) {
return false;
}
- return true;
-}
-
-if (typeof root.Event !== "function" || !isConstructor(root.Event)) {
- root.Event = (function () {
- function Event(type, options) {
- this.bubbles = !!options && !!options.bubbles;
- this.cancelable = !!options && !!options.cancelable;
- this.composed = !!options && !!options.composed;
- this.type = type;
- }
- return Event;
- })();
+ return true;
}
if (typeof root.EventTarget === "undefined" || !isConstructor(root.Event)) {
- root.EventTarget = (function () {
+ root.EventTarget = function () {
function EventTarget() {
this.__listeners = new Map();
}
EventTarget.prototype = Object.create(Object.prototype);
- EventTarget.prototype.addEventListener = function (
- type,
- listener,
- options
- ) {
+ EventTarget.prototype.addEventListener = function (type, listener, options) {
if (arguments.length < 2) {
- throw new TypeError(
- `TypeError: Failed to execute 'addEventListener' on 'EventTarget': 2 arguments required, but only ${arguments.length} present.`
- );
+ throw new TypeError("TypeError: Failed to execute 'addEventListener' on 'EventTarget': 2 arguments required, but only ".concat(arguments.length, " present."));
}
- const __listeners = this.__listeners;
- const actualType = type.toString();
+
+ var __listeners = this.__listeners;
+ var actualType = type.toString();
+
if (!__listeners.has(actualType)) {
__listeners.set(actualType, new Map());
}
- const listenersForType = __listeners.get(actualType);
+
+ var listenersForType = __listeners.get(actualType);
+
if (!listenersForType.has(listener)) {
// Any given listener is only registered once
listenersForType.set(listener, options);
}
};
- EventTarget.prototype.removeEventListener = function (
- type,
- listener,
- _options
- ) {
+ EventTarget.prototype.removeEventListener = function (type, listener, _options) {
if (arguments.length < 2) {
- throw new TypeError(
- `TypeError: Failed to execute 'addEventListener' on 'EventTarget': 2 arguments required, but only ${arguments.length} present.`
- );
+ throw new TypeError("TypeError: Failed to execute 'addEventListener' on 'EventTarget': 2 arguments required, but only ".concat(arguments.length, " present."));
}
- const __listeners = this.__listeners;
- const actualType = type.toString();
+
+ var __listeners = this.__listeners;
+ var actualType = type.toString();
+
if (__listeners.has(actualType)) {
- const listenersForType = __listeners.get(actualType);
+ var listenersForType = __listeners.get(actualType);
+
if (listenersForType.has(listener)) {
listenersForType.delete(listener);
}
@@ -77,45 +73,61 @@ if (typeof root.EventTarget === "undefined" || !isConstructor(root.Event)) {
EventTarget.prototype.dispatchEvent = function (event) {
if (!(event instanceof Event)) {
- throw new TypeError(
- `Failed to execute 'dispatchEvent' on 'EventTarget': parameter 1 is not of type 'Event'.`
- );
+ throw new TypeError("Failed to execute 'dispatchEvent' on 'EventTarget': parameter 1 is not of type 'Event'.");
}
- const type = event.type;
- const __listeners = this.__listeners;
- const listenersForType = __listeners.get(type);
+
+ var type = event.type;
+ var __listeners = this.__listeners;
+
+ var listenersForType = __listeners.get(type);
+
if (listenersForType) {
- for (const [listener, options] of listenersForType.entries()) {
- try {
- if (typeof listener === "function") {
- // Listener functions must be executed with the EventTarget as the `this` context.
- listener.call(this, event);
- } else if (listener && typeof listener.handleEvent === "function") {
- // Listener objects have their handleEvent method called, if they have one
- listener.handleEvent(event);
+ var _iterator = _createForOfIteratorHelper(listenersForType.entries()),
+ _step;
+
+ try {
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
+ var _step$value = _slicedToArray(_step.value, 2),
+ listener = _step$value[0],
+ options = _step$value[1];
+
+ try {
+ if (typeof listener === "function") {
+ // Listener functions must be executed with the EventTarget as the `this` context.
+ listener.call(this, event);
+ } else if (listener && typeof listener.handleEvent === "function") {
+ // Listener objects have their handleEvent method called, if they have one
+ listener.handleEvent(event);
+ }
+ } catch (err) {
+ // We need to report the error to the global error handling event,
+ // but we do not want to break the loop that is executing the events.
+ // Unfortunately, this is the best we can do, which isn't great, because the
+ // native EventTarget will actually do this synchronously before moving to the next
+ // event in the loop.
+ setTimeout(function () {
+ throw err;
+ });
+ }
+
+ if (options && options.once) {
+ // If this was registered with { once: true }, we need
+ // to remove it now.
+ listenersForType.delete(listener);
}
- } catch (err) {
- // We need to report the error to the global error handling event,
- // but we do not want to break the loop that is executing the events.
- // Unfortunately, this is the best we can do, which isn't great, because the
- // native EventTarget will actually do this synchronously before moving to the next
- // event in the loop.
- setTimeout(() => {
- throw err;
- });
- }
- if (options && options.once) {
- // If this was registered with { once: true }, we need
- // to remove it now.
- listenersForType.delete(listener);
}
+ } catch (err) {
+ _iterator.e(err);
+ } finally {
+ _iterator.f();
}
- }
- // Since there are no cancellable events on a base EventTarget,
+ } // Since there are no cancellable events on a base EventTarget,
// this should always return true.
+
+
return true;
};
return EventTarget;
- })();
+ }();
}

View file

@ -1,61 +0,0 @@
diff --git a/node_modules/formdata-polyfill/FormData.js b/node_modules/formdata-polyfill/FormData.js
index 8e73660..133dcd0 100644
--- a/node_modules/formdata-polyfill/FormData.js
+++ b/node_modules/formdata-polyfill/FormData.js
@@ -71,7 +71,7 @@ if (typeof Blob !== 'undefined' && (typeof FormData === 'undefined' || !FormData
function ensureArgs (args, expected) {
if (args.length < expected) {
- throw new TypeError(`${expected} argument required, but only ${args.length} present.`)
+ throw new TypeError("".concat(expected, " argument required, but only ").concat(args.length, " present."))
}
}
@@ -342,11 +342,11 @@ if (typeof Blob !== 'undefined' && (typeof FormData === 'undefined' || !FormData
['_blob'] () {
const boundary = '----formdata-polyfill-' + Math.random(),
chunks = [],
- p = `--${boundary}\r\nContent-Disposition: form-data; name="`
- this.forEach((value, name) => typeof value == 'string'
- ? chunks.push(p + escape(normalizeLinefeeds(name)) + `"\r\n\r\n${normalizeLinefeeds(value)}\r\n`)
- : chunks.push(p + escape(normalizeLinefeeds(name)) + `"; filename="${escape(value.name)}"\r\nContent-Type: ${value.type||"application/octet-stream"}\r\n\r\n`, value, `\r\n`))
- chunks.push(`--${boundary}--`)
+ p = "--".concat(boundary, "\r\nContent-Disposition: form-data; name=\"");
+ this.forEach((value, name) => typeof value == 'string'
+ ? chunks.push(p + escape(normalizeLinefeeds(name)) + "\"\r\n\r\n".concat(normalizeLinefeeds(value), "\r\n"))
+ : chunks.push(p + escape(normalizeLinefeeds(name)) + "\"; filename=\"".concat(escape(value.name), "\"\r\nContent-Type: ").concat(value.type || "application/octet-stream", "\r\n\r\n"), value, "\r\n"))
+ chunks.push("--".concat(boundary, "--"));
return new Blob(chunks, {
type: "multipart/form-data; boundary=" + boundary
})
diff --git a/node_modules/formdata-polyfill/formdata-to-blob.js b/node_modules/formdata-polyfill/formdata-to-blob.js
index 5a0a517..05d1aca 100644
--- a/node_modules/formdata-polyfill/formdata-to-blob.js
+++ b/node_modules/formdata-polyfill/formdata-to-blob.js
@@ -16,22 +16,17 @@ const escape = (str, filename) =>
export function formDataToBlob (formData, BlobClass = Blob) {
const boundary = ('----formdata-polyfill-' + Math.random())
const chunks = []
- const prefix = `--${boundary}\r\nContent-Disposition: form-data; name="`
+ var prefix = "--".concat(boundary, "\r\nContent-Disposition: form-data; name=\"");
for (let [name, value] of formData) {
if (typeof value === 'string') {
- chunks.push(prefix + escape(name) + `"\r\n\r\n${value.replace(/\r(?!\n)|(?<!\r)\n/g, '\r\n')}\r\n`)
+ chunks.push(prefix + escape(name) + "\"\r\n\r\n".concat(value.replace(/\r(?!\n)|(?<!\r)\n/g, '\r\n'), "\r\n"));
} else {
- chunks.push(
- prefix + escape(name) + `"; filename="${escape(value.name, 1)}"\r\n` +
- `Content-Type: ${value.type || 'application/octet-stream'}\r\n\r\n`,
- value,
- '\r\n'
- )
+ chunks.push(prefix + escape(name) + "\"; filename=\"".concat(escape(value.name, 1), "\"\r\n") + "Content-Type: ".concat(value.type || 'application/octet-stream', "\r\n\r\n"), value, '\r\n');
}
}
- chunks.push(`--${boundary}--`)
+ chunks.push("--".concat(boundary, "--"));
return new BlobClass(chunks, {
type: 'multipart/form-data; boundary=' + boundary

View file

@ -30,10 +30,7 @@ if (shouldBuildLegacy()) {
'dom4',
'core-js/stable',
'@stimulus/polyfills',
'@webcomponents/custom-elements',
'@webcomponents/template',
'event-target-polyfill',
'formdata-polyfill',
'turbo-polyfills',
'intersection-observer',
'regenerator-runtime/runtime',
'whatwg-fetch',

View file

@ -946,16 +946,6 @@
react-refresh "^0.13.0"
resolve "^1.22.0"
"@webcomponents/custom-elements@^1.5.0":
version "1.5.0"
resolved "https://registry.yarnpkg.com/@webcomponents/custom-elements/-/custom-elements-1.5.0.tgz#7d07ff4979312dda167cc0a2b7586e76dc1cf6ab"
integrity sha512-c+7jPQCs9h/BYVcZ2Kna/3tsl3A/9EyXfvWjp5RiTDm1OpTcbZaCa1z4RNcTe/hUtXaqn64JjNW1yrWT+rZ8gg==
"@webcomponents/template@^1.5.0":
version "1.5.0"
resolved "https://registry.yarnpkg.com/@webcomponents/template/-/template-1.5.0.tgz#45c15735f1ea7ecdb2f2d9d35663ea1f169a1308"
integrity sha512-DPQgBAedzjsFD7rgv7b6OKmpHq5VTBUCLmYfDiov2FC2C79QGaz+4iNmlVAem5iSicvN8DWTwU1kZ48XYLtuqg==
"@yarnpkg/lockfile@^1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@yarnpkg/lockfile/-/lockfile-1.1.0.tgz#e77a97fbd345b76d83245edcd17d393b1b41fb31"
@ -1401,11 +1391,6 @@ csstype@^3.0.2:
resolved "https://registry.yarnpkg.com/csstype/-/csstype-3.0.10.tgz#2ad3a7bed70f35b965707c092e5f30b327c290e5"
integrity sha512-2u44ZG2OcNUO9HDp/Jl8C07x6pU/eTR3ncV91SiK3dhG9TWvRVsCoJw14Ckx5DgWkzGA3waZWO3d7pgqpUI/XA==
custom-event-polyfill@^1.0.7:
version "1.0.7"
resolved "https://registry.yarnpkg.com/custom-event-polyfill/-/custom-event-polyfill-1.0.7.tgz#9bc993ddda937c1a30ccd335614c6c58c4f87aee"
integrity sha512-TDDkd5DkaZxZFM8p+1I3yAlvM3rSr1wbrOliG4yJiwinMZN8z/iGL7BTlDkrJcYTmgUSb4ywVCc3ZaUtOtC76w==
dashdash@^1.12.0:
version "1.14.1"
resolved "https://registry.yarnpkg.com/dashdash/-/dashdash-1.14.1.tgz#853cfa0f7cbe2fed5de20326b8dd581035f6e2f0"
@ -1984,11 +1969,6 @@ esutils@^2.0.2:
resolved "https://registry.yarnpkg.com/esutils/-/esutils-2.0.3.tgz#74d2eb4de0b8da1293711910d50775b9b710ef64"
integrity sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g==
event-target-polyfill@^0.0.3:
version "0.0.3"
resolved "https://registry.yarnpkg.com/event-target-polyfill/-/event-target-polyfill-0.0.3.tgz#ed373295f3b257774b5d75afb2599331d9f3406c"
integrity sha512-ZMc6UuvmbinrCk4RzGyVmRyIsAyxMRlp4CqSrcQRO8Dy0A9ldbiRy5kdtBj4OtP7EClGdqGfIqo9JmOClMsGLQ==
eventlistener-polyfill@^1.0.5:
version "1.0.5"
resolved "https://registry.yarnpkg.com/eventlistener-polyfill/-/eventlistener-polyfill-1.0.5.tgz#d3c96e57d52ca4dc30b3b8f3cf9698dc8e277d8a"
@ -2058,14 +2038,6 @@ fastq@^1.6.0:
dependencies:
reusify "^1.0.4"
fetch-blob@^3.1.2:
version "3.1.5"
resolved "https://registry.yarnpkg.com/fetch-blob/-/fetch-blob-3.1.5.tgz#0077bf5f3fcdbd9d75a0b5362f77dbb743489863"
integrity sha512-N64ZpKqoLejlrwkIAnb9iLSA3Vx/kjgzpcDhygcqJ2KKjky8nCgUQ+dzXtbrLaWZGZNmNfQTsiQ0weZ1svglHg==
dependencies:
node-domexception "^1.0.0"
web-streams-polyfill "^3.0.3"
file-entry-cache@^6.0.1:
version "6.0.1"
resolved "https://registry.yarnpkg.com/file-entry-cache/-/file-entry-cache-6.0.1.tgz#211b2dd9659cb0394b073e7323ac3c933d522027"
@ -2131,13 +2103,6 @@ form-data@~2.3.2:
combined-stream "^1.0.6"
mime-types "^2.1.12"
formdata-polyfill@^4.0.10:
version "4.0.10"
resolved "https://registry.yarnpkg.com/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz#24807c31c9d402e002ab3d8c720144ceb8848423"
integrity sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==
dependencies:
fetch-blob "^3.1.2"
fs-extra@^0.30.0:
version "0.30.0"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-0.30.0.tgz#f233ffcc08d4da7d432daa449776989db1df93f0"
@ -3119,11 +3084,6 @@ nice-try@^1.0.4:
resolved "https://registry.yarnpkg.com/nice-try/-/nice-try-1.0.5.tgz#a3378a7696ce7d223e88fc9b764bd7ef1089e366"
integrity sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==
node-domexception@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/node-domexception/-/node-domexception-1.0.0.tgz#6888db46a1f71c0b76b3f7555016b63fe64766e5"
integrity sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==
node-releases@^2.0.5:
version "2.0.5"
resolved "https://registry.yarnpkg.com/node-releases/-/node-releases-2.0.5.tgz#280ed5bc3eba0d96ce44897d8aee478bfb3d9666"
@ -4095,6 +4055,11 @@ tunnel-agent@^0.6.0:
dependencies:
safe-buffer "^5.0.1"
turbo-polyfills@^0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/turbo-polyfills/-/turbo-polyfills-0.3.0.tgz#9ad0644baa86be8f9736e84751ef0b6c26fe6bd9"
integrity sha512-LUhSw0mGcrKGG4ixCt+XgEVvetvwTXoZ61RqnJytstP6rm1kMBaSYkqUL8LYMzXniQ56gomtvioT9PTm8UqGGg==
tweetnacl@^0.14.3, tweetnacl@~0.14.0:
version "0.14.5"
resolved "https://registry.yarnpkg.com/tweetnacl/-/tweetnacl-0.14.5.tgz#5ae68177f192d4456269d108afa93ff8743f4f64"
@ -4279,11 +4244,6 @@ warning@^4.0.2:
dependencies:
loose-envify "^1.0.0"
web-streams-polyfill@^3.0.3:
version "3.2.1"
resolved "https://registry.yarnpkg.com/web-streams-polyfill/-/web-streams-polyfill-3.2.1.tgz#71c2718c52b45fd49dbeee88634b3a60ceab42a6"
integrity sha512-e0MO3wdXWKrLbL0DgGnUV7WHVuw9OUvL4hjgnPkIeEvESk74gAITi5G606JtZPp39cd8HA9VQzCIvA49LpPN5Q==
webidl-conversions@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-7.0.0.tgz#256b4e1882be7debbf01d05f0aa2039778ea080a"