webpack via file config

closes CNVS-26213

Allow using a config file rather than an env var
for turning on web pack.

TEST PLAN:
 1) set USE_WEBPACK env var to nothing
 2) touch config/WEBPACK
 3) web pack should still be enabled
 4) rm config/WEBPACK
 5) restart server
 6) canvas should use require-js bundles again

Change-Id: Ie733b66326482341c2371fddefe17c1cfa3006b3
Reviewed-on: https://gerrit.instructure.com/69739
Tested-by: Jenkins
Reviewed-by: Ryan Shaw <ryan@instructure.com>
QA-Review: Jeremy Putnam <jeremyp@instructure.com>
Product-Review: Ethan Vizitei <evizitei@instructure.com>
This commit is contained in:
Ethan Vizitei 2016-01-05 14:50:15 -06:00 committed by Ethan Vizitei
parent b58d10c966
commit 2a78a61561
6 changed files with 35 additions and 7 deletions

View File

@ -233,7 +233,7 @@ module ApplicationHelper
end
def use_webpack?
if ENV['USE_WEBPACK'].present? && ENV['USE_WEBPACK'] != 'false' && ENV['USE_WEBPACK'] != 'False'
if CANVAS_WEBPACK
!(params[:require_js])
else
params[:webpack]

View File

@ -0,0 +1,10 @@
if !defined?(CANVAS_WEBPACK)
webpack_env_var = ENV['USE_WEBPACK']
if webpack_env_var.present?
CANVAS_WEBPACK = (webpack_env_var != 'false' && webpack_env_var != 'False')
else
webpack_file_path = Rails.root.join('config', "WEBPACK")
CANVAS_WEBPACK = File.exist?(webpack_file_path)
end
end

View File

@ -55,7 +55,9 @@ Webpack's output goes to "public/webpack-dist" for development js and
The environment variable USE_WEBPACK is useful for trying out the assets
that webpack builds locally. If set to 'true', when you start your server
you'll load JS from your webpack-dist directory rather than public/js.
you'll load JS from your webpack-dist directory rather than public/js. You
can do the same thing by touching the file "config/WEBPACK" (if present, it's
like having the USE_WEBPACK env var set).
At any time you can use the url parameter "require_js=1" to get the requirejs
version of the js instead so you can compare them side by side.

View File

@ -1,5 +1,20 @@
var fs = require("fs");
var karmaFiles = [];
var usingWebpack = (process.env.USE_WEBPACK == 'True' || process.env.USE_WEBPACK == 'true');
var webpackFileExists = false;
var webpackFilePath = __dirname + "/config/WEBPACK";
try {
fs.statSync(webpackFilePath);
webpackFileExists = true;
}
catch (e) {
console.log("no webpack file....")
}
var usingWebpack = (process.env.USE_WEBPACK == 'True' ||
process.env.USE_WEBPACK == 'true' ||
webpackFileExists);
// If we're using webpack, we don't want to load all the requirejs stuff;
if(usingWebpack){
karmaFiles = [

View File

@ -1,5 +1,6 @@
require 'timeout'
require 'json'
require_relative "../../config/initializers/webpack"
namespace :js do
@ -70,7 +71,7 @@ namespace :js do
desc 'test javascript specs with Karma'
task :test, :reporter do |task, args|
reporter = args[:reporter]
if ENV['USE_WEBPACK'].present? && ENV['USE_WEBPACK'] != 'false' && ENV['USE_WEBPACK'] != 'False'
if CANVAS_WEBPACK
Rake::Task['i18n:generate_js'].invoke
webpack_test_dir = Rails.root + "spec/javascripts/webpack"
FileUtils.rm_rf(webpack_test_dir)
@ -293,7 +294,7 @@ namespace :js do
desc "build webpack js for production"
task :webpack do
if ENV['USE_WEBPACK'].present? && ENV['USE_WEBPACK'] != 'false' && ENV['USE_WEBPACK'] != 'False'
if CANVAS_WEBPACK
if ENV['RAILS_ENV'] == 'production'
puts "--> Building PRODUCTION webpack bundles"
`npm run webpack-production`

View File

@ -6,7 +6,7 @@ describe "i18n js" do
before (:each) do
course_with_teacher_logged_in
get "/"
if ENV['USE_WEBPACK'].present? && ENV['USE_WEBPACK'] != 'false' && ENV['USE_WEBPACK'] != 'False'
if CANVAS_WEBPACK
# I18n will already be exposed in webpack land
else
# get I18n and _ global for all the tests
@ -59,7 +59,7 @@ describe "i18n js" do
(I18n.available_locales - [:en]).each do |locale|
exec_cs("I18n.locale = '#{locale}'")
rb_value = I18n.t('dashboard.confirm.close', locale: locale)
js_value = if ENV['USE_WEBPACK'].present? && ENV['USE_WEBPACK'] != 'false' && ENV['USE_WEBPACK'] != 'False'
js_value = if CANVAS_WEBPACK
driver.execute_script("return I18n.scoped('dashboard').t('confirm.close');")
else
require_exec('i18n!dashboard', "i18n.t('confirm.close')")