1
0
Fork 0

update after fixing some pretokeniser issues

master
Gordon Williams 2020-04-27 17:00:54 +01:00
parent c065f3aa43
commit 44054aaf9e
2 changed files with 149 additions and 77 deletions

View File

@ -14,7 +14,7 @@ var AppInfo = {
if (storageFile.url.endsWith(".js") && !storageFile.url.endsWith(".min.js")) { // if original file ends in '.js'...
return Espruino.transform(content, {
SET_TIME_ON_WRITE : false,
//PRETOKENISE : true, // pretokenise still has an issue with number passing
PRETOKENISE : true,
MINIFICATION_LEVEL : "ESPRIMA",
builtinModules : "Flash,Storage,heatshrink,tensorflow,locale"
});

View File

@ -1,4 +1,5 @@
// EspruinoTools (https://github.com/espruino/EspruinoTools)
// EspruinoTools bundle (https://github.com/espruino/EspruinoTools)
// Cteated with https://github.com/espruino/EspruinoWebIDE/blob/gh-pages/extras/create_espruinotools_js.sh
if (typeof $ === "undefined") {
var jqReady = [];
@ -4141,10 +4142,22 @@ Object.defineProperty(exports, '__esModule', { value: true });
} while (isIn(chAlphaNum,ch));
} else if (isIn(chNum,ch)) { // NUMBER
type = "NUMBER";
do {
var chRange = chNum;
if (ch=="0") { // Handle
s+=ch;
nextCh();
} while (isIn(chNum,ch) || ch==".")
if ("xXoObB".indexOf(ch)>=0) {
if (ch=="b" || ch=="B") chRange="01";
if (ch=="o" || ch=="O") chRange="01234567";
if (ch=="x" || ch=="X") chRange="0123456789ABCDEFabcdef";
s+=ch;
nextCh();
}
}
while (isIn(chRange,ch) || ch==".") {
s+=ch;
nextCh();
}
} else if (isIn(chQuotes,ch)) { // STRING
type = "STRING";
var q = ch;
@ -4514,8 +4527,8 @@ Object.defineProperty(exports, '__esModule', { value: true });
fileLoader.click();
}
// Save a file with a save file dialog
function fileSaveDialog(data, filename) {
/* Save a file with a save file dialog. callback(savedFileName) only called in chrome app case when we knopw the filename*/
function fileSaveDialog(data, filename, callback) {
function errorHandler() {
Espruino.Core.Notifications.error("Error Saving", true);
}
@ -4531,6 +4544,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
writer.onwriteend = function(e) {
writer.onwriteend = function(e) {
console.log('FileWriter: complete');
if (callback) callback(writableFileEntry.name);
};
console.log('FileWriter: writing');
writer.write(blob);
@ -5614,6 +5628,9 @@ To add a new serial device, you must add an object to
// When code is sent to Espruino, search it for modules and add extra code required to load them
Espruino.addProcessor("transformForEspruino", function(code, callback) {
if (Espruino.Config.ROLLUP) {
return loadModulesRollup(code, callback);
}
loadModules(code, callback);
});
@ -5790,8 +5807,24 @@ To add a new serial device, you must add an object to
callback(loadedModuleData.join("\n") + "\n" + code);
});
}
};
}
function loadModulesRollup(code, callback) {
rollupTools.loadModulesRollup(code)
.then(generated => {
const minified = generated.code;
console.log('rollup: '+minified.length+' bytes');
// FIXME: needs warnings?
Espruino.Core.Notifications.info('Rollup no errors. Bundling ' + code.length + ' bytes to ' + minified.length + ' bytes');
callback(minified);
})
.catch(err => {
console.log('rollup:error', err);
Espruino.Core.Notifications.error("Rollup errors - Bundling failed: " + String(err).trim());
callback(code);
});
}
Espruino.Core.Modules = {
init : init
@ -32702,6 +32735,7 @@ global.esmangle = require('../lib/esmangle');
name : "Minification",
description : "Automatically minify code from the Editor window?",
type : { "":"No Minification",
"TERSER":"Terser (uglify-es)",
"ESPRIMA":"Esprima (offline)",
"WHITESPACE_ONLY":"Closure (online) - Whitespace Only",
"SIMPLE_OPTIMIZATIONS":"Closure (online) - Simple Optimizations",
@ -32713,6 +32747,7 @@ global.esmangle = require('../lib/esmangle');
name : "Module Minification",
description : "Automatically minify modules? Only modules with a .js extension will be minified - if a file with a .min.js extension exists then it will be used instead.",
type : { "":"No Minification",
"TERSER":"Terser (uglify-es)",
"ESPRIMA":"Esprima (offline)",
"WHITESPACE_ONLY":"Closure (online) - Whitespace Only",
"SIMPLE_OPTIMIZATIONS":"Closure (online) - Simple Optimizations",
@ -32720,6 +32755,20 @@ global.esmangle = require('../lib/esmangle');
defaultValue : "ESPRIMA"
});
Espruino.Core.Config.add("ROLLUP",{
section : "Minification",
name : "Rollup: Enable Rollup",
description : "Use rollup.js along with rollup-plugin-espruino-modules for bundling",
type : "boolean",
defaultValue : false
});
Espruino.Core.Config.add("MODULE_MERGE",{
section : "Minification",
name : "Rollup: Unwrap modules using rollup.js",
description : "Uses rollup.js to merge modules and remove unused code",
type : "boolean",
defaultValue : true
});
Espruino.Core.Config.add("MINIFICATION_Mangle",{
section : "Minification",
name : "Esprima: Mangle",
@ -32856,7 +32905,28 @@ global.esmangle = require('../lib/esmangle');
}
}
function minifyCodeTerser(code, callback, description){
rollupTools.minifyCodeTerser(code)
.then(generated => {
var minified = generated.code;
// FIXME: needs warnings?
Espruino.Core.Notifications.info('Terser no errors'+description+'. Minifying ' + code.length + ' bytes to ' + minified.length + ' bytes');
callback(minified);
})
.catch(err => {
Espruino.Core.Notifications.warning("Terser errors"+description+" - sending unminified code.");
Espruino.Core.Notifications.error(String(err).trim());
callback(code);
});
}
function minify(code, callback, level, isModule, description) {
if (Espruino.Config.ROLLUP) {
// already minified by the ROLLUP pipeline
return callback(code);
}
(function() {
Espruino.Core.Status.setStatus("Minifying"+(isModule?description.substr(2):""));
var _callback = callback;
@ -32865,6 +32935,7 @@ global.esmangle = require('../lib/esmangle');
_callback(code);
};
})();
var minifyCode = code;
var minifyCallback = callback;
if (isModule) {
@ -32883,6 +32954,7 @@ global.esmangle = require('../lib/esmangle');
case "SIMPLE_OPTIMIZATIONS":
case "ADVANCED_OPTIMIZATIONS": minifyCodeGoogle(code, callback, level, description); break;
case "ESPRIMA": minifyCodeEsprima(code, callback, description); break;
case "TERSER": minifyCodeTerser(code, callback, description); break;
default: callback(code); break;
}
}
@ -33011,16 +33083,16 @@ global.esmangle = require('../lib/esmangle');
//console.log("prev "+JSON.stringify(previousString)+" next "+tokenString);
if (tok.str=="(" || tok.str=="{" || tok.str=="[") brackets++;
if (tok.str==")" || tok.str=="}" || tok.str=="]") brackets--;
// TODO: check for eg. two IDs/similar which can't be merged without a space
// preserve newlines at root scope to avoid us filling up the command buffer all at once
if (brackets==0 && previousString.indexOf("\n")>=0)
resultCode += "\n";
if (tok.str==")" || tok.str=="}" || tok.str=="]") brackets--;
// if we have a token for something, use that - else use the string
if (tokenId) {
//console.log(JSON.stringify(tok.str)+" => "+tokenId);
resultCode += String.fromCharCode(tokenId);
tok.type = "TOKENISED";
} else {
if ((tok.type=="ID" || tok.type=="NUMBER") &&
(lastTok.type=="ID" || lastTok.type=="NUMBER"))