Skip to content
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
32 changes: 30 additions & 2 deletions .github/release/npm_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,30 @@ def stage(root: Path, packages: list[dict], dist: Path) -> None:
tarball(dist, package)


def has_commonjs_entry(manifest: dict) -> bool:
"""Whether require() of the package root is part of its published contract.

ESM-only packages ("type": "module" with only an "import" export, such as
plugins loaded by hosts that use ES modules) are valid and are checked with
import() alone; every other package must also load with require().
"""
commonjs = manifest.get("type", "commonjs") != "module"
exports = manifest.get("exports")
if exports is None:
return commonjs or str(manifest.get("main", "")).endswith(".cjs")
root = exports.get(".", exports) if isinstance(exports, dict) else exports
if isinstance(root, str):
return commonjs or root.endswith(".cjs")
if isinstance(root, dict):
if "require" in root:
return True
if "node" in root and isinstance(root["node"], dict) and "require" in root["node"]:
return True
default = root.get("default")
return isinstance(default, str) and (commonjs or default.endswith(".cjs"))
return commonjs


def verify(root: Path, packages: list[dict], dist: Path, smoke: Path, *, offline: bool = False) -> None:
inventory = typescript_packages(root)
for package in packages:
Expand All @@ -98,8 +122,12 @@ def verify(root: Path, packages: list[dict], dist: Path, smoke: Path, *, offline
if installed["version"] != staged["version"]:
raise ValueError(f"Installed {staged['name']} != staged workspace version {staged['version']}")
env = {**os.environ, "NAME": package["name"]}
subprocess.run(["node", "-e", "const m = require(process.env.NAME); console.log('require ok:', Object.keys(m).length, 'exports')"],
cwd=directory, env=env, check=True)
installed = json.loads((directory / "node_modules" / package["name"] / "package.json").read_text())
if has_commonjs_entry(installed):
subprocess.run(["node", "-e", "const m = require(process.env.NAME); console.log('require ok:', Object.keys(m).length, 'exports')"],
cwd=directory, env=env, check=True)
else:
print(f"{package['name']}@{package['version']}: ESM-only package, checking import() only", flush=True)
subprocess.run(["node", "--input-type=module", "-e", "import(process.env.NAME).then(m => console.log('import ok:', Object.keys(m).length, 'exports'))"],
cwd=directory, env=env, check=True)

Expand Down
14 changes: 14 additions & 0 deletions .github/release/test_npm_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,5 +139,19 @@ def test_omitted_unpublished_dependency_blocks_delivery_before_upload(self):
run.assert_not_called()


class CommonJsEntryTests(unittest.TestCase):
def test_esm_only_packages_skip_require(self):
esm = {"type": "module", "exports": {".": {"types": "./dist/index.d.ts", "import": "./dist/index.js"}}}
self.assertFalse(npm_release.has_commonjs_entry(esm))
self.assertFalse(npm_release.has_commonjs_entry({"type": "module", "main": "./dist/index.js"}))

def test_commonjs_and_dual_packages_keep_require(self):
self.assertTrue(npm_release.has_commonjs_entry({"main": "dist/index.js"}))
self.assertTrue(npm_release.has_commonjs_entry({"exports": {".": {"import": "./a.mjs", "require": "./a.cjs"}}}))
self.assertTrue(npm_release.has_commonjs_entry({"type": "module", "exports": {".": {"import": "./a.js", "require": "./a.cjs"}}}))
self.assertTrue(npm_release.has_commonjs_entry({"exports": {".": {"types": "./a.d.ts", "default": "./a.js"}}}))
self.assertTrue(npm_release.has_commonjs_entry({"type": "module", "exports": "./index.cjs"}))


if __name__ == '__main__':
unittest.main()
Loading