Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

  • Loading branch information...
commit fc540f7614f55ab6e084b3b58d309fa3419a6512 1 parent 13a6d03
Andy Edinborough authored
45 Chirpy/Engines/UglifyEngine.cs
... ... @@ -0,0 +1,45 @@
  1 +namespace Chirpy.Engines
  2 +{
  3 + using System.Collections.Generic;
  4 + using System.ComponentModel.Composition;
  5 + using ChirpyInterface;
  6 +
  7 + [Export(typeof (IEngine))]
  8 + [EngineMetadata("Uglify", "1.0", ".js", true)]
  9 + public class UglifyEngine : IEngine
  10 + {
  11 + [Import] public IJavascriptRunner JavascriptRunner { get; set; }
  12 +
  13 +
  14 + List<EngineResult> IEngine.Process(string contents, string filename)
  15 + {
  16 + var result = new EngineResult { FileName = filename, Extension = "min.js" };
  17 + var properties = new Dictionary<string, object>();
  18 + properties["js"] = contents;
  19 +
  20 + const string script = @"
  21 +var uglify = require('https://raw.github.com/mishoo/UglifyJS/master/uglify-js.js');
  22 +var js = external.Get('js');
  23 +var result = uglify(js);
  24 +external.Set('result', result);
  25 +";
  26 +
  27 + try
  28 + {
  29 + var javascriptResult = JavascriptRunner.Execute(script, properties);
  30 + result.Contents = javascriptResult.Get("result") as string;
  31 + result.Exceptions.AddRange(javascriptResult.Messages);
  32 +
  33 + } catch (ChirpyException cex) {
  34 + result.Exceptions.Add(cex);
  35 + }
  36 +
  37 + return new List<EngineResult> { result };
  38 + }
  39 +
  40 + public List<string> GetDependencies(string contents, string filename)
  41 + {
  42 + return null;
  43 + }
  44 + }
  45 +}
33 Chirpy/Javascript/JavascriptRunner.cs
@@ -48,8 +48,6 @@ string GetHtml(string script)
48 48 </head>
49 49 <body>
50 50 <script>
51   - var module = window;
52   -
53 51 var onerror = window.onerror = function(msg, file, line){
54 52 external.LogError(msg, line, 0, file, null);
55 53 };
@@ -69,21 +67,28 @@ string GetHtml(string script)
69 67 };
70 68
71 69 var require = window.require = (function() {
72   - var required = {}, bases = [];
73   - return function (path, presource, postsource) {
74   - var key = external.GetFullUri(path, bases.length == 0 ? '' : bases[bases.length-1]);
  70 + var required = {};
  71 + return function _require(path, presource, postsource, root) {
  72 + if(!path) return undefined;
  73 + if(path.substr(path.lastIndexOf('.')+1).toLowerCase() !== 'js')
  74 + path += '.js';
75 75
76   - if(!required[key])
77   - {
78   - required[key] = {};
79   - var code = external.Download(key);
80   - var func = new Function('exports', (presource || '') + ';\r\n' + code+ ';\r\n' + (postsource || ''));
  76 + var url = external.GetFullUri(path, root || '');
  77 + var key = '~/' + url;
  78 + var module = required[key];
81 79
82   - bases.push(key);
83   - func(required[key]);
84   - bases.pop();
  80 + if(!module)
  81 + {
  82 + module = required[key] = {exports:{}};
  83 + var code = external.Download(url);
  84 + code = (presource || '') + ';\r\n' + code+ ';\r\n' + (postsource || '');
  85 + var func = new Function('window', 'module', 'exports', 'require', 'external', code);
  86 +
  87 + func(module, module, module.exports, function (path) {
  88 + return _require(path, null, null, url);
  89 + });
85 90 }
86   - return required[key];
  91 + return module.exports;
87 92 };
88 93 })();
89 94

0 comments on commit fc540f7

Please sign in to comment.
Something went wrong with that request. Please try again.