Bugs with webpack assets solved

This commit is contained in:
Juan Ferrer Toribio 2018-02-14 12:52:24 +01:00
parent cb92e48eef
commit 87dfa49692
6 changed files with 43 additions and 56 deletions

View File

@ -2,7 +2,7 @@ FROM node:8.9.4
COPY auth /app
COPY loopback /loopback
COPY nginx/static/webpack-assets.json /loopback/server/
COPY nginx/static/webpack-assets.json /loopback/client/
WORKDIR /app

View File

@ -6,7 +6,7 @@ module.exports = function(app) {
let applications = app.get('applications');
app.get('/', function(req, res) {
res.render('index.ejs');
app.renderIndex(res);
});
app.post('/login', function(req, res) {

View File

@ -1,38 +0,0 @@
require('require-yaml');
/**
* Obtains the webpack asset filenames with its hash.
*
* @param {String} main The main asset name
* @param {Array} deps The main asset dependencies
* @return {Array} The assets filenames
*/
function assets(main, deps) {
let jsFiles;
let env = process.env.NODE_ENV ? process.env.NODE_ENV : 'development';
if (env === 'development') {
const wpConfig = require('../../../webpack.config.yml');
let publicPath = wpConfig.publicPath;
jsFiles = [`${publicPath}/manifest.js`];
for (let dep of deps)
jsFiles.push(`${publicPath}/${dep}.js`);
jsFiles.push(`${publicPath}/${main}.js`);
} else {
const wpAssets = require('./webpack-assets.json');
let jsFiles = [wpAssets.manifest.js];
for (let dep of deps)
jsFiles.push(wpAssets[dep].js);
jsFiles.push(wpAssets[main].js);
}
return jsFiles;
}
module.exports = assets;

View File

@ -1,4 +1,3 @@
let loopback = require('loopback');
let boot = require('loopback-boot');
let fs = require('fs-extra');
@ -26,32 +25,58 @@ function vnBoot(app, rootDir, rootModule) {
// View
let wpAssets;
let viewDir = `${rootDir}/../client`;
if (fs.existsSync(viewDir)) {
try {
wpAssets = require('../client/webpack-assets.json');
} catch (e) {}
app.set('view engine', 'ejs');
app.set('views', viewDir);
app.use(loopback.static(path.resolve(rootDir, '../client')));
app.get('/', function(req, res) {
res.render(`${viewDir}/index.ejs`, {
assets: require('./assets')
});
});
}
app.renderIndex = async res => {
res.render(`${viewDir}/index.ejs`, {
assets: assets
});
function assets(main, deps) {
let jsFiles;
if (wpAssets) {
jsFiles = [wpAssets.manifest.js];
for (let dep of deps)
jsFiles.push(wpAssets[dep].js);
jsFiles.push(wpAssets[main].js);
} else {
let publicPath = '/static';
jsFiles = [`${publicPath}/manifest.js`];
for (let dep of deps)
jsFiles.push(`${publicPath}/${dep}.js`);
jsFiles.push(`${publicPath}/${main}.js`);
}
return jsFiles;
}
};
// Initialization
app.start = function(port) {
app.start = port => {
function onListen() {
app.emit('started');
if (require.main === rootModule) {
let packageJson = require(`${rootDir}/../package.json`);
let appName = packageJson.name;
let baseUrl = app.get('url').replace(/\/$/, '');
console.log(`Web server ${appName} listening at: %s`, baseUrl);
}
}
let args = port ? [port, onListen] : [onListen];
return app.listen.apply(app, args);

View File

@ -2,7 +2,7 @@ FROM node:8.9.4
COPY salix /app
COPY loopback /loopback
COPY nginx/static/webpack-assets.json /loopback/server/
COPY nginx/static/webpack-assets.json /loopback/client/
WORKDIR /app

View File

@ -5,7 +5,7 @@ module.exports = function(app) {
let token = req.cookies.vnToken;
validateToken(token, function(isValid) {
if (isValid)
res.render('index.ejs');
app.renderIndex(res);
else
redirectToAuth(res, req.get('origin'));
});