diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index e666e722ccb..dc9b699d907 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -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] diff --git a/config/initializers/webpack.rb b/config/initializers/webpack.rb new file mode 100644 index 00000000000..178794b4cf6 --- /dev/null +++ b/config/initializers/webpack.rb @@ -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 diff --git a/doc/working_with_webpack.md b/doc/working_with_webpack.md index c4777f5e980..f31355cb2ef 100644 --- a/doc/working_with_webpack.md +++ b/doc/working_with_webpack.md @@ -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. diff --git a/karma.conf.js b/karma.conf.js index c7da12e6640..f9099d982fa 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -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 = [ diff --git a/lib/tasks/js.rake b/lib/tasks/js.rake index aa94e4afcc2..f181afe566f 100644 --- a/lib/tasks/js.rake +++ b/lib/tasks/js.rake @@ -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` diff --git a/spec/selenium/i18n_js_spec.rb b/spec/selenium/i18n_js_spec.rb index d72bd2f8849..5f548e09ac7 100644 --- a/spec/selenium/i18n_js_spec.rb +++ b/spec/selenium/i18n_js_spec.rb @@ -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')")