webpack: Help handlebars compilation not be terri-slow.

closes CNVS-27874

TEST PLAN:
 1) run webpack build
 2) should be fast

Change-Id: I3d0b07cb7417cbd235bd628375d9888c6cc8d090
Reviewed-on: https://gerrit.instructure.com/74112
QA-Review: August Thornton <august@instructure.com>
Reviewed-by: Jon Jensen <jon@instructure.com>
Product-Review: Jon Jensen <jon@instructure.com>
Tested-by: Jenkins
This commit is contained in:
Ethan Vizitei 2016-03-09 10:31:16 -07:00 committed by Ethan Vizitei
parent bb133d199b
commit 16c6c1c172
3 changed files with 50 additions and 8 deletions

View File

@ -0,0 +1,41 @@
// This is a port of some of the functionality in
// lib/brandable_css.rb, because shelling out to run this
// stuff take FOREVER. That means for the time being, changes here
// if they happen may need to be mirrored in that file.
var fs = require('fs');
var yaml = require('js-yaml');
var child_process = require('child_process');
var configDocument = yaml.safeLoad(fs.readFileSync(__dirname + '/../config/brandable_css.yml', 'utf8'));
var variants = Object.keys(configDocument.variants);
var manifestKeySeperator = configDocument.manifest_key_seperator;
var useCompressed = process.env.RAILS_ENV == 'production'
var SassStyle = process.env.SASS_STYLE || (useCompressed ? 'compressed' : 'nested');
// load checksums data for fast retrieval during parsing
var checksumsFilePath = "/../" + configDocument.paths.bundles_with_deps + SassStyle;
var checksumsJson = JSON.parse(fs.readFileSync(__dirname + checksumsFilePath, 'utf8'))
var combinedChecksums = {}
Object.keys(checksumsJson).forEach(function(key){
if(checksumsJson.hasOwnProperty(key)){
var jsonObj = checksumsJson[key]
combinedChecksums[key] = {
combinedChecksum: jsonObj.combinedChecksum,
includesNoVariables: jsonObj.includesNoVariables
}
}
})
var cacheFor = function(bundleName, variant){
var key = [bundleName + ".scss", variant].join(manifestKeySeperator);
return combinedChecksums[key];
}
var fingerprint = function(bundleName){
var fingerprints = {}
variants.forEach(function(variant){
fingerprints[variant] = cacheFor(bundleName, variant);
})
return fingerprints;
};
module.exports.allFingerprintsFor = fingerprint;

View File

@ -9,7 +9,7 @@ var EmberHandlebars = require('ember-template-compiler').EmberHandlebars;
var ScopedHbsExtractor = require(__dirname + '/../gems/canvas_i18nliner/js/scoped_hbs_extractor'); var ScopedHbsExtractor = require(__dirname + '/../gems/canvas_i18nliner/js/scoped_hbs_extractor');
var PreProcessor = require(__dirname + '/../gems/canvas_i18nliner/node_modules/i18nliner-handlebars/dist/lib/pre_processor')['default']; var PreProcessor = require(__dirname + '/../gems/canvas_i18nliner/node_modules/i18nliner-handlebars/dist/lib/pre_processor')['default'];
var fs = require('fs'); var fs = require('fs');
var child_process = require('child_process'); var brandableCss = require(__dirname + "/brandableCss")
var compileHandlebars = function(data){ var compileHandlebars = function(data){
var path = data.path; var path = data.path;
@ -35,6 +35,8 @@ var compileHandlebars = function(data){
} }
}; };
var emitTemplate = function(path, name, result, dependencies, cssRegistration, partialRegistration){ var emitTemplate = function(path, name, result, dependencies, cssRegistration, partialRegistration){
var moduleName = "jst/" + path.replace(/.*\/\jst\//, '').replace(/\.handlebars/, ""); var moduleName = "jst/" + path.replace(/.*\/\jst\//, '').replace(/\.handlebars/, "");
return "" + return "" +
@ -60,13 +62,7 @@ var buildCssReference = function(name){
var cssStat = fs.statSync(matchingCssPath); var cssStat = fs.statSync(matchingCssPath);
if(cssStat.isFile()){ if(cssStat.isFile()){
var bundle = "jst/" + name; var bundle = "jst/" + name;
var fingerprintArgs = ["exec", "rake", "brand_configs:fingerprints["+bundle+"]"]; var cached = brandableCss.allFingerprintsFor(bundle)
var fingerprintOutput = child_process.spawnSync("bundle", fingerprintArgs);
if(fingerprintOutput.error){
console.log("ERROR IN FINGERPRINTING", fingerprintOutput.error);
throw fingerprintOutput.error;
}
var cached = JSON.parse(fingerprintOutput.stdout.toString());
var firstVariant = Object.keys(cached)[0]; var firstVariant = Object.keys(cached)[0];
var options = ""; var options = "";
if(cached[firstVariant]['includesNoVariables']){ if(cached[firstVariant]['includesNoVariables']){

View File

@ -2,6 +2,11 @@ require 'pathname'
require 'yaml' require 'yaml'
require 'open3' require 'open3'
# Some of the functionality for fingerprint lookup here is mirrored in
# frontend_build/brandableCss.js, because shelling out to run this
# stuff take FOREVER in the webpack build. That means for the time being,
# changes here if they happen may need to be mirrored in that file.
module BrandableCSS module BrandableCSS
APP_ROOT = defined?(Rails) && Rails.root || Pathname.pwd APP_ROOT = defined?(Rails) && Rails.root || Pathname.pwd
CONFIG = YAML.load_file(APP_ROOT.join('config/brandable_css.yml')).freeze CONFIG = YAML.load_file(APP_ROOT.join('config/brandable_css.yml')).freeze