Skip to content

Commit d98e827

Browse files
committed
Update Readme
1 parent 3cabd5f commit d98e827

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,35 @@ console.log(myCode);
9696
// Outputs the content of 'example.js' as a string.
9797
```
9898

99+
### Good News:
100+
101+
With the latest update, you no longer need to specify the file extension explicitly.
102+
103+
```js
104+
import myCode from "./example?raw";
105+
```
106+
107+
This works seamlessly! Additionally, if you're exporting from files like `index.tsx`, `index.jsx`, etc., you can simplify imports. For example, if your file path is `my-lib/index.ts`, you can import the raw content like this:
108+
109+
```js
110+
import myCode from "./my-lib?raw";
111+
```
112+
113+
### Extension Options (Optional)
114+
115+
```ts
116+
export interface RawPluginOptions {
117+
/**
118+
* Extensions to check in order if the file does not exist.
119+
* If it's a directory, the plugin will look for `dir/index.[ext]`.
120+
* @defaultValue ["tsx", "ts", "jsx", "js", "mjs", "mts", "module.css", "module.scss", "css", "scss"]
121+
*
122+
* You can provide your own extensions to optimize build performance or extend the list based on your use case.
123+
*/
124+
ext?: string[];
125+
}
126+
```
127+
99128
### Supported File Types
100129

101130
You can use `?raw` with any file type, including:

lib/src/index.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ import fs from "node:fs";
33
import path from "node:path";
44

55
export interface RawPluginOptions {
6-
/** Extensions to check in order if the file does not exist.
7-
* If it is directory we check dir/index.[ext]
6+
/**
7+
* Extensions to check in order if the file does not exist.
8+
* If it's a directory, the plugin will look for `dir/index.[ext]`.
89
* @defaultValue ["tsx", "ts", "jsx", "js", "mjs", "mts", "module.css", "module.scss", "css", "scss"]
10+
*
11+
* You can provide your own extensions to optimize build performance or extend the list based on your use case.
912
*/
1013
ext?: string[];
1114
}
@@ -44,6 +47,10 @@ export const raw: (options?: RawPluginOptions) => Plugin = options => ({
4447
filePath += "." + e;
4548
break;
4649
}
50+
if (!fs.existsSync(filePath))
51+
throw new Error(
52+
`File not found: ${args.pluginData}\nWe checked for following extensions: ${ext.join(", ")}. You can customise by passing {ext: [...]} to raw({ext:[...]})`,
53+
);
4754
return {
4855
contents: fs.readFileSync(filePath, "utf8"),
4956
loader: "text",

0 commit comments

Comments
 (0)