diff --git a/gulp/mod.js b/gulp/mod.js index 87912bb3..ad6cd4ae 100644 --- a/gulp/mod.js +++ b/gulp/mod.js @@ -1,3 +1,39 @@ +const oneExport = exp => { + 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) (?\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) { - return source + `\nexport let $s=(n,v)=>eval(n+"=v")`; + const body = defineFnBody(source); + if (!body) return source; + return source + `\nexport const __$S__=(n,v)=>{${body}}`; }; diff --git a/gulp/webpack.config.js b/gulp/webpack.config.js index 7db0bf0b..c696d2bd 100644 --- a/gulp/webpack.config.js +++ b/gulp/webpack.config.js @@ -93,6 +93,10 @@ module.exports = ({ watch = false, standalone = false, chineseVersion = false, w end: "typehints:end", }, }, + { + loader: path.resolve(__dirname, "mod.js"), + }, + ], ], }, { diff --git a/gulp/webpack.production.config.js b/gulp/webpack.production.config.js index fdd477b9..f56ae609 100644 --- a/gulp/webpack.production.config.js +++ b/gulp/webpack.production.config.js @@ -131,6 +131,7 @@ module.exports = ({ warnings: true, }, mangle: { + reserved: ["__$S__"], eval: true, keep_classnames: !minifyNames, keep_fnames: !minifyNames, @@ -210,6 +211,9 @@ module.exports = ({ test: /\.js$/, use: [ // "thread-loader", + { + loader: path.resolve(__dirname, "mod.js"), + }, { loader: "babel-loader?cacheDirectory", options: { diff --git a/src/js/mods/modloader.js b/src/js/mods/modloader.js index daf0766e..3d0985d5 100644 --- a/src/js/mods/modloader.js +++ b/src/js/mods/modloader.js @@ -112,7 +112,8 @@ export class ModLoader { // @ts-ignore const module = modules(key); for (const member in module) { - if (member === "default") { + if (member === "default" || member === "__$S__") { + // Setter continue; } if (exports[member]) { @@ -124,7 +125,7 @@ export class ModLoader { return module[member]; }, set(v) { - throw new Error("Overriding the shapez exports is currently not possible"); + module.__$S__(member, v); }, }); }