-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(contractStarter): build core-eval script / permit with rollup
- main - emitPermit
- Loading branch information
Showing
4 changed files
with
120 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* @file rollup configuration to bundle core-eval script | ||
* | ||
* Supports developing core-eval script, permit as a module: | ||
* - import { E } from '@endo/far' | ||
* We can strip this declaration during bundling | ||
* since the core-eval scope includes exports of @endo/far | ||
* - `bundleID = ...` is replaced using updated/cached bundle hash | ||
* - `main` export is appended as script completion value | ||
* - `permit` export is emitted as JSON | ||
*/ | ||
// @ts-check | ||
import { | ||
coreEvalGlobals, | ||
moduleToScript, | ||
configureBundleID, | ||
emitPermit, | ||
} from './tools/rollup-plugin-core-eval.js'; | ||
import { permit } from './src/start-contractStarter.js'; | ||
|
||
/** @type {import('rollup').RollupOptions} */ | ||
const config = { | ||
output: { | ||
globals: coreEvalGlobals, | ||
file: 'bundles/deploy-starter.js', | ||
format: 'es', | ||
footer: 'main', | ||
}, | ||
external: ['@endo/far'], | ||
plugins: [ | ||
configureBundleID({ | ||
name: 'contractStarter', | ||
rootModule: './src/contractStarter.js', | ||
cache: 'bundles', | ||
}), | ||
moduleToScript(), | ||
emitPermit({ permit, file: 'deploy-starter-permit.json' }), | ||
], | ||
}; | ||
export default config; |
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// @ts-check | ||
import '@endo/init'; | ||
import { makeNodeBundleCache } from '@endo/bundle-source/cache.js'; | ||
export const coreEvalGlobals = { | ||
E: 'E', | ||
Far: 'Far', | ||
}; | ||
|
||
const redactImportDecls = txt => | ||
txt.replace(/^\s*import\b\s*(.*)/gm, '// XMPORT: $1'); | ||
const omitExportKewords = txt => txt.replace(/^\s*export\b\s*/gm, ''); | ||
// cf. ses rejectImportExpressions | ||
// https://github.com/endojs/endo/blob/ebc8f66e9498f13085a8e64e17fc2f5f7b528faa/packages/ses/src/transforms.js#L143 | ||
const hideImportExpr = txt => txt.replace(/\bimport\b/g, 'XMPORT'); | ||
|
||
export const moduleToScript = () => ({ | ||
name: 'module-to-script', | ||
generateBundle: (_opts, bundle, _isWrite) => { | ||
for (const fileName of Object.keys(bundle)) { | ||
bundle[fileName].code = hideImportExpr( | ||
redactImportDecls(omitExportKewords(bundle[fileName].code)), | ||
); | ||
} | ||
}, | ||
}); | ||
|
||
export const configureBundleID = ({ name, rootModule, cache }) => { | ||
const pattern = new RegExp(`bundleID\\b = Fail.*`, 'g'); | ||
const bundleCacheP = makeNodeBundleCache(cache, {}, s => import(s)); | ||
return { | ||
name: 'configureBundleID', | ||
transform: async (code, id) => { | ||
const bundle = await bundleCacheP.then(c => c.load(rootModule, name)); | ||
const revised = code.replace( | ||
pattern, | ||
`bundleID = ${JSON.stringify(`b1-${bundle.endoZipBase64Sha512}`)},`, | ||
); | ||
if (revised === code) return null; | ||
return { code: revised }; | ||
}, | ||
}; | ||
}; | ||
|
||
export const emitPermit = ({ permit, file }) => ({ | ||
name: 'emit-permit', | ||
generateBundle(_opts, _bundle) { | ||
console.log('@@@emit permit'); | ||
this.emitFile({ | ||
type: 'asset', | ||
fileName: file, | ||
source: JSON.stringify(permit, null, 2), | ||
}); | ||
}, | ||
}); |