Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add enable_import_extensions option #233

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.idea
/.vscode
.DS_Store
/node_modules
15 changes: 11 additions & 4 deletions packages/plugin-framework/src/typescript-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import {TypescriptFile} from "./typescript-file";
export class TypeScriptImports {

private readonly symbols: SymbolTable;
private readonly enable_import_extensions: boolean;


constructor(symbols: SymbolTable) {
constructor(symbols: SymbolTable, enable_import_extensions: boolean) {
this.symbols = symbols;
this.enable_import_extensions = enable_import_extensions;
}


Expand Down Expand Up @@ -76,7 +77,8 @@ export class TypeScriptImports {
// add an import statement
const importPath = createRelativeImportPath(
source.getSourceFile().fileName,
symbolReg.file.getFilename()
symbolReg.file.getFilename(),
this.enable_import_extensions
);
const blackListedNames = this.symbols.list(source).map(e => e.name);
return ensureNamedImportPresent(
Expand Down Expand Up @@ -285,7 +287,7 @@ export function findNamedImports(sourceFile: ts.SourceFile): { name: string, as:
* Create a relative path for an import statement like
* `import {Foo} from "./foo"`
*/
function createRelativeImportPath(currentPath: string, pathToImportFrom: string): string {
function createRelativeImportPath(currentPath: string, pathToImportFrom: string, addExtension: boolean): string {
// create relative path to the file to import
let fromPath = path.relative(path.dirname(currentPath), pathToImportFrom);

Expand All @@ -302,5 +304,10 @@ function createRelativeImportPath(currentPath: string, pathToImportFrom: string)
if (!fromPath.startsWith('../') && !fromPath.startsWith('./')) {
fromPath = './' + fromPath;
}

// add .js extensions on import statements for ESM compatibility with typescript and nodejs
if (addExtension === true) {
fromPath = fromPath + ".js";
}
return fromPath;
}
4 changes: 3 additions & 1 deletion packages/plugin/src/our-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ export interface InternalOptions {
readonly transpileTarget: ts.ScriptTarget | undefined,
readonly transpileModule: ts.ModuleKind,
readonly addPbSuffix: boolean;
readonly enable_import_extensions: boolean;
}

export function makeInternalOptions(
Expand Down Expand Up @@ -232,6 +233,7 @@ export function makeInternalOptions(
output_javascript_es2019: boolean,
output_javascript_es2020: boolean,
output_legacy_commonjs: boolean,
enable_import_extensions: boolean,
},
pluginCredit?: string,
): InternalOptions {
Expand Down Expand Up @@ -260,6 +262,7 @@ export function makeInternalOptions(
transpileTarget: undefined,
transpileModule: ts.ModuleKind.ES2015,
addPbSuffix: false,
enable_import_extensions: true,
},
) as Writeable<InternalOptions>;
if (pluginCredit) {
Expand Down Expand Up @@ -418,4 +421,3 @@ export class OptionResolver {
}

}

7 changes: 6 additions & 1 deletion packages/plugin/src/protobufts-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ export class ProtobuftsPlugin extends PluginBase {
excludes: ["output_typescript"]
},

// Add file extension for ESM compatibility with node
enable_import_extensions: {
description: "Use .js extension for ESM import statements",
},

// client
client_none: {
description: "Do not generate rpc clients. \n" +
Expand Down Expand Up @@ -216,7 +221,7 @@ export class ProtobuftsPlugin extends PluginBase {
registry = DescriptorRegistry.createFrom(request),
symbols = new SymbolTable(),
fileTable = new FileTable(),
imports = new TypeScriptImports(symbols),
imports = new TypeScriptImports(symbols, options.enable_import_extensions),
comments = new CommentGenerator(registry),
interpreter = new Interpreter(registry, options),
optionResolver = new OptionResolver(interpreter, registry, options),
Expand Down