Some improvements to the edit-example script

This commit is contained in:
Maximilian Krög
2022-08-13 16:35:55 +02:00
parent cf42592f78
commit 5fb74d21e5
2 changed files with 21 additions and 55 deletions

View File

@@ -1,4 +1,6 @@
(function() {
"use strict"
/* global LZString */
function compress(json) {
return LZString.compressToBase64(JSON.stringify(json))
@@ -43,55 +45,34 @@
const pkgJson = document.getElementById('example-pkg-source').innerText;
const unique = new Set();
const localResources = (js.match(/'(\.\/)?data\/[^']*/g) || [])
.concat(js.match(/'(\.\/)?resources\/[^']*/g) || [])
.map(
function (f) {
return f.replace(/^'(\.\/)?/, '');
}
)
.filter(
function (f) {
return unique.has(f) ? false : (unique.add(f) || unique);
}
);
const localResources = (js.match(/'(?:\.\/)?(?:data|resources)\/[^']*'/g) || [])
.map(function (f) {
return f.replace(/^'(?:\.\/)?|'$/g, '');
})
.filter(function (f) {
return unique.has(f) ? false : unique.add(f);
});
const promises = localResources.map(
function (resource) {
return fetchResource(resource);
}
);
const promises = localResources.map(function (resource) {
return fetchResource(resource);
});
Promise.all(promises).then(
function (results) {
const files = {
'index.html': {
content: html
},
'main.js': {
content: js
},
'package.json': {
content: pkgJson
},
'sandbox.config.json': {
content: '{"template": "parcel"}'
}
'index.html': {content: html},
'main.js': {content: js},
'package.json': {content: pkgJson},
'sandbox.config.json': {content: '{"template": "parcel"}'}
};
if (worker) {
files['worker.js'] = {
content: worker
}
files['worker.js'] = {content: worker}
}
const data = {
files: files
};
for (let i = 0; i < localResources.length; i++) {
data.files[localResources[i]] = results[i];
files[localResources[i]] = results[i];
}
form.parameters.value = compress(data);
form.parameters.value = compress({files: files});
form.submit();
}
);