Skip to content
This repository was archived by the owner on Jul 28, 2024. It is now read-only.

Commit 88add1a

Browse files
committed
Feat: add imageLoader plugin.
1 parent 2d491bb commit 88add1a

File tree

5 files changed

+146
-0
lines changed

5 files changed

+146
-0
lines changed

plugin/imageLoader/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# denopack/plugin/imageLoader
2+
3+
This plugin allows you to import JPG, PNG, GIF, SVG, and WebP files.
4+
5+
## Options
6+
7+
### `dom`
8+
9+
Type: `Boolean`<br>
10+
Default: `false`
11+
12+
If `true`, instructs the plugin to generate an ES Module which exports a DOM `Image` which can be used with a browser's DOM. Otherwise, the plugin generates an ES Module which exports a `default const` containing the Base64 representation of the image.
13+
14+
Using this option set to `true`, the export can be used as such:
15+
16+
```js
17+
import logo from './rollup.png';
18+
document.body.appendChild(logo);
19+
```
20+
21+
### `exclude`
22+
23+
Type: `String` | `Array[...String]`<br>
24+
Default: `null`
25+
26+
A [minimatch pattern](https://github.com/isaacs/minimatch), or array of patterns, which specifies the files in the build the plugin should _ignore_. By default no files are ignored.
27+
28+
### `include`
29+
30+
Type: `String` | `Array[...String]`<br>
31+
Default: `null`
32+
33+
A [minimatch pattern](https://github.com/isaacs/minimatch), or array of patterns, which specifies the files in the build the plugin should operate on. By default all files are targeted.
34+
35+
## Attribution
36+
37+
This plugin is a Deno rewrite of [@rollup/plugin-image](https://github.com/rollup/plugins/tree/master/packages/image).
38+
39+
## Usage
40+
41+
```ts
42+
import { pluginImageLoader as image } from "https://deno.land/x/denopack@0.10.0/plugin/imageLoader/mod.ts";
43+
44+
export default {
45+
plugins: [image()],
46+
};
47+
```

plugin/imageLoader/deps.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default as svgToMiniDataURI } from "https://esm.sh/mini-svg-data-uri";

plugin/imageLoader/lock.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"https://cdn.esm.sh/v9/mini-svg-data-uri@1.2.3/esnext/mini-svg-data-uri.js": "ec4c1dca1595bd346d96d5e14a663de36c77ea0c5ccc5a905e4d008f47c94c92",
3+
"https://cdn.esm.sh/v9/mini-svg-data-uri@1.2.3/index.d.ts": "8d95dbb5f2d1730f766e3672c7140601ff5bc9d54613a77a10b5fe63e46c00f0",
4+
"https://esm.sh/mini-svg-data-uri": "c639270c03579e50ad6daf38a29a94496684b96947e23a897038a40e901625bf"
5+
}

plugin/imageLoader/mod.ts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import { path, Plugin } from "../../deps.ts";
2+
import { createFilter } from "../deps.ts";
3+
import { svgToMiniDataURI } from "./deps.ts";
4+
5+
const defaults = {
6+
dom: false,
7+
exclude: null,
8+
include: null,
9+
};
10+
11+
interface mimeTypesInterface {
12+
".jpg": string;
13+
".jpeg": string;
14+
".png": string;
15+
".gif": string;
16+
".svg": string;
17+
".webp": string;
18+
[key: string]: string;
19+
}
20+
21+
const mimeTypes: mimeTypesInterface = {
22+
".jpg": "image/jpeg",
23+
".jpeg": "image/jpeg",
24+
".png": "image/png",
25+
".gif": "image/gif",
26+
".svg": "image/svg+xml",
27+
".webp": "image/webp",
28+
};
29+
30+
const domTemplate = ({ dataUri }: { dataUri: string }) =>
31+
`
32+
const img = new Image();
33+
img.src = "${dataUri}";
34+
export default img;
35+
`;
36+
37+
const constTemplate = ({ dataUri }: { dataUri: string }) =>
38+
`
39+
const img = "${dataUri}";
40+
export default img;
41+
`;
42+
43+
type Opts = {
44+
dom?: boolean;
45+
exclude?: string | string[];
46+
include?: string | string[];
47+
};
48+
49+
const getDataUri = (
50+
{ format, isSvg, mime, source }: {
51+
format: string;
52+
isSvg: boolean;
53+
mime: string;
54+
source: string;
55+
},
56+
) => isSvg ? svgToMiniDataURI(source) : `data:${mime};${format},${source}`;
57+
58+
export function pluginImageLoader(opts: Opts = {}): Plugin {
59+
const options = Object.assign({}, defaults, opts) as Opts;
60+
const filter = createFilter(options.include, options.exclude);
61+
62+
return {
63+
name: "denopack-plugin-imageLoader",
64+
async load(id) {
65+
if (!filter(id)) {
66+
return null;
67+
}
68+
69+
const mime = mimeTypes[path.extname(id)];
70+
if (!mime) {
71+
// not an image
72+
return null;
73+
}
74+
75+
const isSvg = mime === mimeTypes[".svg"];
76+
const format = isSvg ? "utf-8" : "base64";
77+
const decoder = new TextDecoder("utf-8");
78+
const data = await Deno.readFile(new URL(id));
79+
const source = decoder.decode(data).replace(
80+
/[\r\n]+/gm,
81+
"",
82+
);
83+
const dataUri = getDataUri({ format, isSvg, mime, source });
84+
const code = options.dom
85+
? domTemplate({ dataUri })
86+
: constTemplate({ dataUri });
87+
return code.trim();
88+
},
89+
};
90+
}
91+
92+
export default pluginImageLoader;

plugin/mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export { pluginCacheLoader } from "./cacheLoader/mod.ts";
66
export type { Opts as CacheLoaderOptions } from "./cacheLoader/mod.ts";
77
export { pluginFileLoader } from "./fileLoader/mod.ts";
88
export type { Opts as FileLoaderOptions } from "./fileLoader/mod.ts";
9+
export * from "./imageLoader/mod.ts";
910
export * from "./terserTransform/mod.ts";
1011
export * from "./typescriptTransform/mod.ts";
1112
export * from "./typescriptCompile/mod.ts";

0 commit comments

Comments
 (0)