diff --git a/packages/protobuf/package.json b/packages/protobuf/package.json index e2e34ae3c..01989fb95 100644 --- a/packages/protobuf/package.json +++ b/packages/protobuf/package.json @@ -10,10 +10,9 @@ }, "scripts": { "clean": "rm -rf ./dist/*", - "build": "npm run build:cjs && npm run build:esm && npm run build:proxy", + "build": "npm run build:cjs && npm run build:esm", "build:cjs": "../../node_modules/typescript/bin/tsc --project tsconfig.json --module commonjs --outDir ./dist/cjs --declaration --declarationDir ./dist/cjs && echo >./dist/cjs/package.json '{\"type\":\"commonjs\"}'", "build:esm": "../../node_modules/typescript/bin/tsc --project tsconfig.json --module ES2015 --verbatimModuleSyntax --outDir ./dist/esm --declaration --declarationDir ./dist/esm", - "build:proxy": "node ../../scripts/gen-esm-proxy.mjs .", "bootstrap:featureset-defaults": "upstream-inject-feature-defaults src/private/feature-set.ts", "prebootstrap:wkt": "rm -rf .tmp && mkdir -p .tmp/google/protobuf && cp -rp src/google/protobuf/* .tmp/google/protobuf", "bootstrap:wkt": "protoc --es_out=src --es_opt=bootstrap_wkt=true,ts_nocheck=false,target=ts --proto_path $(upstream-include wkt) $(upstream-files wkt) && license-header src/google/protobuf", @@ -25,11 +24,6 @@ "main": "./dist/cjs/index.js", "exports": { ".": { - "node": { - "import": "./dist/proxy/index.js", - "require": "./dist/cjs/index.js" - }, - "module": "./dist/esm/index.js", "import": "./dist/esm/index.js", "require": "./dist/cjs/index.js" } diff --git a/packages/protobuf/src/create-descriptor-set.ts b/packages/protobuf/src/create-descriptor-set.ts index d51d62385..38d91def6 100644 --- a/packages/protobuf/src/create-descriptor-set.ts +++ b/packages/protobuf/src/create-descriptor-set.ts @@ -55,7 +55,7 @@ import type { BinaryReadOptions, BinaryWriteOptions } from "./binary-format.js"; import type { FeatureResolverFn } from "./private/feature-set.js"; import { createFeatureResolver } from "./private/feature-set.js"; import { LongType, ScalarType } from "./scalar.js"; -import { isMessage } from "./is-message"; +import { isMessage } from "./is-message.js"; /** * Create a DescriptorSet, a convenient interface for working with a set of diff --git a/packages/protobuf/src/create-registry-from-desc.ts b/packages/protobuf/src/create-registry-from-desc.ts index 569a657bf..10ace8525 100644 --- a/packages/protobuf/src/create-registry-from-desc.ts +++ b/packages/protobuf/src/create-registry-from-desc.ts @@ -66,7 +66,7 @@ import type { import { createDescriptorSet } from "./create-descriptor-set.js"; import type { Extension } from "./extension.js"; import type { ExtensionFieldSource } from "./private/extensions.js"; -import { isMessage } from "./is-message"; +import { isMessage } from "./is-message.js"; // well-known message types with specialized JSON representation const wkMessages = [ diff --git a/packages/protobuf/src/private/binary-format.ts b/packages/protobuf/src/private/binary-format.ts index 132323343..069f700c4 100644 --- a/packages/protobuf/src/private/binary-format.ts +++ b/packages/protobuf/src/private/binary-format.ts @@ -27,7 +27,7 @@ import { assert } from "./assert.js"; import { isFieldSet } from "./reflect.js"; import type { ScalarValue } from "../scalar.js"; import { LongType, ScalarType } from "../scalar.js"; -import { isMessage } from "../is-message"; +import { isMessage } from "../is-message.js"; /* eslint-disable prefer-const,no-case-declarations,@typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-argument,@typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-return */ diff --git a/packages/protobuf/src/private/field-wrapper.ts b/packages/protobuf/src/private/field-wrapper.ts index 9c399e8fc..16facfbcf 100644 --- a/packages/protobuf/src/private/field-wrapper.ts +++ b/packages/protobuf/src/private/field-wrapper.ts @@ -16,7 +16,7 @@ import { Message } from "../message.js"; import type { MessageType } from "../message-type.js"; import type { DescExtension, DescField } from "../descriptor-set.js"; import { ScalarType } from "../scalar.js"; -import { isMessage } from "../is-message"; +import { isMessage } from "../is-message.js"; /* eslint-disable @typescript-eslint/no-explicit-any -- unknown fields are represented with any */ diff --git a/packages/protobuf/src/private/json-format.ts b/packages/protobuf/src/private/json-format.ts index f5790e937..0ee056f52 100644 --- a/packages/protobuf/src/private/json-format.ts +++ b/packages/protobuf/src/private/json-format.ts @@ -45,7 +45,7 @@ import { scalarZeroValue } from "./scalars.js"; import { isScalarZeroValue } from "./scalars.js"; import type { ScalarValue } from "../scalar.js"; import { LongType, ScalarType } from "../scalar.js"; -import { isMessage } from "../is-message"; +import { isMessage } from "../is-message.js"; /* eslint-disable no-case-declarations,@typescript-eslint/no-unsafe-argument,@typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-call */ diff --git a/packages/protobuf/src/private/util-common.ts b/packages/protobuf/src/private/util-common.ts index e3c0126fc..c2e1a5400 100644 --- a/packages/protobuf/src/private/util-common.ts +++ b/packages/protobuf/src/private/util-common.ts @@ -19,7 +19,7 @@ import type { MessageType } from "../message-type.js"; import type { Util } from "./util.js"; import { scalarEquals } from "./scalars.js"; import { ScalarType } from "../scalar.js"; -import { isMessage } from "../is-message"; +import { isMessage } from "../is-message.js"; /* eslint-disable @typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-return,@typescript-eslint/no-unsafe-argument,no-case-declarations */ diff --git a/packages/protobuf/src/to-plain-message.ts b/packages/protobuf/src/to-plain-message.ts index d4bac975f..fca3f9a5c 100644 --- a/packages/protobuf/src/to-plain-message.ts +++ b/packages/protobuf/src/to-plain-message.ts @@ -16,7 +16,7 @@ import { Message } from "./message.js"; import type { AnyMessage, PlainMessage } from "./message.js"; -import { isMessage } from "./is-message"; +import { isMessage } from "./is-message.js"; /** * toPlainMessage returns a new object by stripping diff --git a/packages/protoplugin/package.json b/packages/protoplugin/package.json index f71bbd205..dc9c8fe07 100644 --- a/packages/protoplugin/package.json +++ b/packages/protoplugin/package.json @@ -10,10 +10,9 @@ }, "scripts": { "clean": "rm -rf ./dist/*", - "build": "npm run build:cjs && npm run build:esm && npm run build:proxy", + "build": "npm run build:cjs && npm run build:esm", "build:cjs": "../../node_modules/typescript/bin/tsc --project tsconfig.json --module commonjs --outDir ./dist/cjs --declaration --declarationDir ./dist/cjs && echo >./dist/cjs/package.json '{\"type\":\"commonjs\"}'", "build:esm": "../../node_modules/typescript/bin/tsc --project tsconfig.json --module ES2015 --outDir ./dist/esm --declaration --declarationDir ./dist/esm", - "build:proxy": "node ../../scripts/gen-esm-proxy.mjs . ecmascript", "attw": "attw --pack" }, "type": "module", @@ -21,20 +20,10 @@ "main": "./dist/cjs/index.js", "exports": { ".": { - "node": { - "import": "./dist/proxy/index.js", - "require": "./dist/cjs/index.js" - }, - "module": "./dist/esm/index.js", "import": "./dist/esm/index.js", "require": "./dist/cjs/index.js" }, "./ecmascript": { - "node": { - "import": "./dist/proxy/ecmascript/index.js", - "require": "./dist/cjs/ecmascript/index.js" - }, - "module": "./dist/esm/ecmascript/index.js", "import": "./dist/esm/ecmascript/index.js", "require": "./dist/cjs/ecmascript/index.js" } diff --git a/packages/protoplugin/src/ecmascript/generated-file.ts b/packages/protoplugin/src/ecmascript/generated-file.ts index 1c0e7e7b8..c0bd31479 100644 --- a/packages/protoplugin/src/ecmascript/generated-file.ts +++ b/packages/protoplugin/src/ecmascript/generated-file.ts @@ -34,7 +34,7 @@ import { LiteralString, RefDescEnum, RefDescMessage, -} from "./opaque-printables"; +} from "./opaque-printables.js"; /** * All types that can be passed to GeneratedFile.print() diff --git a/scripts/gen-esm-proxy.mjs b/scripts/gen-esm-proxy.mjs deleted file mode 100644 index 249953fbe..000000000 --- a/scripts/gen-esm-proxy.mjs +++ /dev/null @@ -1,117 +0,0 @@ -// Copyright 2021-2024 Buf Technologies, Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"; -import * as path from "node:path"; -import { exit, stderr, stdout, argv } from "node:process"; - -const args = argv.slice(2); - -if (args.length === 0) { - stdout.write(`USAGE: ${path.basename(argv[1])} [...path] -path: One or more subpath exports. A dot (.) is valid. - -Generates ESM wrappers for dual packages. - -Example: - - ${path.basename(argv[1])} . foo - -Assumes that the following CJS artifacts exist: - - dist/cjs - ├── index.d.ts - ├── index.js - └── foo - ├── index.d.ts - └── index.js - -Generates the following ESM wrappers: - - dist/proxy - ├── index.d.ts - ├── index.js - └── foo - ├── index.d.ts - └── index.js - -package.json must contain: - - "type": "module", - "exports": { - ".": { - "require": "./dist/cjs/index.js", - "import": "./dist/proxy/index.js" - }, - "./protocol": { - "require": "./dist/cjs/foo/index.js", - "import": "./dist/proxy/foo/index.js" - }, - -Known limitations: -- expects CJS files with a .js extension, not .cjs -- does not support default exports -- supports only simple subpath directory exports, assuming a index.js file -- does not support export patterns -`); - exit(1); -} - -const cjsDist = "dist/cjs"; -const proxyDist = "dist/proxy"; - -const packageType = readPackageJsonType(); -if (packageType !== "module") { - stderr.write(`package.json is missing "type": "module" field.\n`); - exit(1); -} - -for (const subpath of args) { - const cjsPath = path.join(cjsDist, subpath, "index.js"); - if (!existsSync(cjsPath)) { - stderr.write( - `CommonJS artifact for subpath "${subpath}" not found at expected path ${cjsPath}\n`, - ); - exit(1); - } - const proxyDir = path.join(proxyDist, subpath); - if (!existsSync(proxyDir)) { - mkdirSync(proxyDir, { recursive: true }); - } - const cjsImportPath = path.relative(proxyDir, cjsPath); - writeFileSync( - path.join(proxyDir, "index.js"), - `export * from "${cjsImportPath}";\n`, - ); - writeFileSync( - path.join(proxyDir, "index.d.ts"), - `export * from "${cjsImportPath}";\n`, - ); -} - -/** - * @return {"commonjs"|"module"} - */ -function readPackageJsonType() { - const pkgString = readFileSync("package.json", "utf-8"); - const pkg = JSON.parse(pkgString); - const t = pkg.type; - if (typeof t !== "string") { - return "commonjs"; - } - if (t.toLowerCase() === "module") { - return "module"; - } - return "commonjs"; -}