75 lines
2.0 KiB
JavaScript
Raw Normal View History

2016-08-18 11:41:13 +02:00
/**
* app.js
*
* This is the entry file for the application when running the build
2016-08-18 11:41:13 +02:00
* code.
*/
/* eslint-disable */
2016-08-18 11:41:13 +02:00
import 'babel-polyfill';
import { findIndex } from 'lodash';
import 'sanitize.css/sanitize.css';
2017-09-09 15:49:59 +02:00
import 'whatwg-fetch';
import {
2018-09-25 17:11:14 +02:00
getAppPluginsSucceeded,
unsetHasUserPlugin,
} from 'containers/App/actions';
import { basename, store } from './createStore';
import './intlPolyfill';
import './public-path';
import './strapi';
const dispatch = store.dispatch;
2016-08-18 11:41:13 +02:00
// Don't inject plugins in development mode.
if (window.location.port !== '4000') {
2019-01-30 13:25:08 +01:00
fetch(`${strapi.remoteURL}/config/plugins.json`, { cache: 'no-cache' })
.then(response => {
return response.json();
})
.then(plugins => {
2018-09-25 17:11:14 +02:00
dispatch(getAppPluginsSucceeded(plugins));
if (findIndex(plugins, ['id', 'users-permissions']) === -1) {
2018-09-25 17:11:14 +02:00
dispatch(unsetHasUserPlugin());
}
2017-12-06 11:58:24 +01:00
const $body = document.getElementsByTagName('body')[0];
(plugins || []).forEach(plugin => {
const script = document.createElement('script');
script.type = 'text/javascript';
script.onerror = function (oError) {
const source = new URL(oError.target.src);
const url = new URL(`${strapi.remoteURL}`);
if (!source || !url) {
throw new Error(`Impossible to load: ${oError.target.src}`);
}
// Remove tag.
$body.removeChild(script);
// New attempt with new src.
const newScript = document.createElement('script');
newScript.type = 'text/javascript';
newScript.src = `${url.origin}${source.pathname}`;
$body.appendChild(newScript);
};
script.src = plugin.source[process.env.NODE_ENV].indexOf('://') === -1 ?
`${basename}${plugin.source[process.env.NODE_ENV]}`.replace('//', '/'): // relative
plugin.source[process.env.NODE_ENV]; // absolute
$body.appendChild(script);
});
})
.catch(err => {
2018-08-08 17:27:47 +02:00
console.log(err); // eslint-disable-line no-console
});
}
2016-09-30 18:25:04 +02:00
export {
dispatch,
2016-10-05 11:32:31 +02:00
};