Skip to content

Commit

Permalink
Add default .npmrc for astro add lit (#6460)
Browse files Browse the repository at this point in the history
Co-authored-by: Sarah Rainsberger <[email protected]>
  • Loading branch information
bluwy and sarah11918 committed Mar 9, 2023
1 parent af25778 commit 77a046e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
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*
```

### 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

0 comments on commit 77a046e

Please sign in to comment.