Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/gentle-otters-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@shopware/nuxt-module": patch
---

Register the `#shopware` types in every TypeScript context (app, server/nitro, node, shared) instead of only the app one. This fixes `Cannot find module '#shopware'` in server-side code (e.g. `server/` routes and API builders) when a project uses the Nuxt 4 project-references `tsconfig.json` layout. Projects that ship their own `shopware.d.ts` are referenced in place so their relative imports keep resolving.
33 changes: 26 additions & 7 deletions packages/nuxt-module/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,25 +75,44 @@ describe("@shopware/nuxt-module", () => {
isConfigDeprecatedMock.mockReturnValue(false);
});

it("should inject default shopware.d.ts when project has no custom types", async () => {
const ALL_TYPE_CONTEXTS = {
nuxt: true,
nitro: true,
node: true,
shared: true,
};

it("should inject default shopware.d.ts in all type contexts when project has no custom types", async () => {
existsSyncMock.mockReturnValue(false);
const setup = await getModuleSetup();

await setup({}, createNuxtMock("/tmp/test-project"));

expect(addTypeTemplateMock).toHaveBeenCalledWith({
filename: "shopware.d.ts",
src: "/mocked-module-dir/../shopware.d.ts",
});
expect(addTypeTemplateMock).toHaveBeenCalledWith(
{
filename: "shopware.d.ts",
src: "/mocked-module-dir/../shopware.d.ts",
},
ALL_TYPE_CONTEXTS,
);
});

it("should skip injecting shopware.d.ts when project has custom types", async () => {
it("should reference the project's own shopware.d.ts in all type contexts when it exists", async () => {
existsSyncMock.mockReturnValue(true);
const setup = await getModuleSetup();

await setup({}, createNuxtMock("/tmp/test-project"));

expect(addTypeTemplateMock).not.toHaveBeenCalled();
expect(addTypeTemplateMock).toHaveBeenCalledTimes(1);
const [template, context] = addTypeTemplateMock.mock.calls[0] as [
{ filename: string; getContents: () => string },
Record<string, boolean>,
];
expect(template.filename).toBe("shopware.d.ts");
expect(template.getContents()).toContain(
'/// <reference path="/tmp/test-project/shopware.d.ts" />',
);
expect(context).toEqual(ALL_TYPE_CONTEXTS);
});

it("should check for shopware.d.ts in the project root directory", async () => {
Expand Down
30 changes: 25 additions & 5 deletions packages/nuxt-module/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,31 @@ export default defineNuxtModule<ShopwareNuxtOptions>({
});

const projectShopwareTypes = resolve(nuxt.options.rootDir, "shopware.d.ts");
if (!existsSync(projectShopwareTypes)) {
addTypeTemplate({
filename: "shopware.d.ts",
src: resolver.resolve("../shopware.d.ts"),
});
// Register `#shopware` in every type-check context, not just the app one.
const shopwareTypeContexts = {
nuxt: true,
nitro: true,
node: true,
shared: true,
};
if (existsSync(projectShopwareTypes)) {
// Reference the project's file in place so its relative imports keep resolving.
const referencePath = projectShopwareTypes.replace(/\\/g, "/");
addTypeTemplate(
{
filename: "shopware.d.ts",
getContents: () => `/// <reference path="${referencePath}" />\n`,
},
shopwareTypeContexts,
);
} else {
addTypeTemplate(
{
filename: "shopware.d.ts",
src: resolver.resolve("../shopware.d.ts"),
},
shopwareTypeContexts,
);
}

addCustomTab({
Expand Down