Use relative URL instead of absolute to load plugin and set administration origin

This commit is contained in:
Aurelsicoko 2018-01-09 17:09:54 +01:00
parent 0b3e8f7ae5
commit ce61844427
16 changed files with 150 additions and 95 deletions

View File

@ -7,13 +7,29 @@
/* eslint-disable */
// Retrieve remote and backend URLs.
const remoteURL = window.location.port === '4000' ? 'http://localhost:4000/admin' : process.env.REMOTE_URL || 'http://localhost:1337/admin';
const backendURL = process.env.BACKEND_URL || 'http://localhost:1337';
const remoteURL = (() => {
if (window.location.port === '4000') {
return 'http://localhost:4000/admin';
}
// Relative URL (ex: /dashboard)
if (process.env.REMOTE_URL[0] === '/' && process.env.REMOTE_URL.length > 1) {
return (window.location.origin + process.env.REMOTE_URL).replace(/\/$/, '');
}
return process.env.REMOTE_URL.replace(/\/$/, '');
})();
const backendURL = (process.env.BACKEND_URL === '/' ? window.location.origin : process.env.BACKEND_URL);
// Retrieve development URL to avoid to re-build.
const $body = document.getElementsByTagName('body')[0];
const devFrontURL = $body.getAttribute('front');
const devBackendURL = $body.getAttribute('back');
const devFrontURL = $body.getAttribute('front') ? window.location.origin + $body.getAttribute('front').replace(/\/$/, '') : null;
const devBackendURL = $body.getAttribute('back') ? window.location.origin + $body.getAttribute('back').replace(/\/$/, '') : null;
console.log("Remote URL", remoteURL);
console.log("Backend URL", backendURL);
console.log("DevFront URL", devFrontURL);
console.log("DevBackend URL", devBackendURL);
$body.removeAttribute('front');
$body.removeAttribute('back');
@ -51,11 +67,11 @@ const plugins = (() => {
/* eslint-enable */
// Create redux store with history
const initialState = {};
const basename = (devFrontURL || remoteURL).replace(window.location.origin, '');
const history = createHistory({
basename: (devFrontURL || remoteURL).replace(window.location.origin, ''),
basename,
});
const store = configureStore(initialState, history);
const store = configureStore({}, history);
const render = (translatedMessages) => {
ReactDOM.render(
@ -124,7 +140,7 @@ if (window.location.port !== '4000') {
$body.appendChild(newScript);
};
script.src = plugin.source[process.env.NODE_ENV];
script.src = `${basename}${plugin.source[process.env.NODE_ENV]}`.replace('//', '/');
$body.appendChild(script);
});
})

View File

@ -46,4 +46,4 @@
"npm": ">= 3.0.0"
},
"license": "MIT"
}
}

View File

@ -4,37 +4,48 @@ const _ = require('lodash');
shell.echo('');
shell.echo('🕓 The setup process can take few minutes.');
shell.echo('📦 Installing admin packages...');
shell.echo('');
shell.echo(`🔸 Administration Panel`);
shell.echo('📦 Installing packages...');
const pwd = shell.pwd();
const isDevelopmentMode = path.resolve(pwd.stdout).indexOf('strapi-admin') !== -1;
const appPath = isDevelopmentMode ? path.resolve(process.env.PWD, '..') : path.resolve(pwd.stdout, '..');
shell.rm('-rf', path.resolve(pwd.stdout, 'package-lock.json'));
// Remove package-lock.json.
shell.rm('-rf', path.resolve(appPath, 'package-lock.json'));
shell.rm('-rf', path.resolve(appPath, 'admin', 'package-lock.json'));
shell.exec(`cd ${path.resolve(pwd.stdout)} && npm install`, {
// Install the project dependencies.
shell.exec(`cd ${appPath} && npm install --ignore-scripts`, {
silent: true
});
// Install the administration dependencies.
shell.exec(`cd ${path.resolve(appPath, 'admin')} && npm install`, {
silent: true
});
if (isDevelopmentMode) {
shell.exec(`cd ${path.resolve(pwd.stdout)} && npm link strapi-helper-plugin && npm link strapi-utils`, {
shell.exec(`cd ${path.resolve(appPath, 'admin')} && npm link strapi-helper-plugin && npm link strapi-utils`, {
silent: true
});
} else {
shell.exec(`cd ${path.resolve(pwd.stdout, 'node_modules', 'strapi-helper-plugin')} && npm install`, {
shell.exec(`cd ${path.resolve(appPath, 'admin', 'node_modules', 'strapi-helper-plugin')} && npm install`, {
silent: true
});
}
shell.echo('🏗 Building...');
const build = shell.exec(`cd ${path.resolve(pwd.stdout)} && APP_PATH=${appPath} npm run build `, {
silent: true
const build = shell.exec(`cd ${path.resolve(appPath, 'admin')} && APP_PATH=${appPath} npm run build `, {
silent: false
});
if (build.stderr && build.code !== 0) {
console.error(build.stderr);
process.exit(1);
}
shell.echo('✅ Success');
@ -53,12 +64,13 @@ shell.ls('* -d', plugins).forEach(function (plugin) {
});
shell.echo('🏗 Building...');
const build = shell.exec(`cd ${path.resolve(plugins, plugin)} && npm run build`, {
const build = shell.exec(`cd ${path.resolve(plugins, plugin)} && APP_PATH=${appPath} npm run build`, {
silent: true
});
if (build.stderr && build.code !== 0) {
console.error(build.stderr);
process.exit(1);
}
shell.echo('✅ Success');

View File

@ -24,7 +24,7 @@ module.exports = scope => {
'description': 'A Strapi application.',
'main': './server.js',
'scripts': {
'setup': 'npm install --ignore-scripts && cd admin && npm run setup', // Ready to deploy setup
'setup': 'cd admin && npm run setup', // Ready to deploy setup
'start': 'node server.js',
'strapi': 'node_modules/strapi/bin/strapi.js', // Allow to use `npm run strapi` CLI,
'lint': 'node_modules/.bin/eslint api/**/*.js config/**/*.js plugins/**/*.js',

View File

@ -18,8 +18,14 @@ const appPath = (() => {
return isAdmin ? path.resolve(process.env.PWD, '..') : path.resolve(process.env.PWD, '..', '..');
})();
const adminPath = path.resolve(appPath, 'admin');
const isSetup = path.resolve(process.env.PWD, '..', '..') === path.resolve(process.env.INIT_CWD);
const adminPath = (() => {
if (isSetup) {
return isAdmin ? path.resolve(appPath, 'strapi-admin') : path.resolve(process.env.PWD);
}
return path.resolve(appPath, 'admin');
})();
if (!isSetup) {
try {
@ -30,7 +36,9 @@ if (!isSetup) {
strapi.log.level = 'silent';
(async () => {
await strapi.load();
await strapi.load({
environment: process.env.NODE_ENV
});
})();
} catch (e) {
console.log(e);
@ -40,8 +48,8 @@ if (!isSetup) {
// Define remote and backend URLs.
const URLs = {
host: null,
backend: null
host: '/admin',
backend: '/'
};
if (isAdmin && !isSetup) {
@ -53,8 +61,13 @@ if (isAdmin && !isSetup) {
const path = _.get(server, 'admin.path', '/admin');
if (process.env.PWD.indexOf('/admin') !== -1) {
URLs.host = _.get(server, 'admin.build.host', `http://${_.get(server, 'host', 'localhost')}:${_.get(server, 'port', 1337)}${path}`);
URLs.backend = _.get(server, 'admin.build.backend', `http://${_.get(server, 'host', 'localhost')}:${_.get(server, 'port', 1337)}`);
if (_.get(server, 'admin.build.host')) {
URLs.host = `${_.get(server, 'admin.build.host').replace(/\/$/, '')}`;
} else {
URLs.host = _.get(server, 'admin.path', '/admin');
}
URLs.backend = _.get(server, 'admin.build.backend', `/`);
}
} catch (e) {
throw new Error(`Impossible to access to ${serverConfig}`)

View File

@ -25,11 +25,18 @@ const appPath = (() => {
return isAdmin ? path.resolve(process.env.PWD, '..') : path.resolve(process.env.PWD, '..', '..');
})();
const adminPath = (() => {
if (isSetup) {
return isAdmin ? path.resolve(appPath, 'strapi-admin') : path.resolve(process.env.PWD, '..');
}
return path.resolve(appPath, 'admin');
})();
// Necessary configuration file to ensure that plugins will be loaded.
const pluginsToInitialize = (() => {
try {
return require(path.resolve(appPath, 'admin', 'src', 'config', 'plugins.json'));
return require(path.resolve(adminPath, 'admin', 'src', 'config', 'plugins.json'));
} catch (e) {
return [];
}
@ -60,27 +67,6 @@ const plugins = [
// new BundleAnalyzerPlugin(),
];
// Default configurations.
const settings = {
path: 'admin',
folder: 'plugins',
host: 'http://localhost:1337'
};
if (!isSetup) {
// Load server configurations.
const serverConfig = path.resolve(appPath, 'config', 'environments', _.lowerCase(process.env.NODE_ENV), 'server.json');
const server = require(serverConfig);
const pathAccess = _.get(server, 'admin.path', 'admin');
Object.assign(settings, {
path: pathAccess[0] === '/' ? pathAccess.substring(1) : pathAccess,
folder: _.get(server, 'admin.build.plugins.folder', 'plugins'),
host:_.get(server, 'admin.build.host', `http://${_.get(server, 'host', 'localhost')}:${_.get(server, 'port', 1337)}${path}`);
});
}
// Build the `index.html file`
if (isAdmin) {
plugins.push(new HtmlWebpackPlugin({
@ -120,7 +106,7 @@ const main = (() => {
return path.join(appPath, 'admin', 'admin', 'src', 'app.js');
}
return path.join(appPath, 'admin', 'node_modules', 'strapi-helper-plugin', 'lib', 'src', 'app.js');
return path.join(process.env.PWD, 'node_modules', 'strapi-helper-plugin', 'lib', 'src', 'app.js');
})();
module.exports = require('./webpack.base.babel')({

View File

@ -111,4 +111,4 @@
"whatwg-fetch": "^2.0.3",
"write-json-webpack-plugin": "^1.0.2"
}
}
}

View File

@ -48,4 +48,4 @@
"react-select": "^1.0.0-rc.5",
"strapi-helper-plugin": "3.0.0-alpha.7.3"
}
}
}

View File

@ -48,4 +48,4 @@
"npm": ">= 3.0.0"
},
"license": "MIT"
}
}

View File

@ -46,4 +46,4 @@
"npm": ">= 3.0.0"
},
"license": "MIT"
}
}

View File

@ -45,4 +45,4 @@
"npm": ">= 3.0.0"
},
"license": "MIT"
}
}

View File

@ -48,4 +48,4 @@
"npm": ">= 3.0.0"
},
"license": "MIT"
}
}

View File

@ -9,7 +9,9 @@ const cheerio = require('cheerio')
module.exports = function() {
return new Promise((resolve, reject) => {
try {
if (this.config.environment === 'test') {
const environment = this.config.environment;
if (environment === 'test') {
return resolve();
}
@ -40,12 +42,16 @@ module.exports = function() {
if ($(this).attr('src')) {
const parse = path.parse($(this).attr('src'));
$(this).attr('src', `${_.get(strapi.config.currentEnvironment.server, 'admin.path', '/admin')}/${parse.base}`);
if (environment === 'production') {
$(this).attr('src', `/${parse.base}`);
} else {
$(this).attr('src', `${_.get(strapi.config.currentEnvironment.server, 'admin.path', '/admin')}/${parse.base}`);
}
}
});
// Remove previous and use build configurations.
if (this.config.environment === 'production') {
if (environment === 'production') {
$('body').removeAttr('front');
$('body').removeAttr('back');
} else {

View File

@ -1,7 +1,7 @@
{
"cors": {
"enabled": false,
"origin": true,
"origin": "*",
"expose": [
"WWW-Authenticate",
"Server-Authorization"
@ -14,7 +14,8 @@
"PUT",
"PATCH",
"DELETE",
"HEAD"
"HEAD",
"OPTIONS"
],
"headers": [
"Content-Type",

View File

@ -8,7 +8,7 @@ const { after, includes, indexOf, drop, dropRight, uniq, defaultsDeep, get, set,
module.exports = async function() {
// Set if is admin destination for middleware application.
this.app.use(async (ctx, next) => {
if (ctx.request.header['origin'] === 'http://localhost:4000') {
if (ctx.request.header['origin'] === 'http://localhost:4000' || ctx.request.method === 'OPTIONS') {
ctx.request.header['x-forwarded-host'] = 'strapi';
}

View File

@ -2,79 +2,100 @@ const shell = require('shelljs');
// Store installation start date.
const installationStartDate = new Date();
const watcher = (label, cmd, withSuccess = true) => {
if (label.length > 0) {
shell.echo(`📦 ${label}`);
}
const data = shell.exec(cmd, {
silent: true
});
if (data.stderr && data.code !== 0) {
console.error(data.stderr);
process.exit(1);
}
if (label.length > 0 && withSuccess) {
shell.echo('✅ Success');
shell.echo('');
}
};
shell.echo('');
shell.echo('🕓 The setup process can take few minutes.');
shell.echo('');
// Remove existing binary.
shell.rm('-f', '/usr/local/bin/strapi.js');
shell.echo('Linking Strapi CLI...');
shell.cd('packages/strapi-utils');
shell.exec('npm link');
watcher('Linking strapi-utils...', 'npm link');
shell.cd('../strapi-generate');
shell.exec('npm install ../strapi-utils');
shell.exec('npm link');
watcher('', 'npm install ../strapi-utils');
watcher('Linking strapi-generate...', 'npm link');
shell.cd('../strapi-generate-api');
shell.exec('npm link');
watcher('Linking strapi-generate-api...', 'npm link');
shell.cd('../strapi-helper-plugin');
shell.exec('npm link');
watcher('Linking strapi-helper-plugin...', 'npm link');
shell.cd('../strapi-admin');
shell.exec('npm install ../strapi-helper-plugin');
shell.exec('npm install ../strapi-utils');
watcher('', 'npm install ../strapi-helper-plugin');
watcher('', 'npm install ../strapi-utils');
shell.rm('-f', 'package-lock.json');
shell.exec('npm link');
shell.exec('npm run build');
watcher('Linking strapi-admin...', 'npm link', false);
watcher('Building...', 'npm run build');
shell.cd('../strapi-generate-admin');
shell.exec('npm install ../strapi-admin');
shell.exec('npm link');
watcher('', 'npm install ../strapi-admin');
watcher('Linking strapi-generate-admin...', 'npm link');
shell.cd('../strapi-generate-new');
shell.exec('npm install ../strapi-utils');
shell.exec('npm link');
watcher('', 'npm install ../strapi-utils');
watcher('Linking strapi-generate-new', 'npm link');
shell.cd('../strapi-mongoose');
shell.exec('npm install ../strapi-utils');
shell.exec('npm link');
watcher('', 'npm install ../strapi-utils');
watcher('Linking strapi-mongoose...', 'npm link');
shell.cd('../strapi');
shell.exec('npm install ../strapi-generate ../strapi-generate-admin ../strapi-generate-api ../strapi-generate-new ../strapi-generate-plugin ../strapi-generate-policy ../strapi-generate-service ../strapi-utils');
shell.exec('npm link');
watcher('', 'npm install ../strapi-generate ../strapi-generate-admin ../strapi-generate-api ../strapi-generate-new ../strapi-generate-plugin ../strapi-generate-policy ../strapi-generate-service ../strapi-utils');
watcher('Linking strapi...', 'npm link');
shell.cd('../strapi-plugin-email');
shell.exec('npm install ../strapi-helper-plugin');
watcher('', 'npm install ../strapi-helper-plugin');
shell.rm('-f', 'package-lock.json');
shell.exec('npm link');
shell.exec('npm run build');
watcher('Linking strapi-plugin-email...', 'npm link', false);
watcher('Building...', 'npm run build');
shell.cd('../strapi-plugin-users-permissions');
shell.exec('npm install ../strapi-helper-plugin');
watcher('', 'npm install ../strapi-helper-plugin');
shell.rm('-f', 'package-lock.json');
shell.exec('npm link');
shell.exec('npm run build');
watcher('Linking strapi-plugin-users-permissions...', 'npm link', false);
watcher('Building...', 'npm run build');
shell.cd('../strapi-plugin-content-manager');
shell.exec('npm install ../strapi-helper-plugin');
watcher('', 'npm install ../strapi-helper-plugin');
shell.rm('-f', 'package-lock.json');
shell.exec('npm link');
shell.exec('npm run build');
watcher('Linking strapi-plugin-content-manager...', 'npm link', false);
watcher('Building...', 'npm run build');
shell.cd('../strapi-plugin-settings-manager');
shell.exec('npm install ../strapi-helper-plugin');
watcher('', 'npm install ../strapi-helper-plugin');
shell.rm('-f', 'package-lock.json');
shell.exec('npm link');
shell.exec('npm run build');
watcher('Linking strapi-plugin-settings-manager...', 'npm link', false);
watcher('Building...', 'npm run build');
shell.cd('../strapi-plugin-content-type-builder');
shell.exec('npm install ../strapi-helper-plugin');
shell.exec('npm install ../strapi-generate');
shell.exec('npm install ../strapi-generate-api');
watcher('', 'npm install ../strapi-helper-plugin');
watcher('', 'npm install ../strapi-generate');
watcher('', 'npm install ../strapi-generate-api');
shell.rm('-f', 'package-lock.json');
shell.exec('npm link');
shell.exec('npm run build');
watcher('Linking strapi-plugin-content-type-builder...', 'npm link', false);
watcher('Building...', 'npm run build');
// Log installation duration.
const installationEndDate = new Date();