feat: Merge plugin modules (#10895)
Fixes: FRMW-2858 This PR merge the modules exported by the plugins with the modules defined within the user config. As a result, all modules get loaded without changing the internals of the loader. However, you cannot disable the module of a plugin by re-adding it to the `modules` array. That is something we should handle separately. We've added the breaking change label because of the following fix: We did broke the ability to completely disable modules in the past pr's, in this pr we re add the ability to disable a module and that this modules does not get loaded at all. ([here](https://github.com/medusajs/medusa/pull/10895/commits/6dd164f783d632c927e0a48c50090dcf7387ccdd)) Co-authored-by: Adrien de Peretti <25098370+adrien2p@users.noreply.github.com>
This commit is contained in:
co-authored by
Adrien de Peretti
parent
a607b8e800
commit
c1930bd656
@@ -2,6 +2,7 @@ import { join } from "path"
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
MedusaError,
|
||||
mergePluginModules,
|
||||
} from "@medusajs/framework/utils"
|
||||
import { LinkLoader } from "@medusajs/framework/links"
|
||||
import { logger } from "@medusajs/framework/logger"
|
||||
@@ -27,6 +28,8 @@ const main = async function ({ directory, modules }) {
|
||||
)
|
||||
|
||||
const plugins = await getResolvedPlugins(directory, configModule, true)
|
||||
mergePluginModules(configModule, plugins)
|
||||
|
||||
const linksSourcePaths = plugins.map((plugin) =>
|
||||
join(plugin.resolve, "links")
|
||||
)
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
import { join } from "path"
|
||||
import { ContainerRegistrationKeys } from "@medusajs/framework/utils"
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
mergePluginModules,
|
||||
} from "@medusajs/framework/utils"
|
||||
import { LinkLoader } from "@medusajs/framework/links"
|
||||
import { logger } from "@medusajs/framework/logger"
|
||||
import { MedusaAppLoader } from "@medusajs/framework"
|
||||
@@ -38,6 +41,8 @@ export async function migrate({
|
||||
)
|
||||
|
||||
const plugins = await getResolvedPlugins(directory, configModule, true)
|
||||
mergePluginModules(configModule, plugins)
|
||||
|
||||
const linksSourcePaths = plugins.map((plugin) =>
|
||||
join(plugin.resolve, "links")
|
||||
)
|
||||
|
||||
@@ -2,6 +2,7 @@ import { join } from "path"
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
MedusaError,
|
||||
mergePluginModules,
|
||||
} from "@medusajs/framework/utils"
|
||||
import { LinkLoader } from "@medusajs/framework/links"
|
||||
import { logger } from "@medusajs/framework/logger"
|
||||
@@ -27,6 +28,8 @@ const main = async function ({ directory, modules }) {
|
||||
)
|
||||
|
||||
const plugins = await getResolvedPlugins(directory, configModule, true)
|
||||
mergePluginModules(configModule, plugins)
|
||||
|
||||
const linksSourcePaths = plugins.map((plugin) =>
|
||||
join(plugin.resolve, "links")
|
||||
)
|
||||
|
||||
@@ -2,7 +2,10 @@ import boxen from "boxen"
|
||||
import chalk from "chalk"
|
||||
import { join } from "path"
|
||||
import checkbox from "@inquirer/checkbox"
|
||||
import { ContainerRegistrationKeys } from "@medusajs/framework/utils"
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
mergePluginModules,
|
||||
} from "@medusajs/framework/utils"
|
||||
import { LinkMigrationsPlannerAction } from "@medusajs/framework/types"
|
||||
import { LinkLoader } from "@medusajs/framework/links"
|
||||
import { logger } from "@medusajs/framework/logger"
|
||||
@@ -188,6 +191,8 @@ const main = async function ({ directory, executeSafe, executeAll }) {
|
||||
const medusaAppLoader = new MedusaAppLoader()
|
||||
|
||||
const plugins = await getResolvedPlugins(directory, configModule, true)
|
||||
mergePluginModules(configModule, plugins)
|
||||
|
||||
const linksSourcePaths = plugins.map((plugin) =>
|
||||
join(plugin.resolve, "links")
|
||||
)
|
||||
|
||||
@@ -33,7 +33,7 @@ describe("getResolvedPlugins | relative paths", () => {
|
||||
|
||||
expect(plugins).toEqual([
|
||||
{
|
||||
resolve: path.join(fs.basePath, "./plugins/dummy/build"),
|
||||
resolve: path.join(fs.basePath, "./plugins/dummy/.medusa/server/src"),
|
||||
name: "my-dummy-plugin",
|
||||
id: "my-dummy-plugin",
|
||||
options: { apiKey: "asecret" },
|
||||
@@ -48,7 +48,10 @@ describe("getResolvedPlugins | relative paths", () => {
|
||||
name: "my-dummy-plugin",
|
||||
version: "1.0.0",
|
||||
})
|
||||
await fs.create("plugins/dummy/build/modules/blog/index.js", ``)
|
||||
await fs.create(
|
||||
"plugins/dummy/.medusa/server/src/modules/blog/index.js",
|
||||
``
|
||||
)
|
||||
|
||||
const plugins = await getResolvedPlugins(
|
||||
fs.basePath,
|
||||
@@ -67,7 +70,7 @@ describe("getResolvedPlugins | relative paths", () => {
|
||||
|
||||
expect(plugins).toEqual([
|
||||
{
|
||||
resolve: path.join(fs.basePath, "./plugins/dummy/build"),
|
||||
resolve: path.join(fs.basePath, "./plugins/dummy/.medusa/server/src"),
|
||||
name: "my-dummy-plugin",
|
||||
id: "my-dummy-plugin",
|
||||
options: { apiKey: "asecret" },
|
||||
@@ -77,7 +80,7 @@ describe("getResolvedPlugins | relative paths", () => {
|
||||
options: {
|
||||
apiKey: "asecret",
|
||||
},
|
||||
resolve: "./plugins/dummy/build/modules/blog",
|
||||
resolve: "./plugins/dummy/.medusa/server/src/modules/blog",
|
||||
},
|
||||
],
|
||||
},
|
||||
@@ -132,7 +135,10 @@ describe("getResolvedPlugins | package reference", () => {
|
||||
|
||||
expect(plugins).toEqual([
|
||||
{
|
||||
resolve: path.join(fs.basePath, "node_modules/@plugins/dummy/build"),
|
||||
resolve: path.join(
|
||||
fs.basePath,
|
||||
"node_modules/@plugins/dummy/.medusa/server/src"
|
||||
),
|
||||
name: "my-dummy-plugin",
|
||||
id: "my-dummy-plugin",
|
||||
options: { apiKey: "asecret" },
|
||||
@@ -149,7 +155,7 @@ describe("getResolvedPlugins | package reference", () => {
|
||||
version: "1.0.0",
|
||||
})
|
||||
await fs.create(
|
||||
"node_modules/@plugins/dummy/build/modules/blog/index.js",
|
||||
"node_modules/@plugins/dummy/.medusa/server/src/modules/blog/index.js",
|
||||
``
|
||||
)
|
||||
|
||||
@@ -170,7 +176,10 @@ describe("getResolvedPlugins | package reference", () => {
|
||||
|
||||
expect(plugins).toEqual([
|
||||
{
|
||||
resolve: path.join(fs.basePath, "node_modules/@plugins/dummy/build"),
|
||||
resolve: path.join(
|
||||
fs.basePath,
|
||||
"node_modules/@plugins/dummy/.medusa/server/src"
|
||||
),
|
||||
name: "my-dummy-plugin",
|
||||
id: "my-dummy-plugin",
|
||||
options: { apiKey: "asecret" },
|
||||
@@ -180,7 +189,7 @@ describe("getResolvedPlugins | package reference", () => {
|
||||
options: {
|
||||
apiKey: "asecret",
|
||||
},
|
||||
resolve: "@plugins/dummy/build/modules/blog",
|
||||
resolve: "@plugins/dummy/.medusa/server/src/modules/blog",
|
||||
},
|
||||
],
|
||||
},
|
||||
|
||||
@@ -4,6 +4,7 @@ import { isString, readDir } from "@medusajs/framework/utils"
|
||||
import { ConfigModule, PluginDetails } from "@medusajs/framework/types"
|
||||
|
||||
const MEDUSA_APP_SOURCE_PATH = "src"
|
||||
const MEDUSA_PLUGIN_SOURCE_PATH = ".medusa/server/src"
|
||||
export const MEDUSA_PROJECT_NAME = "project-plugin"
|
||||
|
||||
function createPluginId(name: string): string {
|
||||
@@ -57,11 +58,8 @@ async function resolvePlugin(
|
||||
const resolvedPath = path.dirname(pkgJSON.path)
|
||||
|
||||
const name = pkgJSON.contents.name || pluginPath
|
||||
const srcDir = pkgJSON.contents.main
|
||||
? path.dirname(pkgJSON.contents.main)
|
||||
: "build"
|
||||
|
||||
const resolve = path.join(resolvedPath, srcDir)
|
||||
const resolve = path.join(resolvedPath, MEDUSA_PLUGIN_SOURCE_PATH)
|
||||
const modules = await readDir(path.join(resolve, "modules"), {
|
||||
ignoreMissing: true,
|
||||
})
|
||||
@@ -75,7 +73,7 @@ async function resolvePlugin(
|
||||
version: pkgJSON.contents.version || "0.0.0",
|
||||
modules: modules.map((mod) => {
|
||||
return {
|
||||
resolve: `${pluginPath}/${srcDir}/modules/${mod.name}`,
|
||||
resolve: `${pluginPath}/${MEDUSA_PLUGIN_SOURCE_PATH}/modules/${mod.name}`,
|
||||
options: pluginOptions,
|
||||
}
|
||||
}),
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
GraphQLSchema,
|
||||
mergePluginModules,
|
||||
promiseAll,
|
||||
} from "@medusajs/framework/utils"
|
||||
import { asValue } from "awilix"
|
||||
@@ -147,6 +148,8 @@ export default async ({
|
||||
)
|
||||
|
||||
const plugins = await getResolvedPlugins(rootDirectory, configModule, true)
|
||||
mergePluginModules(configModule, plugins)
|
||||
|
||||
const linksSourcePaths = plugins.map((plugin) =>
|
||||
join(plugin.resolve, "links")
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user