rewrite: add makeTransformRewriter
MODIFY
src/index.js
src/index.js
export { makeRootedResolver } from './resolve';
export { makeSuffixLocator } from './locate';
export { makeProtocolRetriever } from './retrieve';
+export { makeTransformRewriter } from './rewrite';
export { makeEvaluateLinker } from './link';
export { makeImporter as default } from './pipeline';

ADD
src/rewrite.js
+// This rewriter accepts a boxed transform-module because
+// we need to tie the knot between make-importer and
+// transform-module.
+export const makeTransformRewriter = boxedTransformModule => {
+ return (moduleSource, moduleId) => {
+ const [{ rewrite }] = boxedTransformModule;
+ const rs = rewrite({
+ endowments: {},
+ sourceType: 'module',
+ src: moduleSource,
+ url: moduleId,
+ });
+ return rs;
+ };
+};

ADD
test/test-rewrite.js
+/* eslint-disable import/no-extraneous-dependencies */
+import { test } from 'tape-promise/tape';
+
+import { makeTransformRewriter } from '../src';
+
+test('transform rewriter', async t => {
+ try {
+ const boxedTransform = [];
+ const rewrite = makeTransformRewriter(boxedTransform);
+ boxedTransform[0] = {
+ rewrite(rs) {
+ t.equal(typeof rewrite, 'function', 'rewrite is a function');
+ t.equal(rs.sourceType, 'module', 'sourceType is a module');
+ rs.endowments.hImport = async mod => `imported:${mod}`;
+ rs.src = rs.src.replace(/import/g, 'hImport');
+ return rs;
+ },
+ };
+ const rs = rewrite(`import('foo')`, '@example/foo');
+ t.equal(
+ await rs.endowments.hImport('something'),
+ 'imported:something',
+ `endowments work`,
+ );
+ t.equal(rs.src, `hImport('foo')`, 'rewrite works');
+ // eslint-disable-next-line no-eval
+ const ret = (1, eval)(`({ hImport }) => (${rs.src}\n)`)(rs.endowments);
+ t.equal(await ret, `imported:foo`, `combination works`);
+ } catch (e) {
+ t.isNot(e, e, 'unexpected exception');
+ } finally {
+ t.end();
+ }
+});

rewrite: add makeTransformRewriter