-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from NerdWalletOSS/clangen/fix-release-preloads
Fix preload module loading in release builds.
- Loading branch information
Showing
5 changed files
with
97 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
#!/bin/bash | ||
yarn install-patch | ||
yarn forwarder:build | ||
rm public/preload-manifest.js | ||
rm public/preload-module-*.js | ||
node script/collect-preload-scripts.js | ||
yarn build | ||
yarn package |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
const path = require('path'); | ||
const crypto = require('crypto'); | ||
const fs = require('fs'); | ||
|
||
console.log('\n\ncollecting preload scripts and generating manifest...\n\n'); | ||
|
||
const preloadsModuleFn = path.join( | ||
__dirname, | ||
'..', | ||
'src/App/Plugins/preloads.js' | ||
); | ||
|
||
const outDir = path.join(__dirname, '..', 'public'); | ||
const outManifestFn = path.normalize(`${outDir}/preload-manifest.js`); | ||
|
||
const MANIFEST_TEMPLATE = `/* this is an automatically generated file, DO NOT MODIFY! */ | ||
exports.default = [{{MODULES}}];`; | ||
|
||
/* eslint-disable-next-line import/no-dynamic-require */ | ||
const preloadFilenames = require(preloadsModuleFn).default(); | ||
const outputFilenames = new Set([]); | ||
|
||
console.log(preloadsModuleFn, preloadFilenames); | ||
|
||
const rmFile = (fn) => { | ||
if (fs.existsSync(fn)) { | ||
try { | ||
fs.rmSync(fn); | ||
} catch (e) {} | ||
try { | ||
fs.unlinkSync(fn); | ||
} catch (e) {} | ||
} | ||
}; | ||
|
||
const copyFile = (inFn) => { | ||
const hash = crypto | ||
.createHash('sha256') | ||
.update(inFn) | ||
.digest('hex') | ||
.toString(); | ||
const leaf = `preload-module-${hash}.js`; | ||
const outFn = path.normalize(`${outDir}/${leaf}`); | ||
rmFile(outFn); | ||
outputFilenames.add(`'./${leaf}'`); | ||
fs.copyFileSync(inFn, outFn); | ||
}; | ||
|
||
for (let i = 0; i < preloadFilenames.length; i++) { | ||
copyFile(preloadFilenames[i]); | ||
} | ||
|
||
const manifestEntries = Array.from(outputFilenames).join(', '); | ||
const manifestDocument = MANIFEST_TEMPLATE.replace( | ||
'{{MODULES}}', | ||
manifestEntries | ||
); | ||
rmFile(outManifestFn); | ||
fs.writeFileSync(outManifestFn, manifestDocument); | ||
|
||
console.log( | ||
`\n\nwrote manifest to ${outManifestFn} with scripts: ${manifestEntries}\n\n` | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters