cleaning up the code a bit before i submit the pull request

This commit is contained in:
Brian Mulhall
2020-03-16 11:38:37 -05:00
parent cc0d399169
commit d4b189f80b
3 changed files with 72 additions and 68 deletions

View File

@@ -25,11 +25,13 @@ meteorJsMinify = function (source) {
if (typeof terserResult.code === "string") {
result.code = terserResult.code;
result.minifier = 'terser';
} else {
}
else {
throw terserResult.error || new Error("unknown terser.minify failure");
}
} catch (err) {
}
catch (err) {
// Although Babel.minify can handle a wider variety of ECMAScript
// 2015+ syntax, it is substantially slower than terser, so
// we use it only as a fallback.
@@ -37,7 +39,9 @@ meteorJsMinify = function (source) {
inlineNodeEnv: NODE_ENV
});
result.code = Babel.minify(source, options).code;
const babelResult = Babel.minify(source, options);
result.code = babelResult.code;
result.minifier = 'babel-minify';
}

View File

@@ -15,7 +15,6 @@ Tinytest.add('minifier-js - verify simple JS minifications work', (test) => {
result = meteorJsMinify('class Person{ constructor(name, age){ this.name = name; this.age = age; } printName(){console.log(this.name)}}\n');
test.equal(result.code, 'class Person{constructor(s,e){this.name=s,this.age=e}printName(){console.log(this.name)}}');
});
@@ -30,12 +29,10 @@ Tinytest.add('minifier-js - verify properties setting', (test) => {
Tinytest.add('minifier-js - verify evaluate setting', (test) => {
let result = meteorJsMinify('let a = 10 + 20 + 30;\n');
test.equal(result.code, 'let a=60;');
});
// this test is an evaluation, but since unsafe is false it won't get evaluated
Tinytest.add('minifier-js - verify that an unsafe evaluation will fail', (test) => {
let result = meteorJsMinify('var a = [ "foo", "bar", "baz" ].join("");\n');
test.equal(result.code, 'var a=["foo","bar","baz"].join("");');
});

View File

@@ -80,90 +80,93 @@ class MeteorBabelMinifier {
if (files.length) {
files[0].addJavaScript(toBeAdded);
}
}
maybeThrowMinifyErrorBySourceFile(error, file) {
var minifierErrorRegex = /^(.*?)\s?\((\d+):(\d+)\)$/;
var parseError = minifierErrorRegex.exec(error.message);
// nested function
maybeThrowMinifyErrorBySourceFile(error, file) {
var minifierErrorRegex = /^(.*?)\s?\((\d+):(\d+)\)$/;
var parseError = minifierErrorRegex.exec(error.message);
if (!parseError) {
// If we were unable to parse it, just let the usual error handling work.
return;
}
if (!parseError) {
// If we were unable to parse it, just let the usual error handling work.
return;
}
var lineErrorMessage = parseError[1];
var lineErrorLineNumber = parseError[2];
var lineErrorMessage = parseError[1];
var lineErrorLineNumber = parseError[2];
var parseErrorContentIndex = lineErrorLineNumber - 1;
var parseErrorContentIndex = lineErrorLineNumber - 1;
// Unlikely, since we have a multi-line fixed header in this file.
if (parseErrorContentIndex < 0) {
return;
}
// Unlikely, since we have a multi-line fixed header in this file.
if (parseErrorContentIndex < 0) {
return;
}
/*
/*
What we're parsing looks like this:
What we're parsing looks like this:
/////////////////////////////////////////
// //
// path/to/file.js //
// //
/////////////////////////////////////////
// 1
var illegalECMAScript = true; // 2
// 3
/////////////////////////////////////////
/////////////////////////////////////////
// //
// path/to/file.js //
// //
/////////////////////////////////////////
// 1
var illegalECMAScript = true; // 2
// 3
/////////////////////////////////////////
Btw, the above code is intentionally not newer ECMAScript so
we don't break ourselves.
Btw, the above code is intentionally not newer ECMAScript so
we don't break ourselves.
*/
*/
var contents = file.getContentsAsString().split(/\n/);
var lineContent = contents[parseErrorContentIndex];
var contents = file.getContentsAsString().split(/\n/);
var lineContent = contents[parseErrorContentIndex];
// Try to grab the line number, which sometimes doesn't exist on
// line, abnormally-long lines in a larger block.
var lineSrcLineParts = /^(.*?)(?:\s*\/\/ (\d+))?$/.exec(lineContent);
// Try to grab the line number, which sometimes doesn't exist on
// line, abnormally-long lines in a larger block.
var lineSrcLineParts = /^(.*?)(?:\s*\/\/ (\d+))?$/.exec(lineContent);
// The line didn't match at all? Let's just not try.
if (!lineSrcLineParts) {
return;
}
// The line didn't match at all? Let's just not try.
if (!lineSrcLineParts) {
return;
}
var lineSrcLineContent = lineSrcLineParts[1];
var lineSrcLineNumber = lineSrcLineParts[2];
var lineSrcLineContent = lineSrcLineParts[1];
var lineSrcLineNumber = lineSrcLineParts[2];
// Count backward from the failed line to find the filename.
for (var c = parseErrorContentIndex - 1; c >= 0; c--) {
var sourceLine = contents[c];
// Count backward from the failed line to find the filename.
for (var c = parseErrorContentIndex - 1; c >= 0; c--) {
var sourceLine = contents[c];
// If the line is a boatload of slashes, we're in the right place.
if (/^\/\/\/{6,}$/.test(sourceLine)) {
// If the line is a boatload of slashes, we're in the right place.
if (/^\/\/\/{6,}$/.test(sourceLine)) {
// If 4 lines back is the same exact line, we've found the framing.
if (contents[c - 4] === sourceLine) {
// If 4 lines back is the same exact line, we've found the framing.
if (contents[c - 4] === sourceLine) {
// So in that case, 2 lines back is the file path.
var parseErrorPath = contents[c - 2]
.substring(3)
.replace(/\s+\/\//, "");
// So in that case, 2 lines back is the file path.
var parseErrorPath = contents[c - 2]
.substring(3)
.replace(/\s+\/\//, "");
var minError = new Error(
"babel-minify minification error " +
"within " + file.getPathInBundle() + ":\n" +
parseErrorPath +
(lineSrcLineNumber ? ", line " + lineSrcLineNumber : "") + "\n" +
"\n" +
lineErrorMessage + ":\n" +
"\n" +
lineSrcLineContent + "\n"
);
var minError = new Error(
"babel-minify minification error " +
"within " + file.getPathInBundle() + ":\n" +
parseErrorPath +
(lineSrcLineNumber ? ", line " + lineSrcLineNumber : "") + "\n" +
"\n" +
lineErrorMessage + ":\n" +
"\n" +
lineSrcLineContent + "\n"
);
throw minError;
throw minError;
}
}
}
}
}
}