From 4dcebbe1c297a4111bc75b1492e4817bbb92ed7d Mon Sep 17 00:00:00 2001 From: Robert Jackson Date: Fri, 15 Oct 2021 13:09:09 -0400 Subject: [PATCH] Enable usage of this plugin by release-it's `require` based system Since the move to `type: modules` in package.json this package no longer works within `release-it`'s normal plugin resolution / resolving steps (because it is internally using `import-from` which is basically just doing `require` relative to the current working directory; and you can't `require` a ES modules based file). This adds a very simple wrapper script and leverages the pre-existing static async method `isEnabled` on the plugin instance to absorb async from a dynamic `import()` expression. The `PluginClass.isEnabled()` hook is always called before `new PluginClass` is called, so this system of absorbing the async in `isEnabled` then returning an instance of "the real plugin" from the fake plugin's constructor is pretty safe. --- cjs-wrapper.cjs | 17 +++++++++++++++++ package.json | 8 +++++++- 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 cjs-wrapper.cjs diff --git a/cjs-wrapper.cjs b/cjs-wrapper.cjs new file mode 100644 index 0000000..cbeb2ae --- /dev/null +++ b/cjs-wrapper.cjs @@ -0,0 +1,17 @@ +// this will be populated inside the static `isEnabled` method just below +let RealPlugin; + +module.exports = class FakeCJSWrapperPlugin { + + static async isEnabled() { + // we use the async of this method to enable us to absorb the dynamic + // import statement + let RealPluginModule = await import('./index.js'); + RealPlugin = RealPluginModule.default; + } + + constructor(...args) { + // now we just use the "real plugin" as is + return new RealPlugin(...args); + } +} diff --git a/package.json b/package.json index 468bea2..330b6f3 100644 --- a/package.json +++ b/package.json @@ -43,6 +43,12 @@ "release-it": "^14.11.6", "sinon": "^11.1.2" }, + "exports": { + ".": { + "import": "./index.js", + "require": "./cjs-wrapper.cjs" + } + }, "peerDependencies": { "release-it": "^14.0.0" }, @@ -54,7 +60,7 @@ }, "release-it": { "plugins": { - "./index.js": { + "./cjs-wrapper.cjs": { "infile": "CHANGELOG.md", "launchEditor": true }