Skip to content

Commit bd577e2

Browse files
authored
Merge pull request #1408 from simonihmig/export-styles
Export the CSS as well as a JS only variant
2 parents b899b81 + 90780b1 commit bd577e2

File tree

5 files changed

+34
-4
lines changed

5 files changed

+34
-4
lines changed

.changeset/many-crabs-cross.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
'@responsive-image/react': minor
3+
---
4+
5+
Export the CSS as well as a JS only variant
6+
7+
Most of the time the default export should "just work", but this change adds some flexibility for users of the package.
8+
9+
In particular it makes it easier to include the CSS in a client-side bundle in an application that is otherwise server-side only.

apps/docs/src/frameworks/react.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,16 @@ To process [local images](../usage/local-images.md) you will need to setup one o
3535
The `@responsibe-image/cdn` package provides multiple helper functions to support [remote images](../usage/remote-images.md) served from different image CDNs for use with the `<responsive-image/>` component.
3636

3737
Please refer to the [image CDN](../cdn/index.md) guide for details on all supported options and examples of the respective image CDN.
38+
39+
## Separate stylesheet and JavaScript imports
40+
41+
The default export includes an import of the component's CSS. If this causes problems with your build setup you can import the JavaScript and CSS separately.
42+
43+
```diff
44+
- import { ResponsiveImage } from "@responsive-image/react";
45+
+ import { ResponsiveImage } from "@responsive-image/react/responsive-image.js";
46+
+ import "@responsive-image/react/responsive-image.css";
47+
```
48+
49+
> [!TIP]
50+
> If you use Vite to build for server-side rendering you can add `@responsive-image/react` to the [ssr.noExternals](https://vite.dev/guide/ssr.html#ssr-externals) option in your Vite config.

packages/react/package.json

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,17 @@
1919
"module": "./dist/index.js",
2020
"types": "./dist/index.d.ts",
2121
"exports": {
22-
"import": {
23-
"types": "./dist/index.d.ts",
24-
"default": "./dist/index.js"
22+
"./responsive-image.css": {
23+
"default": "./dist/responsive-image.css"
24+
},
25+
"./responsive-image.js": {
26+
"default": "./dist/responsive-image.js"
27+
},
28+
".": {
29+
"import": {
30+
"types": "./dist/index.d.ts",
31+
"default": "./dist/index.js"
32+
}
2533
}
2634
},
2735
"scripts": {

packages/react/src/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
import './responsive-image.css'; // import the CSS here to have an export in package.json that is JS-only
12
export { ResponsiveImage } from './responsive-image.tsx';

packages/react/src/responsive-image.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
type ImageUrlForType,
77
} from '@responsive-image/core';
88
import React, { useState } from 'react';
9-
import './responsive-image.css';
109

1110
export type ResponsiveImageLayout = 'responsive' | 'fixed';
1211

0 commit comments

Comments
 (0)