From 5d8def5e0f82edc90b98d4a6019138a219058640 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. --- .eslintrc.cjs | 8 ++++++++ cjs-wrapper.cjs | 23 +++++++++++++++++++++++ package.json | 8 +++++++- 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 cjs-wrapper.cjs diff --git a/.eslintrc.cjs b/.eslintrc.cjs index f80464b..4252ca6 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -12,4 +12,12 @@ module.exports = { rules: { 'prettier/prettier': 'error', }, + overrides: [ + { + files: ['**/*.cjs'], + parserOptions: { + sourceType: 'script', + }, + }, + ], }; diff --git a/cjs-wrapper.cjs b/cjs-wrapper.cjs new file mode 100644 index 0000000..761ca88 --- /dev/null +++ b/cjs-wrapper.cjs @@ -0,0 +1,23 @@ +// 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 + + // dynamic `import()` statements work on Node ^12.17.0 and >= 14; which + // is within our support range; we can remove this inline disable + // when https://github.com/mysticatea/eslint-plugin-node/pull/256 (or another + // PR like it) lands + + // eslint-disable-next-line node/no-unsupported-features/es-syntax + 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 }