Skip to content

Commit

Permalink
Merge pull request #587 from agoric-labs/esm-compat
Browse files Browse the repository at this point in the history
feat: ESM compatibility
  • Loading branch information
Zetazzz committed Mar 18, 2024
2 parents 565c926 + 7901482 commit b953e4a
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 5 deletions.
3 changes: 3 additions & 0 deletions packages/telescope/src/generators/create-bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { TelescopeBuilder } from '../builder';
import {
recursiveModuleBundle
} from '@cosmology/ast';
import { restoreJsExtension } from 'src/utils';

export const plugin = (
builder: TelescopeBuilder,
Expand All @@ -13,6 +14,8 @@ export const plugin = (
return;
}

restoreJsExtension(bundler.bundle.importPaths);

// [x] bundle
const body = recursiveModuleBundle(builder.options, bundler.bundle.bundleVariables);
const prog = []
Expand Down
6 changes: 5 additions & 1 deletion packages/telescope/src/generators/create-stargate-clients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { ProtoRef } from '@cosmology/types';
import { camel, pascal } from 'case';
import { variableSlug } from '@cosmology/utils';
import { buildAllImportsFromGenericContext } from '../imports';
import { restoreJsExtension } from 'src/utils';

export const plugin = (
builder: TelescopeBuilder,
Expand Down Expand Up @@ -93,7 +94,10 @@ export const plugin = (

const imports = buildAllImportsFromGenericContext(ctx, clientFile);

let cProg = [...imports, ...registryImports, ...converterImports]
const importDecls = [...imports, ...registryImports, ...converterImports];
restoreJsExtension(importDecls);

let cProg = importDecls
.concat(aminos)
.concat(protos)
.concat(clientOptions)
Expand Down
4 changes: 2 additions & 2 deletions packages/telescope/src/helpers/binary-coder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const binary = `
// standalone and requires a support library to be linked with it. This
// support library is itself covered by the above license.
import { utf8Length, utf8Read, utf8Write } from "./utf8";
import { utf8Length, utf8Read, utf8Write } from "./utf8.js";
import {
int64ToString,
readInt32,
Expand All @@ -49,7 +49,7 @@ import {
writeByte,
zzDecode,
zzEncode,
} from "./varint";
} from "./varint.js";
export enum WireType {
Varint = 0,
Expand Down
3 changes: 2 additions & 1 deletion packages/telescope/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { ImportDeclaration } from '@babel/types';
import { ProtoServiceMethod, TelescopeOptions } from '@cosmology/types';
export interface Bundle {
bundleVariables: {};
bundleFile: string;
importPaths: any[];
importPaths: ImportDeclaration[];
base: string;
}
export interface BundlerFile {
Expand Down
17 changes: 16 additions & 1 deletion packages/telescope/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ProtoRoot, ProtoRef } from '@cosmology/types';
import { relative, dirname, extname } from 'path';
import { ImportObj } from '../types';
import { ImportDeclaration } from '@babel/types';

export const getRoot = (ref: ProtoRef): ProtoRoot => {
if (ref.traversed) return ref.traversed;
Expand Down Expand Up @@ -117,7 +118,21 @@ export const getRelativePath = (f1: string, f2: string) => {
const rel = relative(dirname(f1), f2);
let importPath = rel.replace(extname(rel), '');
if (!/^\./.test(importPath)) importPath = `./${importPath}`;
return importPath;
return `${importPath}.js`;
}

/**
* Add .js extension to relative imports for compatibility with ESM
*/
export const restoreJsExtension = (importDeclarations: ImportDeclaration[]) => {
for (const stmt of importDeclarations) {
if (
stmt.source.value.startsWith(".") &&
!stmt.source.value.endsWith(".js")
) {
stmt.source.value += ".js";
}
}
}

export * from './common-create-bundle';

0 comments on commit b953e4a

Please sign in to comment.