Skip to content

Commit c43680a

Browse files
authored
Merge pull request #7 from react18-tools/backward-compatible
Backward compatible
2 parents 07ce426 + 997acf3 commit c43680a

File tree

6 files changed

+54
-7
lines changed

6 files changed

+54
-7
lines changed

lib/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
# esbuild-raw-plugin
22

3+
## 0.3.1
4+
5+
### Patch Changes
6+
7+
- 6959695: fix: backward compatibility - add support for and deprecate the deprecated textExtensions field in raw plugin options
8+
39
## 0.3.0
410

511
### Minor Changes
612

713
- a899336: ### ✨ Enhancements
14+
815
- Replaced `textExtensions` with `customLoaders` for fine-grained extension-to-loader mapping.
916
- Introduced `name` option for overriding the plugin name (useful for debugging or deduplication).
1017
- Added support for multiple query-based loaders: `?text`, `?base64`, `?dataurl`, `?file`, `?binary`.
1118
- Improved fallback logic for resolving files: now tries extensions or `index.[ext]` for folders.
1219
- Regex-based `onLoad` filtering boosts performance (leveraging Go-native ESBuild internals).
1320

1421
### 🛠 Internal Refactors
22+
1523
- Code refactored for better readability and maintainability.
1624
- Error messages are now clearer and more actionable.
1725
- Switched to consistent plugin naming (`"esbuild-raw-plugin"` instead of randomized suffix).

lib/__tests__/index.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ describe("Raw plugin", () => {
100100
const plugin = raw({ name: "custom-plugin-name" });
101101
expect(plugin.name).toBe("custom-plugin-name");
102102
});
103+
103104
test("custom loader", async ({ expect }) => {
104105
await esbuild.build({
105106
...buildOptions,
@@ -111,4 +112,17 @@ describe("Raw plugin", () => {
111112
const generatedCodeContent = (await import("./dist/test-loader.js")).getText();
112113
expect(generatedCodeContent).toBe(fileContent.toString("base64"));
113114
});
115+
116+
test("deprecated textExtensions", async ({ expect }) => {
117+
await esbuild.build({
118+
...buildOptions,
119+
plugins: [raw({ textExtensions: ["md"] })],
120+
entryPoints: [path.resolve(__dirname, "test3.ts")],
121+
outdir: "__tests__/dist3",
122+
});
123+
const fileContent = fs.readFileSync(path.resolve(__dirname, "test.md"), "utf-8");
124+
// @ts-ignore
125+
const generatedCodeContent = (await import("./dist3/test3.js")).getText();
126+
expect(generatedCodeContent).toBe(fileContent);
127+
});
114128
});

lib/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "esbuild-raw-plugin",
33
"author": "Mayank Kumar Chaudhari <https://mayank-chaudhari.vercel.app>",
44
"private": false,
5-
"version": "0.3.0",
5+
"version": "0.3.1",
66
"description": "An ESBuild and TSUP plugin that allows importing files as raw text. Useful for loading code files in documentation, interactive demos, or tools like react-live.",
77
"license": "MPL-2.0",
88
"main": "./dist/index.js",

lib/src/index.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ export interface RawPluginOptions {
4040
* Plugin name override (for debugging, deduplication, etc.)
4141
*/
4242
name?: string;
43+
44+
/**
45+
* @deprecated Use `customLoaders` instead.
46+
* Previously used to specify extensions to treat as text.
47+
*
48+
* Example replacement:
49+
* ```ts
50+
* customLoaders: { "module.scss": "text", "md": "text" }
51+
* ```
52+
*/
53+
textExtensions?: string[];
4354
}
4455

4556
/**
@@ -112,10 +123,14 @@ export const raw = (options?: RawPluginOptions): Plugin => ({
112123
return { contents: buffer, loader };
113124
});
114125

115-
if (options?.customLoaders) {
116-
const customLoaderKeys = Object.keys(options.customLoaders).sort(
117-
(a, b) => b.length - a.length,
118-
);
126+
if (options?.customLoaders || options?.textExtensions) {
127+
const customLoaderKeys = [
128+
...new Set([
129+
...Object.keys(options.customLoaders ?? {}),
130+
...(options?.textExtensions ?? []),
131+
]),
132+
].sort((a, b) => b.length - a.length);
133+
119134
const pattern = new RegExp(
120135
`\\.(${customLoaderKeys
121136
.map(e => e.replace(/^\./, "").replace(/[.*+?^${}()|[\]\\]/g, "\\$&"))
@@ -125,7 +140,10 @@ export const raw = (options?: RawPluginOptions): Plugin => ({
125140
build.onLoad({ filter: pattern }, args => {
126141
const path = args.path;
127142
const loaderKey = customLoaderKeys.find(suffix => path.endsWith(suffix));
128-
const loader = options.customLoaders?.[loaderKey ?? ""];
143+
let loader = options.customLoaders?.[loaderKey ?? ""];
144+
145+
if (!loader && options?.textExtensions?.includes(loaderKey ?? "")) loader = "text";
146+
129147
if (!loader) return;
130148

131149
const buffer = fs.readFileSync(path);

packages/shared/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# @repo/shared
22

3+
## 0.0.5
4+
5+
### Patch Changes
6+
7+
- Updated dependencies [6959695]
8+
9+
310
## 0.0.4
411

512
### Patch Changes

packages/shared/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@repo/shared",
3-
"version": "0.0.4",
3+
"version": "0.0.5",
44
"private": true,
55
"sideEffects": false,
66
"main": "./dist/index.js",

0 commit comments

Comments
 (0)