bufbuild / protobuf-es

Protocol Buffers for ECMAScript. The only JavaScript Protobuf library that is fully-compliant with Protobuf conformance tests.
Apache License 2.0
1.16k stars 69 forks source link

Support target prefix in plugin option rewrite_imports #998

Closed timostamm closed 3 weeks ago

timostamm commented 3 weeks ago

With the plugin option rewrite_imports, it's possible to modify generated import paths - more details in https://github.com/bufbuild/protobuf-es/issues/947#issuecomment-2257889255.

It currently isn't possible to specify a target package with a colon:

protoc-gen-es: invalid option "rewrite_imports=@bufbuild/protobuf:npm:@bufbuild/protobuf": must be in the form of <pattern>:<target>

This relaxes the check to accept one or more colons in the target. As a result, it is now possible to add an "npm:" specifier to generated imports from @bufbuild/protobuf for Deno:

version: v2
plugins:
  - local: protoc-gen-es
    opt:
      - target=ts
      - rewrite_imports=@bufbuild/protobuf:npm:@bufbuild/protobuf
      - rewrite_imports=@bufbuild/protobuf/codegenv1:npm:@bufbuild/protobuf/codegenv1
    out: src/gen

This generates imports with the prefixes:

import { fileDesc, messageDesc } from "npm:@bufbuild/protobuf/codegenv1";
import type { Message } from "npm:@bufbuild/protobuf";
...

Closes https://github.com/bufbuild/protobuf-es/issues/992.