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

Add default .npmrc for astro add lit #6460

Merged
merged 3 commits into from
Mar 9, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tough-tigers-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Add default `.npmrc` file when adding the Lit integration through `astro add lit` and using `pnpm`.
20 changes: 20 additions & 0 deletions packages/astro/src/core/add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> = {
netlify: '@astrojs/netlify/functions',
Expand Down Expand Up @@ -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: {
Expand Down
8 changes: 8 additions & 0 deletions packages/integrations/lit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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*
```
ematipico marked this conversation as resolved.
Show resolved Hide resolved

### 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.
Expand Down