subapp-web
Advanced tools
Comparing version 1.0.39 to 1.0.40
@@ -12,2 +12,4 @@ "use strict"; | ||
const { getXRequire } = require("@xarc/app").isomorphicLoader; | ||
module.exports = function setup(setupContext) { | ||
@@ -41,2 +43,10 @@ const cdnEnabled = _.get(setupContext, "routeOptions.cdn.enable"); | ||
// For subapp version 2, when using to do dynamic import, | ||
// code to translate for webpack 4 jsonp bundle loading. | ||
// requires processing done by xarc-webpack/src/plugins/jsonp-script-src-plugin | ||
// TBD: need to update when upgrade to webpack 5 | ||
const webpackJsonpJS = cdnEnabled | ||
? Fs.readFileSync(Path.join(__dirname, distDir, "webpack4-jsonp.js")).toString() | ||
: ""; | ||
let inlineRuntimeJS = ""; | ||
@@ -65,2 +75,3 @@ let runtimeEntryPoints = []; | ||
<script>/*LJ*/${loadJs}/*LJ*/ | ||
${webpackJsonpJS} | ||
${clientJs} | ||
@@ -89,2 +100,18 @@ ${cdnJs} | ||
const setupIsomorphicCdnUrlMapping = () => { | ||
const extRequire = getXRequire(); | ||
if (!extRequire) return; | ||
const cdnAssets = util.loadCdnAssets(setupContext.routeOptions); | ||
const cdnKeys = Object.keys(cdnAssets).map(k => Path.basename(k)); | ||
extRequire.setUrlMapper(url => { | ||
const urlBaseName = Path.basename(url); | ||
return (cdnKeys.includes(urlBaseName) && cdnAssets[urlBaseName]) || url; | ||
}); | ||
}; | ||
if (cdnEnabled) { | ||
setupIsomorphicCdnUrlMapping(); | ||
} | ||
return { | ||
@@ -91,0 +118,0 @@ process: context => { |
102
lib/util.js
@@ -132,2 +132,37 @@ "use strict"; | ||
/* webpack 4 production stats structure | ||
{ | ||
entrypoints: { | ||
"extras": [0, 1, 4] --> A: assets.chunks[4], B: chunks.id | ||
}, | ||
assets: [ | ||
{ | ||
"name": "extras.bundle.js", | ||
"size": 2104, | ||
A: --> "chunks": [4], | ||
"chunkNames": ["extras"], --> C: assetsByChunkName | ||
"info": { | ||
"development": true // if development only asset | ||
}, | ||
"emitted": false | ||
}, | ||
], | ||
chunks: [ | ||
{ | ||
B: --> "id": 4, | ||
"hash": "60a75cbfa4c94ca0b4ff", | ||
"names": ["extras"], --> C: assetsByChunkName | ||
"entry": true, | ||
"initial": true, | ||
"rendered": true | ||
}, | ||
], | ||
assetsByChunkName: { | ||
C: --> "extras": ["extras.bundle.js", "../map/extras.bundle.js.map"], | ||
} | ||
} | ||
*/ | ||
mapCdnAssets(bundlesById, basePath = "", cdnAssets) { | ||
@@ -260,43 +295,36 @@ const cdnBundles = {}; | ||
// | ||
const chunksById = stats.chunks.reduce((a, chunk) => { | ||
a[chunk.id] = chunk; | ||
return a; | ||
}, {}); | ||
const chunksById = stats.chunks.reduce((result, chunk) => { | ||
const { id, names } = chunk; | ||
// in dev mode, chunk id is the same as the name | ||
// in prod mode, chunk id is an integer (index) | ||
if (names.indexOf(id) < 0) { | ||
// save the names for the id | ||
_.set(result, ["_names_", id], names); | ||
} | ||
const byChunkId = {}; | ||
for (const ep in stats.entrypoints) { | ||
for (const id of stats.entrypoints[ep]) { | ||
const names = chunksById[id].names; | ||
names.forEach(name => { | ||
const assets = stats.assetsByChunkName[name]; | ||
// in dev mode, chunk id is the same as the name | ||
// in prod mode, chunk id is an integer (index) | ||
if (names.indexOf(id) < 0) { | ||
// save the names for the id | ||
_.set(byChunkId, ["_names_", id], names); | ||
} | ||
// now assign the assets into byChunkId according to extensions | ||
[] | ||
.concat(assets) | ||
.filter(x => x) | ||
.forEach(asset => { | ||
const ext = Path.extname(asset).substring(1); | ||
assert(ext, `asset ${asset} doesn't have extension`); | ||
const found = _.get(result, [ext, id]); | ||
if (found) { | ||
if (found.indexOf(asset) < 0) { | ||
result[ext][id] = [].concat(found, asset); | ||
} | ||
} else { | ||
_.set(result, [ext, id], asset); | ||
} | ||
}); | ||
}); | ||
names.forEach(name => { | ||
const assets = stats.assetsByChunkName[name]; | ||
return result; | ||
}, {}); | ||
// now assign the assets into byChunkId according to extensions | ||
[] | ||
.concat(assets) | ||
.filter(x => x) | ||
.forEach(asset => { | ||
const ext = Path.extname(asset).substring(1); | ||
assert(ext, `asset ${asset} doesn't have extension`); | ||
const found = _.get(byChunkId, [ext, id]); | ||
if (found) { | ||
if (found.indexOf(asset) < 0) { | ||
byChunkId[ext][id] = [].concat(found, asset); | ||
} | ||
} else { | ||
_.set(byChunkId, [ext, id], asset); | ||
} | ||
}); | ||
}); | ||
} | ||
} | ||
return byChunkId; | ||
return chunksById; | ||
}, | ||
@@ -303,0 +331,0 @@ |
{ | ||
"name": "subapp-web", | ||
"version": "1.0.39", | ||
"version": "1.0.40", | ||
"description": "Electrode subapp web support", | ||
@@ -38,2 +38,5 @@ "module": "dist/dev/index.js", | ||
}, | ||
"peerDependencies": { | ||
"@xarc/app": "^8.1.20" | ||
}, | ||
"devDependencies": { | ||
@@ -46,10 +49,11 @@ "@babel/cli": "^7.2.3", | ||
"@babel/register": "^7.7.7", | ||
"@xarc/app": "^8.1.20", | ||
"babel-preset-minify": "^0.5.1", | ||
"chai-as-promised": "^7.1.1", | ||
"electrode-archetype-njs-module-dev": "^3.0.0", | ||
"electrode-react-webapp": "^3.8.9", | ||
"jsdom": "^15.2.1", | ||
"mock-require": "^1.3.0", | ||
"run-verify": "^1.2.2", | ||
"subapp-pkg-util": "../subapp-pkg-util", | ||
"electrode-react-webapp": "^3.8.9" | ||
"subapp-pkg-util": "../subapp-pkg-util" | ||
}, | ||
@@ -59,2 +63,5 @@ "fyn": { | ||
"subapp-util": "../subapp-util" | ||
}, | ||
"devDependencies": { | ||
"@xarc/app": "../xarc-app" | ||
} | ||
@@ -61,0 +68,0 @@ }, |
174563
39
2297
10
15