From 8f16f114543b78ba6b0d575a7be9fc4897647043 Mon Sep 17 00:00:00 2001 From: Joel Chen Date: Thu, 9 Apr 2020 16:56:07 -0700 Subject: [PATCH] fix: avoid log error in dev mode --- packages/subapp-web/lib/load.js | 4 ++-- packages/subapp-web/lib/util.js | 26 +++++++++++++++++++++++--- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/packages/subapp-web/lib/load.js b/packages/subapp-web/lib/load.js index beb15c02e..73189fb49 100644 --- a/packages/subapp-web/lib/load.js +++ b/packages/subapp-web/lib/load.js @@ -77,13 +77,13 @@ module.exports = function setup(setupContext, { props: setupProps }) { const retrieveDevServerBundle = async () => { return new Promise(resolve => { const routeOptions = setupContext.routeOptions; - const path = `${bundleBase}${bundleAsset.name}`; + const path = Path.posix.join(bundleBase, bundleAsset.name); const bundleUrl = formUrl({ ...routeOptions.httpDevServer, path }); retrieveUrl(bundleUrl, (err, resp, body) => { if (err || resp.statusCode !== 200) { const msg = makeDevDebugMessage( `Error: fail to retrieve subapp bundle from '${bundleUrl}' for inlining in index HTML -${err || body}` +Response: ${err || body}` ); console.error(msg); // eslint-disable-line resolve(makeDevDebugHtml(msg)); diff --git a/packages/subapp-web/lib/util.js b/packages/subapp-web/lib/util.js index 116d9d181..d9a01d612 100644 --- a/packages/subapp-web/lib/util.js +++ b/packages/subapp-web/lib/util.js @@ -164,12 +164,32 @@ const utils = { return CDN_JS_BUNDLES; } - if (routeOptions.cdn.enable !== false && CDN_ASSETS === undefined) { + if (routeOptions.cdn.enable === true && CDN_ASSETS === undefined) { + const env = process.env.NODE_ENV; + const prod = env === "production"; + const ignoreMsg = `== This is OK and you can ignore this message if it's what you intended. ==`; + const logError = console.error; // eslint-disable-line + if (!prod) { + logError( + `Warning: you've set cdn.enable to true for NODE_ENV ${env}. +Generally you should do that for production deployment in the cloud only. +${ignoreMsg}` + ); + } + try { const assetsFp = Path.resolve(cdnAssetsFile); - CDN_ASSETS = require(assetsFp); + CDN_ASSETS = JSON.parse(Fs.readFileSync(assetsFp)); } catch (err) { - console.error("Error: Loading CDN assets map failed", err); // eslint-disable-line + if (prod) { + logError("Error: Loading CDN assets map failed", err); // eslint-disable-line + } else { + logError( + `Warning: load CDN asset map failed, default to local assets for NODE_ENV ${env} mode. path: ${cdnAssetsFile} +Error: ${err.message} +${ignoreMsg}` + ); + } CDN_ASSETS = false; } }