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 ESM wrapper to avoid dual package hazard #566

Merged
merged 3 commits into from
Oct 6, 2023
Merged
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
20 changes: 9 additions & 11 deletions packages/protobuf/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,22 @@
"url": "https://github.com/bufbuild/protobuf-es.git",
"directory": "packages/protobuf"
},
"sideEffects": false,
"scripts": {
"clean": "rm -rf ./dist/cjs/* ./dist/esm/*",
"build": "npm run build:cjs && npm run build:esm",
"clean": "rm -rf ./dist/*",
"build": "npm run build:cjs && npm run build:esm && npm run build:proxy",
"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 .",
"attw": "attw --pack"
},
"main": "./dist/cjs/index.js",
"type": "module",
"sideEffects": false,
"main": "./dist/cjs/index.js",
"exports": {
"import": {
"types": "./dist/esm/index.d.ts",
"default": "./dist/esm/index.js"
},
"require": {
"types": "./dist/cjs/index.d.ts",
"default": "./dist/cjs/index.js"
".": {
"module": "./dist/esm/index.js",
"require": "./dist/cjs/index.js",
"import": "./dist/proxy/index.js"
}
},
"files": [
Expand Down
31 changes: 11 additions & 20 deletions packages/protoplugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,27 @@
"url": "https://github.com/bufbuild/protobuf-es.git",
"directory": "packages/protoplugin"
},
"sideEffects": false,
"scripts": {
"clean": "rm -rf ./dist/cjs/* ./dist/esm/*",
"build": "npm run build:cjs && npm run build:esm",
"clean": "rm -rf ./dist/*",
"build": "npm run build:cjs && npm run build:esm && npm run build:proxy",
"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"
},
"main": "./dist/cjs/index.js",
"type": "module",
"sideEffects": false,
"main": "./dist/cjs/index.js",
"exports": {
".": {
"import": {
"types": "./dist/esm/index.d.ts",
"default": "./dist/esm/index.js"
},
"require": {
"types": "./dist/cjs/index.d.ts",
"default": "./dist/cjs/index.js"
}
"module": "./dist/esm/index.js",
"require": "./dist/cjs/index.js",
"import": "./dist/proxy/index.js"
},
"./ecmascript": {
"import": {
"types": "./dist/esm/ecmascript/index.d.ts",
"default": "./dist/esm/ecmascript/index.js"
},
"require": {
"types": "./dist/cjs/ecmascript/index.d.ts",
"default": "./dist/cjs/ecmascript/index.js"
}
"module": "./dist/esm/ecmascript/index.js",
"require": "./dist/cjs/ecmascript/index.js",
"import": "./dist/proxy/ecmascript/index.js"
}
},
"typesVersions": {
Expand Down
103 changes: 103 additions & 0 deletions scripts/gen-esm-proxy.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
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";
}