Skip to content

Commit 6959695

Browse files
committed
docs(changeset): fix: backward compatibility - add support for and deprecate the deprecated textExtensions field in raw plugin options
1 parent 07ce426 commit 6959695

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

.changeset/new-schools-accept.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"esbuild-raw-plugin": patch
3+
---
4+
5+
fix: backward compatibility - add support for and deprecate the deprecated textExtensions field in raw plugin options

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/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);

0 commit comments

Comments
 (0)