Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
# esbuild-raw-plugin

## 0.3.1

### Patch Changes

- 6959695: fix: backward compatibility - add support for and deprecate the deprecated textExtensions field in raw plugin options

## 0.3.0

### Minor Changes

- a899336: ### ✨ Enhancements

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

### 🛠 Internal Refactors

- Code refactored for better readability and maintainability.
- Error messages are now clearer and more actionable.
- Switched to consistent plugin naming (`"esbuild-raw-plugin"` instead of randomized suffix).
Expand Down
14 changes: 14 additions & 0 deletions lib/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ describe("Raw plugin", () => {
const plugin = raw({ name: "custom-plugin-name" });
expect(plugin.name).toBe("custom-plugin-name");
});

test("custom loader", async ({ expect }) => {
await esbuild.build({
...buildOptions,
Expand All @@ -111,4 +112,17 @@ describe("Raw plugin", () => {
const generatedCodeContent = (await import("./dist/test-loader.js")).getText();
expect(generatedCodeContent).toBe(fileContent.toString("base64"));
});

test("deprecated textExtensions", async ({ expect }) => {
await esbuild.build({
...buildOptions,
plugins: [raw({ textExtensions: ["md"] })],
entryPoints: [path.resolve(__dirname, "test3.ts")],
outdir: "__tests__/dist3",
});
const fileContent = fs.readFileSync(path.resolve(__dirname, "test.md"), "utf-8");
// @ts-ignore
const generatedCodeContent = (await import("./dist3/test3.js")).getText();
expect(generatedCodeContent).toBe(fileContent);
});
});
2 changes: 1 addition & 1 deletion lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "esbuild-raw-plugin",
"author": "Mayank Kumar Chaudhari <https://mayank-chaudhari.vercel.app>",
"private": false,
"version": "0.3.0",
"version": "0.3.1",
"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.",
"license": "MPL-2.0",
"main": "./dist/index.js",
Expand Down
28 changes: 23 additions & 5 deletions lib/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ export interface RawPluginOptions {
* Plugin name override (for debugging, deduplication, etc.)
*/
name?: string;

/**
* @deprecated Use `customLoaders` instead.
* Previously used to specify extensions to treat as text.
*
* Example replacement:
* ```ts
* customLoaders: { "module.scss": "text", "md": "text" }
* ```
*/
textExtensions?: string[];
}

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

if (options?.customLoaders) {
const customLoaderKeys = Object.keys(options.customLoaders).sort(
(a, b) => b.length - a.length,
);
if (options?.customLoaders || options?.textExtensions) {
const customLoaderKeys = [
...new Set([
...Object.keys(options.customLoaders ?? {}),
...(options?.textExtensions ?? []),
]),
].sort((a, b) => b.length - a.length);

const pattern = new RegExp(
`\\.(${customLoaderKeys
.map(e => e.replace(/^\./, "").replace(/[.*+?^${}()|[\]\\]/g, "\\$&"))
Expand All @@ -125,7 +140,10 @@ export const raw = (options?: RawPluginOptions): Plugin => ({
build.onLoad({ filter: pattern }, args => {
const path = args.path;
const loaderKey = customLoaderKeys.find(suffix => path.endsWith(suffix));
const loader = options.customLoaders?.[loaderKey ?? ""];
let loader = options.customLoaders?.[loaderKey ?? ""];

if (!loader && options?.textExtensions?.includes(loaderKey ?? "")) loader = "text";

if (!loader) return;

const buffer = fs.readFileSync(path);
Expand Down
7 changes: 7 additions & 0 deletions packages/shared/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @repo/shared

## 0.0.5

### Patch Changes

- Updated dependencies [6959695]
- esbuild-raw-plugin@0.3.1

## 0.0.4

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@repo/shared",
"version": "0.0.4",
"version": "0.0.5",
"private": true,
"sideEffects": false,
"main": "./dist/index.js",
Expand Down