diff --git a/.changeset/tough-tigers-drum.md b/.changeset/tough-tigers-drum.md new file mode 100644 index 000000000000..b82c4d224f05 --- /dev/null +++ b/.changeset/tough-tigers-drum.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Add default `.npmrc` file when adding the Lit integration through `astro add lit` and using `pnpm`. diff --git a/packages/astro/src/core/add/index.ts b/packages/astro/src/core/add/index.ts index 3abe00db97e9..55880a4c5f0d 100644 --- a/packages/astro/src/core/add/index.ts +++ b/packages/astro/src/core/add/index.ts @@ -60,6 +60,10 @@ export default { preprocess: vitePreprocess(), }; `; +const LIT_NPMRC_STUB = `\ +# Lit libraries are required to be hoisted due to dependency issues. +public-hoist-pattern[]=*lit* +`; const OFFICIAL_ADAPTER_TO_IMPORT_MAP: Record = { netlify: '@astrojs/netlify/functions', @@ -146,6 +150,22 @@ export default async function add(names: string[], { cwd, flags, logging, teleme defaultConfigContent: SVELTE_CONFIG_STUB, }); } + // Some lit dependencies needs to be hoisted, so for strict package managers like pnpm, + // we add an .npmrc to hoist them + if ( + integrations.find((integration) => integration.id === 'lit') && + (await preferredPM(fileURLToPath(root)))?.name === 'pnpm' + ) { + await setupIntegrationConfig({ + root, + logging, + flags, + integrationName: 'Lit', + possibleConfigFiles: ['./.npmrc'], + defaultConfigFile: './.npmrc', + defaultConfigContent: LIT_NPMRC_STUB, + }); + } break; } case UpdateResult.cancelled: { diff --git a/packages/integrations/lit/README.md b/packages/integrations/lit/README.md index fe99f3e2fa90..bd3101a19c91 100644 --- a/packages/integrations/lit/README.md +++ b/packages/integrations/lit/README.md @@ -141,6 +141,14 @@ export default defineConfig({ The correct order might be different depending on the underlying cause of the problem. This is not guaranteed to fix every issue however, and some libraries cannot be used if you are using the Lit integration because of this. +### Strict package managers + +When using a [strict package manager](https://pnpm.io/pnpm-vs-npm#npms-flat-tree) like `pnpm`, you may get an error such as `ReferenceError: module is not defined` when running your site. To fix this, hoist Lit dependencies with an `.npmrc` file: + +```ini title=".npmrc" +public-hoist-pattern[]=*lit* +``` + ### Limitations The Lit integration is powered by `@lit-labs/ssr` which has some limitations. See their [limitations documentation](https://www.npmjs.com/package/@lit-labs/ssr#user-content-notes-and-limitations) to learn more.