From 4c512585268e99587c03d2c716679e4f44218fed Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Tue, 22 Jul 2014 14:13:13 -0400 Subject: [PATCH] PERF: Speed up JSHint tests by using local buffers instead of AJAX requests. --- .jshintignore | 10 ++++++++++ lib/discourse_iife.rb | 12 ++++++++++- .../tilt/es6_module_transpiler_template.rb | 8 ++++++++ test/javascripts/jshint_all.js.erb | 20 ++++++++++++++----- 4 files changed, 44 insertions(+), 6 deletions(-) diff --git a/.jshintignore b/.jshintignore index d22fa806efd..c4ab1fd1a95 100644 --- a/.jshintignore +++ b/.jshintignore @@ -1,3 +1,10 @@ +app/assets/javascripts/env.js +app/assets/javascripts/main_include.js +app/assets/javascripts/main_include_admin.js +app/assets/javascripts/preload_store.js +app/assets/javascripts/pagedown_custom.js +app/assets/javascripts/vendor.js +app/assets/javascripts/locales/i18n.js app/assets/javascripts/defer/html-sanitizer-bundle.js app/assets/javascripts/discourse/lib/Markdown.Editor.js jsapp/lib/Markdown.Editor.js @@ -10,3 +17,6 @@ public/javascripts/ spec/phantom_js/smoke_test.js vendor/ test/javascripts/helpers/ +test/javascripts/test_helper.js +test/javascripts/test_helper.js + diff --git a/lib/discourse_iife.rb b/lib/discourse_iife.rb index 6fe24cfeba4..78c3698ca97 100644 --- a/lib/discourse_iife.rb +++ b/lib/discourse_iife.rb @@ -23,7 +23,17 @@ class DiscourseIIFE < Sprockets::Processor return data if path =~ /\.shbrs/ return data if path =~ /\.hbrs/ - "(function () {\n\nvar $ = window.jQuery;\n// IIFE Wrapped Content Begins:\n\n#{data}\n\n// IIFE Wrapped Content Ends\n\n })(this);" + res = "(function () {\n\nvar $ = window.jQuery;\n// IIFE Wrapped Content Begins:\n\n#{data}\n\n// IIFE Wrapped Content Ends\n\n })(this);" + + # Include JS code for JSHint + unless Rails.env.production? + req_path = path.sub(Rails.root.to_s, '') + .sub("/app/assets/javascripts", "") + .sub("/test/javascripts", "") + res << "\nwindow.__jshintSrc = window.__jshintSrc || {}; window.__jshintSrc['/assets#{req_path}'] = #{data.to_json};\n" + end + + res end end diff --git a/lib/es6_module_transpiler/tilt/es6_module_transpiler_template.rb b/lib/es6_module_transpiler/tilt/es6_module_transpiler_template.rb index 11e1bfed45d..8afe502ce05 100644 --- a/lib/es6_module_transpiler/tilt/es6_module_transpiler_template.rb +++ b/lib/es6_module_transpiler/tilt/es6_module_transpiler_template.rb @@ -65,6 +65,8 @@ module Tilt @output = klass.v8.eval(generate_source(scope)) end + source = @output.dup + # For backwards compatibility with plugins, for now export the Global format too. # We should eventually have an upgrade system for plugins to use ES6 or some other # resolve based API. @@ -81,6 +83,12 @@ module Tilt @output << "\n\nDiscourse.#{class_name}#{type.classify} = require('#{require_name}').default" end + # Include JS code for JSHint + unless Rails.env.production? + req_path = "/assets/#{scope.logical_path}.js" + @output << "\nwindow.__jshintSrc = window.__jshintSrc || {}; window.__jshintSrc['#{req_path}'] = #{source.to_json};\n" + end + @output end diff --git a/test/javascripts/jshint_all.js.erb b/test/javascripts/jshint_all.js.erb index 04f74634d42..f966c43c284 100644 --- a/test/javascripts/jshint_all.js.erb +++ b/test/javascripts/jshint_all.js.erb @@ -9,17 +9,24 @@ var qHint = function(name, sourceFile, options, globals) { } return asyncTestDiscourse(name, function() { + if (typeof window.__jshintSrc !== "undefined") { + var src = window.__jshintSrc[sourceFile]; + if (src) { + start(); + qHint.validateFile(src, options, globals); + return; + } + } + + + console.warn("Using AJAX for JSHint " + sourceFile); + qHint.sendRequest(sourceFile, function(req) { start(); if (req.status == 200) { var text = req.responseText; - - // Remove our generate IIFEs so we get the same line numbers as original - // files - text = text.replace(/^[^]*\/\/ IIFE Wrapped Content Begins:\n\n/m, ""); - text = text.replace(/\n\n\/\/ IIFE Wrapped Content Ends[^]*$/m, ""); qHint.validateFile(text, options, globals); } else { ok(false, "HTTP error " + req.status + @@ -32,6 +39,9 @@ var qHint = function(name, sourceFile, options, globals) { qHint.validateFile = function (source, options, globals) { var i, len, err; + source = source.replace(/^[^]*\/\/ IIFE Wrapped Content Begins:\n\n/m, ""); + source = source.replace(/\n\n\/\/ IIFE Wrapped Content Ends[^]*$/m, ""); + if (JSHINT(source, options, globals)) { ok(true); return;