2017-04-28 12:03:25 +08:00
/ *
* Copyright ( C ) 2016 - present Instructure , Inc .
*
* This file is part of Canvas .
*
* Canvas is free software : you can redistribute it and / or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation , version 3 of the License .
*
* Canvas is distributed in the hope that it will be useful , but WITHOUT ANY
* WARRANTY ; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE . See the GNU Affero General Public License for more
* details .
*
* You should have received a copy of the GNU Affero General Public License along
* with this program . If not , see < http : //www.gnu.org/licenses/>.
* /
2019-08-28 06:00:11 +08:00
import canvasBaseTheme from '@instructure/canvas-theme'
import canvasHighContrastTheme from '@instructure/canvas-high-contrast-theme'
2017-02-15 05:54:51 +08:00
import moment from 'moment'
import tz from 'timezone_core'
import './fakeRequireJSFallback'
Webpack: better handling of chunks and cdn stuff
fixes: CNVS-31779 [webpack] bundle file size
test plan:
* using webpack, look at the network panel in
the chrome developer console.
* the total ammount of javascript loaded for any
given page should be much less than before this commit
and about on par with what requireJS loads for the same page.
* use chrome network panel to throttle to "Regular 3G"
* from both a clean cache and a primed cache:
* make sure that the total time to render the page
is about on par with requireJS
first change: load webpack chunks from CDN
fixes: CNVS-32261
test plan:
* run `npm run webpack-production`
* check your page, it should load js files and chunks
from the same hostname the page is served from.
* now set a host: in config/canvas_cdn.yml
(for testing locally, I just set it to http://127.0.0.1:3000,
* restart rails and reload the page, now the scripts and
chucks should come from that new hostname
second change:include fingerprints in webpack bundle and chunk filenames
closes: CNVS-28628
with this change we don't need to run `gulp rev`
after running webpack and we can load both the bundle
and chunk files safely from the cdn, since they will
have content-based fingerprints so we can tell
browsers to cache them forever. (we set those
http caching headers in lib/canvas/cdn/s3_uploader.rb:53)
test plan:
* for both dev and production
* run `npm run webpack` (or `npm run wepack-production`)
* with a cdn configured in canvas_cdn.yml, run:
RAILS_ENV=<dev or prod> bundle exec rake canvas:cdn:upload_to_s3
* check to make sure that the page loads the webpack js,
that there are no errors, and it all came from the cdn
third change: properly handle moment locale & timezone in webpack
test plan:
(see application_helper_spec.rb)
* set a non-default user timezone, context timezone
and locale
* dates should show up in your timezone and and
the date helper strings should be in your language
better handling of moment custom locales
closes: CNVS-33811
since we now have hatian and maori, we need to do this in
a way that is not just a one-off for maori
test plan:
* set your locale to maori
* in webpack & requireJS make sure dates show up right
Change-Id: I34dbff7d46a1047f9b459d5e1c0d141f435d42fb
Reviewed-on: https://gerrit.instructure.com/95737
Reviewed-by: Clay Diffrient <cdiffrient@instructure.com>
Tested-by: Jenkins
QA-Review: Benjamin Christian Nelson <bcnelson@instructure.com>
Product-Review: Ryan Shaw <ryan@instructure.com>
2016-12-13 01:32:11 +08:00
// we already put a <script> tag for the locale corresponding ENV.MOMENT_LOCALE
// on the page from rails, so this should not cause a new network request.
moment ( ) . locale ( ENV . MOMENT _LOCALE )
// These timezones and locales should already be put on the page as <script>
// tags from rails. this block should not create any network requests.
if ( typeof ENV !== 'undefined' ) {
if ( ENV . TIMEZONE ) tz . changeZone ( ENV . TIMEZONE )
if ( ENV . CONTEXT _TIMEZONE ) tz . preload ( ENV . CONTEXT _TIMEZONE )
if ( ENV . BIGEASY _LOCALE ) tz . changeLocale ( ENV . BIGEASY _LOCALE , ENV . MOMENT _LOCALE )
}
2018-08-23 04:28:54 +08:00
// This will inject and set up sentry for deprecation reporting. It should be
// stripped out and be a no-op in production.
if ( process . env . NODE _ENV !== 'production' && process . env . DEPRECATION _SENTRY _DSN ) {
const Raven = require ( 'raven-js' )
Raven . config ( process . env . DEPRECATION _SENTRY _DSN , {
2019-08-08 04:10:52 +08:00
ignoreErrors : [ 'renderIntoDiv' , 'renderSidebarIntoDiv' ] , // silence the `Cannot read property 'renderIntoDiv' of null` errors we get from the pre- rce_enhancements old rce code
2018-08-23 04:28:54 +08:00
release : process . env . GIT _COMMIT
2018-11-08 06:45:39 +08:00
} ) . install ( )
2018-08-23 04:28:54 +08:00
2018-11-08 06:45:39 +08:00
const setupRavenConsoleLoggingPlugin = require ( '../jsx/shared/helpers/setupRavenConsoleLoggingPlugin' )
2019-05-31 02:38:32 +08:00
. default
2018-11-08 06:45:39 +08:00
setupRavenConsoleLoggingPlugin ( Raven , { loggerName : 'console' } )
2018-08-23 04:28:54 +08:00
}
2019-08-27 11:41:36 +08:00
if ( process . env . NODE _ENV !== 'production' ) {
const {
filterUselessConsoleMessages
} = require ( '@instructure/js-utils/lib/filterUselessConsoleMessages' )
filterUselessConsoleMessages ( console )
}
2017-02-15 05:54:51 +08:00
// setup the inst-ui default theme
2020-11-10 22:07:22 +08:00
// override the fontFamily to include "Lato Extended", which we prefer
// to load over plain Lato (see LS-1559)
2017-02-15 05:54:51 +08:00
if ( ENV . use _high _contrast ) {
2020-11-10 22:07:22 +08:00
canvasHighContrastTheme . use ( {
overrides : {
typography : {
fontFamily : 'LatoWeb, "Lato Extended", Lato, "Helvetica Neue", Helvetica, Arial, sans-serif'
}
}
} )
2017-02-15 05:54:51 +08:00
} else {
2017-02-23 00:03:38 +08:00
const brandvars = window . CANVAS _ACTIVE _BRAND _VARIABLES || { }
2018-10-11 08:06:15 +08:00
// Set CSS transitions to 0ms in Selenium and JS tests
let transitionOverride = { }
2018-11-09 03:51:26 +08:00
if ( process . env . NODE _ENV === 'test' || window . INST . environment === 'test' ) {
2018-11-08 06:45:39 +08:00
transitionOverride = {
2018-10-11 08:06:15 +08:00
transitions : {
duration : '0ms'
}
}
}
canvasBaseTheme . use ( {
2020-11-10 22:07:22 +08:00
overrides : {
... transitionOverride ,
... brandvars ,
typography : {
fontFamily : 'LatoWeb, "Lato Extended", Lato, "Helvetica Neue", Helvetica, Arial, sans-serif'
}
}
2018-10-11 08:06:15 +08:00
} )
2017-02-15 05:54:51 +08:00
}
2018-11-13 03:15:25 +08:00
2019-05-31 02:38:32 +08:00
/* #__PURE__ */ if ( process . env . NODE _ENV === 'test' || window . INST . environment === 'test' ) {
2018-11-13 03:15:25 +08:00
// This is for the `wait_for_ajax_requests` method in selenium
window . _ _CANVAS _IN _FLIGHT _XHR _REQUESTS _ _ = 0
const send = XMLHttpRequest . prototype . send
XMLHttpRequest . prototype . send = function ( ) {
window . _ _CANVAS _IN _FLIGHT _XHR _REQUESTS _ _ ++
// 'loadend' gets fired after both successful and errored requests
this . addEventListener ( 'loadend' , ( ) => {
window . _ _CANVAS _IN _FLIGHT _XHR _REQUESTS _ _ --
window . dispatchEvent ( new CustomEvent ( 'canvasXHRComplete' ) )
} )
return send . apply ( this , arguments )
}
// and this so it also tracks `fetch` requests
const fetch = window . fetch
window . fetch = function ( ) {
window . _ _CANVAS _IN _FLIGHT _XHR _REQUESTS _ _ ++
const promise = fetch . apply ( this , arguments )
promise . finally ( ( ) => window . _ _CANVAS _IN _FLIGHT _XHR _REQUESTS _ _ -- )
return promise
}
}