Skip to content

Commit

Permalink
Enable usage of this plugin by release-it's require based system
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rwjblue committed Oct 15, 2021
1 parent 05972a5 commit 4dcebbe
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
17 changes: 17 additions & 0 deletions cjs-wrapper.cjs
Original file line number Diff line number Diff line change
@@ -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);
}
}
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand All @@ -54,7 +60,7 @@
},
"release-it": {
"plugins": {
"./index.js": {
"./cjs-wrapper.cjs": {
"infile": "CHANGELOG.md",
"launchEditor": true
}
Expand Down

0 comments on commit 4dcebbe

Please sign in to comment.