Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(plugins): add support for loading ESM plugins #2688

Merged
merged 3 commits into from Jan 25, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 10 additions & 6 deletions lib/plugins/utils.js
Expand Up @@ -52,14 +52,18 @@ export async function loadPlugin({cwd}, name, pluginsPath) {
? dirname(resolveFrom.silent(__dirname, pluginsPath[name]) || resolveFrom(cwd, pluginsPath[name]))
: __dirname;

if (!isFunction(name)) {
const file = resolveFrom.silent(basePath, name) || resolveFrom(cwd, name);
// See https://github.com/mysticatea/eslint-plugin-node/issues/250
// eslint-disable-next-line node/no-unsupported-features/es-syntax
name = (await import(`file://${file}`)).default;
if (isFunction(name)) {
return name;
}

return name;
const file = resolveFrom.silent(basePath, name) || resolveFrom(cwd, name);
const { default: cjsExport, ...esmNamedExports } = await import(`file://${file}`);

if (cjsExport) {
return cjsExport;
}

return esmNamedExports;
Comment on lines +55 to +66
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this implementation has changed slightly because of merging it with the fix for windows support of v20

}

export function parseConfig(plugin) {
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/plugin-esm-named-exports.js
@@ -0,0 +1,3 @@
export async function verifyConditions(pluginConfig, context) {
context.logger.log("verifyConditions called");
}
24 changes: 24 additions & 0 deletions test/integration.test.js
Expand Up @@ -44,6 +44,7 @@ const cli = path.resolve("./bin/semantic-release.js");
const pluginError = path.resolve("./test/fixtures/plugin-error");
const pluginInheritedError = path.resolve("./test/fixtures/plugin-error-inherited");
const pluginLogEnv = path.resolve("./test/fixtures/plugin-log-env");
const pluginEsmNamedExports = path.resolve("./test/fixtures/plugin-esm-named-exports");

test.before(async () => {
await Promise.all([gitbox.start(), npmRegistry.start(), mockServer.start()]);
Expand Down Expand Up @@ -713,3 +714,26 @@ test("Use the repository URL as is if none of the given git credentials are vali
dummyUrl
);
});

test("ESM Plugin with named exports", async (t) => {
const packageName = "log-secret";
// Create a git repository, set the current working directory at the root of the repo
t.log("Create git repository");
const { cwd, repositoryUrl } = await gitbox.createRepo(packageName);
await writeJson(path.resolve(cwd, "package.json"), {
name: packageName,
version: "0.0.0-dev",
repository: { url: repositoryUrl },
release: { plugins: [pluginEsmNamedExports] },
});

t.log("$ semantic-release");
const { stdout, stderr } = await execa(cli, [], {
env: { ...env, MY_TOKEN: "secret token" },
cwd,
reject: false,
extendEnv: false,
});

t.regex(stdout, new RegExp(`verifyConditions called`));
});
13 changes: 12 additions & 1 deletion test/plugins/utils.test.js
Expand Up @@ -199,7 +199,18 @@ test('loadPlugin', async (t) => {
await loadPlugin({cwd}, './plugin-noop.cjs', {'./plugin-noop.cjs': './test/fixtures'}),
'From a shareable config context'
);
t.is(func, await loadPlugin({cwd}, func, {}), 'Defined as a function');
t.is(
(await import("../fixtures/plugin-noop.cjs")).default,
await loadPlugin({ cwd }, "./plugin-noop.cjs", { "./plugin-noop.cjs": "./test/fixtures" }),
"From a shareable config context"
);
const { ...namedExports } = await import("../fixtures/plugin-esm-named-exports.js");
const plugin = await loadPlugin({ cwd }, "./plugin-esm-named-exports.js", {
"./plugin-esm-named-exports.js": "./test/fixtures",
});

t.deepEqual(namedExports, plugin, "ESM with named exports");
t.is(func, await loadPlugin({ cwd }, func, {}), "Defined as a function");
});

test('parseConfig', (t) => {
Expand Down