|
2 | 2 |
|
3 | 3 | [](https://github.com/react18-tools/esbuild-raw-plugin/actions/workflows/test.yml) [](https://codeclimate.com/github/react18-tools/esbuild-raw-plugin/maintainability) [](https://codecov.io/gh/react18-tools/esbuild-raw-plugin) [](https://www.npmjs.com/package/esbuild-raw-plugin) [](https://www.npmjs.com/package/esbuild-raw-plugin)  [](https://gitpod.io/from-referrer/) |
4 | 4 |
|
5 | | -Esbuild Raw Plugin is a comprehensive library designed to unlock the full potential of React 18 server components. It provides customizable loading animation components and a fullscreen loader container, seamlessly integrating with React and Next.js. |
| 5 | +**An ESBuild/TSUP plugin to import files as raw text.** |
| 6 | +Ideal for scenarios like importing code files for documentation, interactive tools like `react-live`, or other text-based use cases. |
6 | 7 |
|
7 | | -✅ Fully Treeshakable (import from `esbuild-raw-plugin/client/loader-container`) |
| 8 | +> <img src="https://raw.githubusercontent.com/mayank1513/mayank1513/main/popper.png" style="height: 20px"/> Star [this repository](https://github.com/react18-tools/esbuild-raw-plugin) and share it with your friends. |
8 | 9 |
|
9 | | -✅ Fully TypeScript Supported |
| 10 | +--- |
10 | 11 |
|
11 | | -✅ Leverages the power of React 18 Server components |
| 12 | +## Features |
12 | 13 |
|
13 | | -✅ Compatible with all React 18 build systems/tools/frameworks |
| 14 | +- Import any file (e.g., `.js`, `.ts`, `.css`, etc.) as raw text. |
| 15 | +- Works seamlessly with **ESBuild** and **TSUP**. |
| 16 | +- Perfect for documentation generators, live code editors, and similar tools. |
14 | 17 |
|
15 | | -✅ Documented with [Typedoc](https://react18-tools.github.io/esbuild-raw-plugin) ([Docs](https://react18-tools.github.io/esbuild-raw-plugin)) |
| 18 | +--- |
16 | 19 |
|
17 | | -✅ Examples for Next.js, Vite, and Remix |
| 20 | +## Installation |
18 | 21 |
|
19 | | -> <img src="https://raw.githubusercontent.com/mayank1513/mayank1513/main/popper.png" style="height: 20px"/> Please consider starring [this repository](https://github.com/react18-tools/esbuild-raw-plugin) and sharing it with your friends. |
| 22 | +Using npm: |
20 | 23 |
|
21 | | -## Getting Started |
| 24 | +```bash |
| 25 | +npm install esbuild-raw-plugin --save-dev |
| 26 | +``` |
22 | 27 |
|
23 | | -### Installation |
| 28 | +Using yarn: |
24 | 29 |
|
25 | 30 | ```bash |
26 | | -pnpm add esbuild-raw-plugin |
| 31 | +yarn add esbuild-raw-plugin --dev |
27 | 32 | ``` |
28 | 33 |
|
29 | | -**_or_** |
| 34 | +Using pnpm: |
30 | 35 |
|
31 | 36 | ```bash |
32 | | -npm install esbuild-raw-plugin |
| 37 | +pnpm add esbuild-raw-plugin --save-dev |
33 | 38 | ``` |
34 | 39 |
|
35 | | -**_or_** |
| 40 | +--- |
36 | 41 |
|
37 | | -```bash |
38 | | -yarn add esbuild-raw-plugin |
| 42 | +## Usage |
| 43 | + |
| 44 | +### ESBuild Configuration |
| 45 | + |
| 46 | +Add the plugin to your ESBuild configuration: |
| 47 | + |
| 48 | +```js |
| 49 | +import { build } from "esbuild"; |
| 50 | +import { raw } from "esbuild-raw-plugin"; |
| 51 | + |
| 52 | +build({ |
| 53 | + entryPoints: ["src/index.js"], |
| 54 | + bundle: true, |
| 55 | + outfile: "out.js", |
| 56 | + plugins: [raw()], |
| 57 | +}); |
39 | 58 | ``` |
40 | 59 |
|
41 | | -### Usage |
| 60 | +### TSUP Configuration |
42 | 61 |
|
43 | | -Using loaders is straightforward. |
| 62 | +Add the plugin to your TSUP configuration: |
44 | 63 |
|
45 | | -```tsx |
46 | | -import { Bars1 } from "esbuild-raw-plugin/dist/server/bars/bars1"; |
| 64 | +```js |
| 65 | +import { defineConfig } from "tsup"; |
| 66 | +import { raw } from "esbuild-raw-plugin"; |
| 67 | + |
| 68 | +export default defineConfig({ |
| 69 | + entry: ["src/index.ts"], |
| 70 | + outDir: "dist", |
| 71 | + plugins: [raw()], |
| 72 | +}); |
| 73 | +``` |
47 | 74 |
|
48 | | -export default function MyComponent() { |
49 | | - return someCondition ? <Bars1 /> : <>Something else...</>; |
| 75 | +--- |
| 76 | + |
| 77 | +## IDE Setup for IntelliSense and Type Checking |
| 78 | + |
| 79 | +Add following to your declaration file. If you do not have one, create `declarations.d.ts` file and add following. |
| 80 | + |
| 81 | +```typescript |
| 82 | +declare module "*?raw" { |
| 83 | + const value: string; |
| 84 | + export default value; |
50 | 85 | } |
51 | 86 | ``` |
52 | 87 |
|
53 | | -For detailed API and options, refer to [the API documentation](https://react18-tools.github.io/esbuild-raw-plugin). |
| 88 | +## Importing Files as Raw Text |
54 | 89 |
|
55 | | -**Using LoaderContainer** |
| 90 | +With the plugin enabled, you can import files as raw text directly: |
56 | 91 |
|
57 | | -`LoaderContainer` is a fullscreen component. You can add this component directly in your layout and then use `useLoader` hook to toggle its visibility. |
| 92 | +```js |
| 93 | +import myCode from "./example.js?raw"; |
58 | 94 |
|
59 | | -```tsx |
60 | | -// layout.tsx |
61 | | -<LoaderContainer /> |
62 | | - ... |
| 95 | +console.log(myCode); |
| 96 | +// Outputs the content of 'example.js' as a string. |
63 | 97 | ``` |
64 | 98 |
|
65 | | -```tsx |
66 | | -// some other page or component |
67 | | -import { useLoader } from "esbuild-raw-plugin/dist/hooks"; |
68 | | - |
69 | | -export default MyComponent() { |
70 | | - const { setLoading } = useLoader(); |
71 | | - useCallback(()=>{ |
72 | | - setLoading(true); |
73 | | - ...do some work |
74 | | - setLoading(false); |
75 | | - }, []) |
76 | | - ... |
77 | | -} |
| 99 | +### Supported File Types |
| 100 | + |
| 101 | +You can use `?raw` with any file type, including: |
| 102 | + |
| 103 | +- `.js`, `.ts`, `.jsx`, `.tsx` |
| 104 | +- `.css`, `.scss` |
| 105 | +- `.html` |
| 106 | +- `.md` |
| 107 | +- and more! |
| 108 | + |
| 109 | +--- |
| 110 | + |
| 111 | +## Example Use Case |
| 112 | + |
| 113 | +### Live Code Preview with `react-live` |
| 114 | + |
| 115 | +```jsx |
| 116 | +import React from "react"; |
| 117 | +import { LiveProvider, LiveEditor, LiveError, LivePreview } from "react-live"; |
| 118 | +import exampleCode from "./example.js?raw"; |
| 119 | + |
| 120 | +const App = () => ( |
| 121 | + <LiveProvider code={exampleCode}> |
| 122 | + <LiveEditor /> |
| 123 | + <LiveError /> |
| 124 | + <LivePreview /> |
| 125 | + </LiveProvider> |
| 126 | +); |
| 127 | + |
| 128 | +export default App; |
78 | 129 | ``` |
79 | 130 |
|
| 131 | +--- |
| 132 | + |
| 133 | +## Why Use `esbuild-raw-plugin`? |
| 134 | + |
| 135 | +- Simplifies importing files as raw text for documentation and live previews. |
| 136 | +- Seamlessly integrates with modern build tools like ESBuild and TSUP. |
| 137 | +- Lightweight and easy to configure. |
| 138 | + |
| 139 | +--- |
| 140 | + |
| 141 | +## Keywords |
| 142 | + |
| 143 | +`esbuild`, `esbuild-plugin`, `tsup-plugin`, `raw-text-import`, `import-as-text`, `file-loader`, `react-live`, `documentation-tools`, `frontend-tooling` |
| 144 | + |
| 145 | +--- |
| 146 | + |
| 147 | +## Contributing |
| 148 | + |
| 149 | +Contributions are welcome! |
| 150 | +Feel free to open issues or pull requests to improve the plugin. |
| 151 | + |
| 152 | +--- |
| 153 | + |
| 154 | +Let me know if you'd like further tweaks! 🚀 |
| 155 | + |
| 156 | + |
| 157 | + |
| 158 | +--- |
| 159 | + |
80 | 160 | ## License |
81 | 161 |
|
82 | 162 | This library is licensed under the MPL-2.0 open-source license. |
83 | 163 |
|
84 | | -> <img src="https://raw.githubusercontent.com/mayank1513/mayank1513/main/popper.png" style="height: 20px"/> Please consider enrolling in [our courses](https://mayank-chaudhari.vercel.app/courses) or [sponsoring](https://github.com/sponsors/mayank1513) our work. |
| 164 | +> <img src="https://raw.githubusercontent.com/mayank1513/mayank1513/main/popper.png" style="height: 20px"/> Please enroll in [our courses](https://mayank-chaudhari.vercel.app/courses) or [sponsor](https://github.com/sponsors/mayank1513) our work. |
85 | 165 |
|
86 | 166 | <hr /> |
87 | 167 |
|
|
0 commit comments