From a0ba45a50ae94d52de1a490f80ea8df58e1d8434 Mon Sep 17 00:00:00 2001 From: Steve Ayers Date: Wed, 20 Mar 2024 10:12:29 -0400 Subject: [PATCH] Build ESM artifacts with Node16 moduleResolution (#754) Fixes #599 This switches all packages to build ESM artifacts with Node16 module resolution. In doing so, it turns on Node16 for both `moduleResolution` and `module` by default throughout the repo. It also turns on `verbatimModuleSyntax` by default (which is why the need for all the `import type {...}` changes). For CJS artifacts, the exceptions for `moduleResolution node10` and `verbatimModuleSyntax false` are now specified in the command script. --- packages/protobuf-conformance/package.json | 2 +- packages/protobuf-test/package.json | 2 +- .../protobuf-test/src/create-registry.test.ts | 2 +- packages/protobuf/package.json | 4 ++-- packages/protoc-gen-es/package.json | 2 +- packages/protoplugin-test/package.json | 2 +- .../src/file-es_proto_int64.test.ts | 2 +- .../src/file-es_ref_enum.test.ts | 2 +- .../src/file-es_ref_message.test.ts | 2 +- .../protoplugin-test/src/file-es_string.test.ts | 2 +- packages/protoplugin/ecmascript/index.js | 17 +++++++++++++++++ packages/protoplugin/package.json | 4 ++-- packages/protoplugin/src/create-es-plugin.ts | 3 ++- .../src/ecmascript/generated-file.ts | 8 +++----- .../protoplugin/src/ecmascript/import-path.ts | 3 ++- packages/protoplugin/src/ecmascript/index.ts | 10 +++------- .../src/ecmascript/legacy-custom-options.ts | 10 ++-------- .../src/ecmascript/legacy-gencommon.ts | 9 ++------- .../src/ecmascript/opaque-printables.ts | 9 ++------- .../protoplugin/src/ecmascript/parameter.ts | 4 ++-- packages/protoplugin/src/ecmascript/schema.ts | 8 +++++--- packages/protoplugin/src/index.ts | 4 ++-- packages/upstream-protobuf/package.json | 3 ++- tsconfig.base.json | 5 +++-- 24 files changed, 60 insertions(+), 59 deletions(-) create mode 100644 packages/protoplugin/ecmascript/index.js diff --git a/packages/protobuf-conformance/package.json b/packages/protobuf-conformance/package.json index 218ec9f3c..a95f51fcb 100644 --- a/packages/protobuf-conformance/package.json +++ b/packages/protobuf-conformance/package.json @@ -4,7 +4,7 @@ "scripts": { "clean": "rm -rf ./dist/cjs/* ./dist/types/*", "build": "npm run build:esm+types", - "build:esm+types": "../../node_modules/typescript/bin/tsc --project tsconfig.json --module ES2015 --verbatimModuleSyntax --outDir ./dist/esm --declaration --declarationDir ./dist/types", + "build:esm+types": "../../node_modules/typescript/bin/tsc --project tsconfig.json --outDir ./dist/esm --declaration --declarationDir ./dist/types", "pregenerate": "rm -rf src/gen/*", "generate": "protoc --es_out=src/gen --es_opt=ts_nocheck=false,target=ts --proto_path=$(upstream-include conformance) $(upstream-files conformance)", "postgenerate": "license-header src/gen", diff --git a/packages/protobuf-test/package.json b/packages/protobuf-test/package.json index e4bc30fbc..ebaab5ff1 100644 --- a/packages/protobuf-test/package.json +++ b/packages/protobuf-test/package.json @@ -4,7 +4,7 @@ "scripts": { "clean": "rm -rf ./dist/cjs/* ./dist/esm/* ./dist/types/*", "build": "npm run build:esm+types && npm run build:copy-gen-js", - "build:esm+types": "../../node_modules/typescript/bin/tsc --project tsconfig.json --module ES2015 --verbatimModuleSyntax --outDir ./dist/esm --declaration --declarationDir ./dist/types", + "build:esm+types": "../../node_modules/typescript/bin/tsc --project tsconfig.json --outDir ./dist/esm --declaration --declarationDir ./dist/types", "build:copy-gen-js": "rsync -a --exclude '*.js' src/gen/js dist/types/gen && rsync -a --exclude '*.d.ts' src/gen/js dist/esm/gen", "pregenerate": "rm -rf src/gen/*/* descriptorset.*", "generate": "npm run generate:ts && npm run generate:js && npm run generate:wkt:ts && npm run generate:wkt:js && npm run generate:desc", diff --git a/packages/protobuf-test/src/create-registry.test.ts b/packages/protobuf-test/src/create-registry.test.ts index 0fc219b20..bc3ed57bc 100644 --- a/packages/protobuf-test/src/create-registry.test.ts +++ b/packages/protobuf-test/src/create-registry.test.ts @@ -31,7 +31,7 @@ import { Proto2ExtEnum, Proto2ExtMessage, uint32_ext, -} from "./gen/ts/extra/extensions-proto2_pb"; +} from "./gen/ts/extra/extensions-proto2_pb.js"; import { TestAllTypes, TestAllTypes_NestedEnum, diff --git a/packages/protobuf/package.json b/packages/protobuf/package.json index d5deea522..5abf1837f 100644 --- a/packages/protobuf/package.json +++ b/packages/protobuf/package.json @@ -11,8 +11,8 @@ "scripts": { "clean": "rm -rf ./dist/*", "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:cjs": "../../node_modules/typescript/bin/tsc --project tsconfig.json --module commonjs --verbatimModuleSyntax false --moduleResolution node10 --outDir ./dist/cjs --declaration --declarationDir ./dist/cjs && echo >./dist/cjs/package.json '{\"type\":\"commonjs\"}'", + "build:esm": "../../node_modules/typescript/bin/tsc --project tsconfig.json --outDir ./dist/esm --declaration --declarationDir ./dist/esm", "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", diff --git a/packages/protoc-gen-es/package.json b/packages/protoc-gen-es/package.json index 3ac01964b..5a604ca60 100644 --- a/packages/protoc-gen-es/package.json +++ b/packages/protoc-gen-es/package.json @@ -16,7 +16,7 @@ }, "scripts": { "clean": "rm -rf ./dist/cjs/*", - "build": "../../node_modules/typescript/bin/tsc --project tsconfig.json --module commonjs --outDir ./dist/cjs" + "build": "../../node_modules/typescript/bin/tsc --project tsconfig.json --module commonjs --verbatimModuleSyntax false --moduleResolution node10 --outDir ./dist/cjs" }, "preferUnplugged": true, "dependencies": { diff --git a/packages/protoplugin-test/package.json b/packages/protoplugin-test/package.json index 3efcb1d1c..d700e3ccf 100644 --- a/packages/protoplugin-test/package.json +++ b/packages/protoplugin-test/package.json @@ -4,7 +4,7 @@ "scripts": { "clean": "rm -rf ./dist/cjs/* ./dist/esm/* ./dist/types/*", "build": "npm run build:esm+types", - "build:esm+types": "../../node_modules/typescript/bin/tsc --project tsconfig.json --module ES2015 --verbatimModuleSyntax --outDir ./dist/esm --declaration --declarationDir ./dist/types", + "build:esm+types": "../../node_modules/typescript/bin/tsc --project tsconfig.json --outDir ./dist/esm --declaration --declarationDir ./dist/types", "pregenerate": "rm -rf src/gen/*/*", "generate": "protoc --es_out=src/gen --es_opt=ts_nocheck=false,target=ts --proto_path=proto option-enum.proto option-message.proto file-option.proto", "postgenerate": "license-header src/gen", diff --git a/packages/protoplugin-test/src/file-es_proto_int64.test.ts b/packages/protoplugin-test/src/file-es_proto_int64.test.ts index 66131c678..4878805cf 100644 --- a/packages/protoplugin-test/src/file-es_proto_int64.test.ts +++ b/packages/protoplugin-test/src/file-es_proto_int64.test.ts @@ -13,7 +13,7 @@ // limitations under the License. import { describe, expect, test } from "@jest/globals"; -import { createTestPluginAndRun } from "./helpers"; +import { createTestPluginAndRun } from "./helpers.js"; import type { GeneratedFile, Schema } from "@bufbuild/protoplugin/ecmascript"; import { LongType, ScalarType } from "@bufbuild/protobuf"; diff --git a/packages/protoplugin-test/src/file-es_ref_enum.test.ts b/packages/protoplugin-test/src/file-es_ref_enum.test.ts index c8f3a3143..81eee009c 100644 --- a/packages/protoplugin-test/src/file-es_ref_enum.test.ts +++ b/packages/protoplugin-test/src/file-es_ref_enum.test.ts @@ -13,7 +13,7 @@ // limitations under the License. import { describe, expect, test } from "@jest/globals"; -import { createTestPluginAndRun } from "./helpers"; +import { createTestPluginAndRun } from "./helpers.js"; import type { GeneratedFile, Schema } from "@bufbuild/protoplugin/ecmascript"; describe("file print", () => { diff --git a/packages/protoplugin-test/src/file-es_ref_message.test.ts b/packages/protoplugin-test/src/file-es_ref_message.test.ts index 7c386b068..5db5b22e3 100644 --- a/packages/protoplugin-test/src/file-es_ref_message.test.ts +++ b/packages/protoplugin-test/src/file-es_ref_message.test.ts @@ -13,7 +13,7 @@ // limitations under the License. import { describe, expect, test } from "@jest/globals"; -import { createTestPluginAndRun } from "./helpers"; +import { createTestPluginAndRun } from "./helpers.js"; import type { GeneratedFile, Schema } from "@bufbuild/protoplugin/ecmascript"; describe("file print", () => { diff --git a/packages/protoplugin-test/src/file-es_string.test.ts b/packages/protoplugin-test/src/file-es_string.test.ts index 2da6a7c3c..ef441a053 100644 --- a/packages/protoplugin-test/src/file-es_string.test.ts +++ b/packages/protoplugin-test/src/file-es_string.test.ts @@ -13,7 +13,7 @@ // limitations under the License. import { describe, expect, test } from "@jest/globals"; -import { createTestPluginAndRun } from "./helpers"; +import { createTestPluginAndRun } from "./helpers.js"; import type { GeneratedFile, Schema } from "@bufbuild/protoplugin/ecmascript"; describe("file print", () => { diff --git a/packages/protoplugin/ecmascript/index.js b/packages/protoplugin/ecmascript/index.js new file mode 100644 index 000000000..e37bae133 --- /dev/null +++ b/packages/protoplugin/ecmascript/index.js @@ -0,0 +1,17 @@ +// 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. + +// Workaround for bundlers that do not support subpath exports. +// eslint-disable-next-line no-undef +module.exports = require("../dist/cjs/ecmascript/index.js"); diff --git a/packages/protoplugin/package.json b/packages/protoplugin/package.json index 551e4e5b4..dc2369d87 100644 --- a/packages/protoplugin/package.json +++ b/packages/protoplugin/package.json @@ -11,8 +11,8 @@ "scripts": { "clean": "rm -rf ./dist/*", "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:cjs": "../../node_modules/typescript/bin/tsc --project tsconfig.json --module commonjs --verbatimModuleSyntax false --moduleResolution node10 --outDir ./dist/cjs --declaration --declarationDir ./dist/cjs && echo >./dist/cjs/package.json '{\"type\":\"commonjs\"}'", + "build:esm": "../../node_modules/typescript/bin/tsc --project tsconfig.json --outDir ./dist/esm --declaration --declarationDir ./dist/esm", "attw": "attw --pack" }, "type": "module", diff --git a/packages/protoplugin/src/create-es-plugin.ts b/packages/protoplugin/src/create-es-plugin.ts index e4131a71e..ce0c2895e 100644 --- a/packages/protoplugin/src/create-es-plugin.ts +++ b/packages/protoplugin/src/create-es-plugin.ts @@ -12,7 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { createSchema, Schema } from "./ecmascript/schema.js"; +import type { Schema } from "./ecmascript/schema.js"; +import { createSchema } from "./ecmascript/schema.js"; import type { FileInfo } from "./ecmascript/generated-file.js"; import type { Plugin } from "./plugin.js"; import { transpile } from "./ecmascript/transpile.js"; diff --git a/packages/protoplugin/src/ecmascript/generated-file.ts b/packages/protoplugin/src/ecmascript/generated-file.ts index c0bd31479..ab8ee3f5d 100644 --- a/packages/protoplugin/src/ecmascript/generated-file.ts +++ b/packages/protoplugin/src/ecmascript/generated-file.ts @@ -12,23 +12,21 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { +import type { AnyDesc, DescEnum, DescExtension, DescFile, DescMessage, - LongType, - protoInt64, - ScalarType, } from "@bufbuild/protobuf"; +import { LongType, protoInt64, ScalarType } from "@bufbuild/protobuf"; import type { ImportSymbol } from "./import-symbol.js"; import { createImportSymbol } from "./import-symbol.js"; import type { RuntimeImports } from "./runtime-imports.js"; import { makeImportPathRelative } from "./import-path.js"; import type { JSDocBlock } from "./jsdoc.js"; import { createJsDocBlock } from "./jsdoc.js"; -import { +import type { ExportDeclaration, LiteralProtoInt64, LiteralString, diff --git a/packages/protoplugin/src/ecmascript/import-path.ts b/packages/protoplugin/src/ecmascript/import-path.ts index 2d3069e2c..9e0f404bf 100644 --- a/packages/protoplugin/src/ecmascript/import-path.ts +++ b/packages/protoplugin/src/ecmascript/import-path.ts @@ -12,7 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { codegenInfo, DescFile } from "@bufbuild/protobuf"; +import type { DescFile } from "@bufbuild/protobuf"; +import { codegenInfo } from "@bufbuild/protobuf"; /** * A configuration for rewriting import paths, a feature mainly used for diff --git a/packages/protoplugin/src/ecmascript/index.ts b/packages/protoplugin/src/ecmascript/index.ts index a89da5a14..222448c9a 100644 --- a/packages/protoplugin/src/ecmascript/index.ts +++ b/packages/protoplugin/src/ecmascript/index.ts @@ -12,13 +12,9 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { - AnyDesc, - codegenInfo, - DescExtension, - DescFile, -} from "@bufbuild/protobuf"; -import { Printable } from "./generated-file.js"; +import type { AnyDesc, DescExtension, DescFile } from "@bufbuild/protobuf"; +import { codegenInfo } from "@bufbuild/protobuf"; +import type { Printable } from "./generated-file.js"; import { createJsDocBlock as createJsDocBlockInternal } from "./jsdoc.js"; export { reifyWkt } from "./reify-wkt.js"; diff --git a/packages/protoplugin/src/ecmascript/legacy-custom-options.ts b/packages/protoplugin/src/ecmascript/legacy-custom-options.ts index d52d56d7a..05850587e 100644 --- a/packages/protoplugin/src/ecmascript/legacy-custom-options.ts +++ b/packages/protoplugin/src/ecmascript/legacy-custom-options.ts @@ -12,14 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -import type { AnyDesc } from "@bufbuild/protobuf"; -import { - proto3, - BinaryReader, - Message, - MessageType, - ScalarType, -} from "@bufbuild/protobuf"; +import type { AnyDesc, MessageType } from "@bufbuild/protobuf"; +import { proto3, BinaryReader, Message, ScalarType } from "@bufbuild/protobuf"; /** * @deprecated Please use extensions instead. diff --git a/packages/protoplugin/src/ecmascript/legacy-gencommon.ts b/packages/protoplugin/src/ecmascript/legacy-gencommon.ts index 8d83ce16b..0ee83237b 100644 --- a/packages/protoplugin/src/ecmascript/legacy-gencommon.ts +++ b/packages/protoplugin/src/ecmascript/legacy-gencommon.ts @@ -12,13 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -import type { DescExtension } from "@bufbuild/protobuf"; -import { - codegenInfo, - DescField, - LongType, - ScalarType, -} from "@bufbuild/protobuf"; +import type { DescExtension, DescField } from "@bufbuild/protobuf"; +import { codegenInfo, LongType, ScalarType } from "@bufbuild/protobuf"; import type { GeneratedFile, Printable } from "./generated-file.js"; import type { ImportSymbol } from "./import-symbol.js"; diff --git a/packages/protoplugin/src/ecmascript/opaque-printables.ts b/packages/protoplugin/src/ecmascript/opaque-printables.ts index 7b0442241..726e9d5fe 100644 --- a/packages/protoplugin/src/ecmascript/opaque-printables.ts +++ b/packages/protoplugin/src/ecmascript/opaque-printables.ts @@ -12,13 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { - DescEnum, - DescExtension, - DescMessage, - LongType, - ScalarType, -} from "@bufbuild/protobuf"; +import type { DescEnum, DescExtension, DescMessage } from "@bufbuild/protobuf"; +import { LongType, ScalarType } from "@bufbuild/protobuf"; export type LiteralProtoInt64 = { readonly kind: "es_proto_int64"; diff --git a/packages/protoplugin/src/ecmascript/parameter.ts b/packages/protoplugin/src/ecmascript/parameter.ts index ebaa39956..3ae8bda2f 100644 --- a/packages/protoplugin/src/ecmascript/parameter.ts +++ b/packages/protoplugin/src/ecmascript/parameter.ts @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { Target } from "./target.js"; -import { RewriteImports } from "./import-path.js"; +import type { Target } from "./target.js"; +import type { RewriteImports } from "./import-path.js"; import { PluginOptionError } from "../error.js"; export interface ParsedParameter { diff --git a/packages/protoplugin/src/ecmascript/schema.ts b/packages/protoplugin/src/ecmascript/schema.ts index ca7525a61..a9994d849 100644 --- a/packages/protoplugin/src/ecmascript/schema.ts +++ b/packages/protoplugin/src/ecmascript/schema.ts @@ -31,15 +31,17 @@ import type { GeneratedFileController, } from "./generated-file.js"; import { createGeneratedFile } from "./generated-file.js"; -import { createRuntimeImports, RuntimeImports } from "./runtime-imports.js"; -import { createImportSymbol, ImportSymbol } from "./import-symbol.js"; +import type { RuntimeImports } from "./runtime-imports.js"; +import { createRuntimeImports } from "./runtime-imports.js"; +import type { ImportSymbol } from "./import-symbol.js"; +import { createImportSymbol } from "./import-symbol.js"; import type { Target } from "./target.js"; import { deriveImportPath, makeImportPath, rewriteImportPath, } from "./import-path.js"; -import { ParsedParameter } from "./parameter.js"; +import type { ParsedParameter } from "./parameter.js"; import { makeFilePreamble } from "./file-preamble.js"; /** diff --git a/packages/protoplugin/src/index.ts b/packages/protoplugin/src/index.ts index 236737f32..9aac3dc4b 100644 --- a/packages/protoplugin/src/index.ts +++ b/packages/protoplugin/src/index.ts @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { Schema as SchemaInternal } from "./ecmascript/schema.js"; -export { Plugin } from "./plugin.js"; +import type { Schema as SchemaInternal } from "./ecmascript/schema.js"; +export type { Plugin } from "./plugin.js"; export { runNodeJs } from "./run-node.js"; export { createEcmaScriptPlugin } from "./create-es-plugin.js"; diff --git a/packages/upstream-protobuf/package.json b/packages/upstream-protobuf/package.json index 603b2d321..d0c1a18d0 100644 --- a/packages/upstream-protobuf/package.json +++ b/packages/upstream-protobuf/package.json @@ -16,7 +16,8 @@ }, "exports": { ".": { - "import": "./index.mjs" + "import": "./index.mjs", + "types": "./index.d.ts" } }, "dependencies": { diff --git a/tsconfig.base.json b/tsconfig.base.json index 0ea6ff88c..6af3ac6a4 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -15,9 +15,10 @@ "noImplicitReturns": true, "noFallthroughCasesInSwitch": true, "noImplicitOverride": true, - // We need node's module resolution, so we do not have to skip lib checks - "moduleResolution": "Node", + "moduleResolution": "Node16", + "module": "Node16", + "verbatimModuleSyntax": true, "skipLibCheck": false } }