yassinedoghri / astro-i18next

An astro integration of i18next + some utility components to help you translate your astro websites!
https://astro-i18next.yassinedoghri.com
MIT License
487 stars 36 forks source link

Support for PNPM (astro-i18next throws with "module is not defined") #48

Open Alexandre-Fernandez opened 1 year ago

Alexandre-Fernandez commented 1 year ago

When I try to use <Trans />, <HeadHrefLangs />, <LanguageSelector />, interpolate, etc I get the following error :

module is not defined

/@fs/my/local/directories/turborepo-astro-nestjs/node_modules/.pnpm/deepmerge@4.2.2/node_modules/deepmerge/dist/cjs.js:133:1

ReferenceError: module is not defined
    at /my/local/directories/turborepo-astro-nestjs/node_modules/.pnpm/deepmerge@4.2.2/node_modules/deepmerge/dist/cjs.js:133:1
    at instantiateModule (file:///my/local/directories/turborepo-astro-nestjs/node_modules/.pnpm/vite@3.1.6/node_modules/vite/dist/node/chunks/dep-db16f19c.js:53436:15

I'm using pnpm and turborepo, reproduction (error at apps/frontend/src/layouts/Layout.astro line 2 & 19): https://github.com/Alexandre-Fernandez/turborepo-astro-nestjs

Astro app is located in /apps/frontend, commands to get the error :

EDIT:

This might happen because it's trying to load from the monorepo root /node_modules instead of its corresponding workspace /apps/frontend/node_modules. I say that because I get a different error (another missing dependency) when I install deepmerge@4.2.2 in the root package.json (pnpm add deepmerge@4.2.2 -w). This only happens with astro-i18next and not with i18next.

Alexandre-Fernandez commented 1 year ago

Apparently the problem are not monorepos but PNPM. I made a new reproduction here : https://github.com/Alexandre-Fernandez/astro-i18next-issue-48

If you install with npm install it works, but if you install with pnpm install you get the module not defined error.

Workaround

Add this file to your root :

/.npmrc

public-hoist-pattern[]="*deepmerge*"
public-hoist-pattern[]="*proload*"
public-hoist-pattern[]="*locale-emoji*"

https://pnpm.io/npmrc#public-hoist-pattern

millette commented 1 year ago

While I haven't used the module yet, I'm a big fan of both astro and pnpm so I had a quick look at the source.

First thing I notice is these 2 imports:

cli/utils.ts:import { AstroI18nextConfig } from "types"; cli/generate.ts:import { AstroI18nextConfig } from "types";

Both should probably import from "./types" (or ./types.ts - I never know if the extension is required).

Other than that, I see these imports, all from dev deps. Probably should be regular deps:

cli/utils.ts:import { fdir, PathsOutput } from "fdir"; // dev dep
index.ts:import { AstroIntegration } from "astro"; // dev dep
cli/middlewares.ts:import { MiddlewareFunction } from "yargs"; // dev dep
cli/index.ts:import yargs from "yargs"; // dev dep
cli/index.ts:import { hideBin } from "yargs/helpers"; // dev dep
cli/utils.ts:import fsExtra from "fs-extra"; // dev dep
cli/utils.ts:import ts from "typescript"; // dev dep

With those fixes, pnpm might be happy without resorting to public-hoist-patterns.