0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

Add default .npmrc for astro add lit (#6460)

Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
This commit is contained in:
Bjorn Lu 2023-03-09 14:42:57 +08:00 committed by GitHub
parent af25778a9b
commit 77a046e886
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 0 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Add default `.npmrc` file when adding the Lit integration through `astro add lit` and using `pnpm`.

View file

@ -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',
@ -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: {

View file

@ -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.