absolute-version / commit-and-tag-version

Fork of the excellent standard-version. Automate versioning and CHANGELOG generation, with semver.org and conventionalcommits.org :trophy:
ISC License
360 stars 33 forks source link

Support ESM format for config file #147

Open davidsneighbour opened 3 months ago

davidsneighbour commented 3 months ago

I don't know if this is a bug or maybe a future feature: using a config that is in the ESM format leads to errors:

❯ npm run release

> @davidsneighbour/kollitsch-dev@2024.1.32 release
> npm run release:patch

> @davidsneighbour/kollitsch-dev@2024.1.32 release:patch
> commit-and-tag-version --release-as patch --sign -a -t "v" --releaseCommitMessageFormat "chore(release): v{{currentTag}}" && ./bin/repo/hooks/postrelease

/home/patrick/github.com/davidsneighbour/kollitsch.dev/node_modules/commit-and-tag-version/lib/configuration.js:20
    const jsConfiguration = require(configPath);
                            ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /home/patrick/github.com/davidsneighbour/kollitsch.dev/.versionrc.js from /home/patrick/github.com/davidsneighbour/kollitsch.dev/node_modules/commit-and-tag-version/lib/configuration.js not supported.
Instead change the require of .versionrc.js in /home/patrick/github.com/davidsneighbour/kollitsch.dev/node_modules/commit-and-tag-version/lib/configuration.js to a dynamic import() which is available in all CommonJS modules.
    at module.exports.getConfiguration (/home/patrick/github.com/davidsneighbour/kollitsch.dev/node_modules/commit-and-tag-version/lib/configuration.js:20:29)
    at Object.<anonymous> (/home/patrick/github.com/davidsneighbour/kollitsch.dev/node_modules/commit-and-tag-version/command.js:153:11) {
  code: 'ERR_REQUIRE_ESM'
}

Node.js v20.12.2

Does the package understand the config in ESM format and if not is it possible to add this as a feature?

amoshydra commented 2 months ago

change .versionrc.js to .versionrc.cjs.

this package support cjs extension

amoshydra commented 2 months ago

Oops, I have mistook the issue as about "supporting Package type=module environment". My suggestion above isn't that relevant then.

I have checked the source code, mjs is not supported as the config is always imported via require. See: https://github.com/absolute-version/commit-and-tag-version/blob/7ebdacd6edd26076eabed753cdcf3070142f5f0d/lib/configuration.js#L19-L20

I guess this would be a feature request.

Off the top of my head, it will need to use dynamic import to load an ESM config and making the code flow async.

TimothyJones commented 2 months ago

I agree, I think this is a feature request. It would definitely be good to have the ability to use ESM for the config.

I believe it would (almost) come for free if we upgraded it to typescript, so I'll tag #29