Skip to content

Commit

Permalink
Allow some minification through the source urls
Browse files Browse the repository at this point in the history
* **EXPERIMENTAL**  ES5 minification via url ... Uses same routes but `.min` prefix on extension. If ES6 is encountered it should just not minify.

**NOTES**
* ES6 minification isn't supported/stable yet but will probably request an experimental branch on *express-minify* or something else
* This is on a trial basis to see how much interest and possible clobbering happens... *uglifyjs2* claims stable support with ES5 ... this will test that. Authors should recommend debugging against original source but with a notation that minified may or may not work.
* OpenUserJS#819 followup with renaming external urls
* Debug mode of course won't use *express-minify* currently so nothing should happen on these routes.. this is being reevaluated.

* BUG fix for libraries sharing same S3 storage space when ending in a reserved extension from script name
* BUG fix for the regular expression allowing some urls without 404

Applies to OpenUserJS#432
  • Loading branch information
Martii committed Dec 23, 2015
1 parent b0a9c6b commit ba2ed1a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
9 changes: 9 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,15 @@ if (minify && !isDbg) {
app.use(minify());
}

app.use(function(aReq, aRes, aNext) {
if (/\.(user|meta)\.js$/.test(aReq.url)) {
aRes._uglifyOutput = {
comments: true
};
}
aNext();
});

app.use(lessMiddleware(__dirname + '/public', {
render: {
compress: false,
Expand Down
21 changes: 17 additions & 4 deletions controllers/scriptStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function getInstallNameBase(aReq, aOptions) {
var username = aReq.params.username;
var scriptname = aReq.params.scriptname;

var rKnownExtensions = /(?:\.(?:user|meta))?\.js(?:on)?$/;
var rKnownExtensions = /\.((min\.)?(user\.)?js|meta\.js(on)?)$/;

if (!aOptions) {
aOptions = {};
Expand Down Expand Up @@ -197,7 +197,6 @@ exports.getSource = function (aReq, aCallback) {
};

exports.sendScript = function (aReq, aRes, aNext) {

if (aReq.params.type === 'libs') {
aReq.params.isLib = true;
}
Expand All @@ -219,8 +218,10 @@ exports.sendScript = function (aReq, aRes, aNext) {
// Send the script
aRes.set('Content-Type', 'text/javascript; charset=UTF-8');

// Disable *express-minify* for this response
aRes._skip = true;
// Disable *express-minify* for responses that don't contain `.min.` extension
if (!/\.min\.user\.js$/.test(aReq.url)) {
aRes._skip = true;
}

aStream.setEncoding('utf8');
aStream.pipe(aRes);
Expand Down Expand Up @@ -484,6 +485,12 @@ exports.storeScript = function (aUser, aMeta, aBuf, aCallback, aUpdate) {
return;
}

// Can't install a userscript ending in a reserved extension
if (/\.min$/.test(scriptName)) {
aCallback(null);
return;
}

author = findMeta(aMeta, 'OpenUserJS.author.0.value');
collaborators = findMeta(aMeta, 'OpenUserJS.collaborator.value');

Expand Down Expand Up @@ -518,6 +525,12 @@ exports.storeScript = function (aUser, aMeta, aBuf, aCallback, aUpdate) {
return;
}

// Can't install a library ending in a reserved extension
if (/\.(min|user)$/.test(scriptName)) {
aCallback(null);
return;
}

installName += scriptName + '.js';
}

Expand Down
1 change: 1 addition & 0 deletions libs/modelParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ var parseScript = function (aScriptData) {
// Urls: Public
script.scriptPageUrl = getScriptPageUrl(script);
script.scriptInstallPageUrl = getScriptInstallPageUrl(script);
script.scriptInstallPageXUrl = script.scriptInstallPageUrl.replace(/(\.user)?\.js/, '');
script.scriptViewSourcePageUrl = getScriptViewSourcePageUrl(script);

// Urls: Issues
Expand Down
2 changes: 2 additions & 0 deletions views/includes/scriptPageHeader.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ <h2 class="page-heading">
<span class="sr-only">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu">
<li><a href="{{{script.scriptInstallPageXUrl}}}.min.user.js">&hellip; with minification <span class="label label-warning">EXPERIMENTAL</span></a></li>
<li role="separator" class="divider"></li>
<li><a href="/about/Userscript-Beginners-HOWTO"><em class="fa fa-fw fa-file-text-o"></em> Userscript Beginners HOWTO</a></li>
</ul>
</div>
Expand Down

0 comments on commit ba2ed1a

Please sign in to comment.