Skip to content

Commit

Permalink
Fix client only import with importsNotUsedAsValues error (withastro#5639
Browse files Browse the repository at this point in the history
)
  • Loading branch information
bluwy committed Dec 19, 2022
1 parent 53e0c98 commit 1ac1ed8
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/two-pants-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix `client:only` imports with `"importsNotUsedAsValues": "error"` tsconfig
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { useState } from 'react';

// accessing browser globals as side effects is allowed if the component is client:only
console.log(document.title)

/** a counter written in React */
export function Counter({ children, id }) {
const [count, setCount] = useState(0);
Expand Down
5 changes: 5 additions & 0 deletions packages/astro/e2e/fixtures/client-only/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"importsNotUsedAsValues": "error"
}
}
6 changes: 6 additions & 0 deletions packages/astro/src/vite-plugin-astro/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ export async function cachedFullCompilation({
loader: 'ts',
target: 'esnext',
sourcemap: 'external',
tsconfigRaw: {
compilerOptions: {
// Ensure client:only imports are treeshaken
importsNotUsedAsValues: 'remove',
},
},
});
} catch (err: any) {
await enhanceCompileError({
Expand Down
6 changes: 6 additions & 0 deletions packages/astro/src/vite-plugin-jsx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ export default function jsx({ settings, logging }: AstroPluginJSXOptions): Plugi
loader: getEsbuildLoader(id),
jsx: 'preserve',
sourcemap: 'inline',
tsconfigRaw: {
compilerOptions: {
// Ensure client:only imports are treeshaken
importsNotUsedAsValues: 'remove',
},
},
});
return transformJSX({
code: jsxCode,
Expand Down

0 comments on commit 1ac1ed8

Please sign in to comment.