1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2024-10-27 20:34:29 +00:00

Fix that whole export debacle (#1370)

* Re-add setting exports

* Update webpack.production.config.js

* Update mod.js

* Slight change

* Update mod.js

* Update webpack.production.config.js

* Update webpack.config.js
This commit is contained in:
Bagel03 2022-02-13 15:09:41 -05:00 committed by GitHub
parent dab4aa9cda
commit f534a88f80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 4 deletions

View File

@ -1,3 +1,39 @@
module.exports = function (source, map) { const oneExport = exp => {
return source + `\nexport let $s=(n,v)=>eval(n+"=v")`; return `${exp}=v`; // No checks needed
};
const twoExports = (exp1, exp2) => {
return `n=="${exp1}"?${exp1}=v:${exp2}=v`;
};
const multiExports = exps => {
exps = exps.map(exp => `case "${exp}":${exp}=v;break;`);
return `switch(n){${exps.toString().replaceAll(";,", ";")} }`;
};
const defineFnBody = source => {
const regex = /export (?:let|class) (?<name>\w+)/g;
let names = [...source.matchAll(regex)].map(n => n.groups.name);
switch (names.length) {
case 0:
return false;
case 1:
return oneExport(names[0]);
case 2:
return twoExports(names[0], names[1]);
default:
return multiExports(names);
}
};
/**
*
* @param {string} source
* @param {*} map
* @returns
*/
module.exports = function (source, map) {
const body = defineFnBody(source);
if (!body) return source;
return source + `\nexport const __$S__=(n,v)=>{${body}}`;
}; };

View File

@ -93,6 +93,10 @@ module.exports = ({ watch = false, standalone = false, chineseVersion = false, w
end: "typehints:end", end: "typehints:end",
}, },
}, },
{
loader: path.resolve(__dirname, "mod.js"),
},
],
], ],
}, },
{ {

View File

@ -131,6 +131,7 @@ module.exports = ({
warnings: true, warnings: true,
}, },
mangle: { mangle: {
reserved: ["__$S__"],
eval: true, eval: true,
keep_classnames: !minifyNames, keep_classnames: !minifyNames,
keep_fnames: !minifyNames, keep_fnames: !minifyNames,
@ -210,6 +211,9 @@ module.exports = ({
test: /\.js$/, test: /\.js$/,
use: [ use: [
// "thread-loader", // "thread-loader",
{
loader: path.resolve(__dirname, "mod.js"),
},
{ {
loader: "babel-loader?cacheDirectory", loader: "babel-loader?cacheDirectory",
options: { options: {

View File

@ -112,7 +112,8 @@ export class ModLoader {
// @ts-ignore // @ts-ignore
const module = modules(key); const module = modules(key);
for (const member in module) { for (const member in module) {
if (member === "default") { if (member === "default" || member === "__$S__") {
// Setter
continue; continue;
} }
if (exports[member]) { if (exports[member]) {
@ -124,7 +125,7 @@ export class ModLoader {
return module[member]; return module[member];
}, },
set(v) { set(v) {
throw new Error("Overriding the shapez exports is currently not possible"); module.__$S__(member, v);
}, },
}); });
} }