Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

better `require` method; `path` may have '.js' dropped; uglify engine

  • Loading branch information...
commit fc540f7614f55ab6e084b3b58d309fa3419a6512 1 parent 13a6d03
@andyedinborough authored
Showing with 64 additions and 14 deletions.
  1. +45 −0 Chirpy/Engines/UglifyEngine.cs
  2. +19 −14 Chirpy/Javascript/JavascriptRunner.cs
View
45 Chirpy/Engines/UglifyEngine.cs
@@ -0,0 +1,45 @@
+namespace Chirpy.Engines
+{
+ using System.Collections.Generic;
+ using System.ComponentModel.Composition;
+ using ChirpyInterface;
+
+ [Export(typeof (IEngine))]
+ [EngineMetadata("Uglify", "1.0", ".js", true)]
+ public class UglifyEngine : IEngine
+ {
+ [Import] public IJavascriptRunner JavascriptRunner { get; set; }
+
+
+ List<EngineResult> IEngine.Process(string contents, string filename)
+ {
+ var result = new EngineResult { FileName = filename, Extension = "min.js" };
+ var properties = new Dictionary<string, object>();
+ properties["js"] = contents;
+
+ const string script = @"
+var uglify = require('https://raw.github.com/mishoo/UglifyJS/master/uglify-js.js');
+var js = external.Get('js');
+var result = uglify(js);
+external.Set('result', result);
+";
+
+ try
+ {
+ var javascriptResult = JavascriptRunner.Execute(script, properties);
+ result.Contents = javascriptResult.Get("result") as string;
+ result.Exceptions.AddRange(javascriptResult.Messages);
+
+ } catch (ChirpyException cex) {
+ result.Exceptions.Add(cex);
+ }
+
+ return new List<EngineResult> { result };
+ }
+
+ public List<string> GetDependencies(string contents, string filename)
+ {
+ return null;
+ }
+ }
+}
View
33 Chirpy/Javascript/JavascriptRunner.cs
@@ -48,8 +48,6 @@ string GetHtml(string script)
</head>
<body>
<script>
- var module = window;
-
var onerror = window.onerror = function(msg, file, line){
external.LogError(msg, line, 0, file, null);
};
@@ -69,21 +67,28 @@ string GetHtml(string script)
};
var require = window.require = (function() {
- var required = {}, bases = [];
- return function (path, presource, postsource) {
- var key = external.GetFullUri(path, bases.length == 0 ? '' : bases[bases.length-1]);
+ var required = {};
+ return function _require(path, presource, postsource, root) {
+ if(!path) return undefined;
+ if(path.substr(path.lastIndexOf('.')+1).toLowerCase() !== 'js')
+ path += '.js';
- if(!required[key])
- {
- required[key] = {};
- var code = external.Download(key);
- var func = new Function('exports', (presource || '') + ';\r\n' + code+ ';\r\n' + (postsource || ''));
+ var url = external.GetFullUri(path, root || '');
+ var key = '~/' + url;
+ var module = required[key];
- bases.push(key);
- func(required[key]);
- bases.pop();
+ if(!module)
+ {
+ module = required[key] = {exports:{}};
+ var code = external.Download(url);
+ code = (presource || '') + ';\r\n' + code+ ';\r\n' + (postsource || '');
+ var func = new Function('window', 'module', 'exports', 'require', 'external', code);
+
+ func(module, module, module.exports, function (path) {
+ return _require(path, null, null, url);
+ });
}
- return required[key];
+ return module.exports;
};
})();
Please sign in to comment.
Something went wrong with that request. Please try again.