unplugin-raw 0.5.1 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,12 +2,14 @@
2
2
 
3
3
  [![Unit Test](https://github.com/unplugin/unplugin-raw/actions/workflows/unit-test.yml/badge.svg)](https://github.com/unplugin/unplugin-raw/actions/workflows/unit-test.yml)
4
4
 
5
- Transform file to a default-export string. It will be transformed to JavaScript, then a string.
5
+ Transform file to a default-export string, and can be transformed by esbuild.
6
6
 
7
7
  ## Installation
8
8
 
9
9
  ```bash
10
10
  npm i -D unplugin-raw
11
+
12
+ npm i -D esbuild # Optional, if you want to transform TypeScript to JavaScript
11
13
  ```
12
14
 
13
15
  <details>
@@ -38,6 +40,20 @@ export default {
38
40
 
39
41
  <br></details>
40
42
 
43
+ <details>
44
+ <summary>Rolldown</summary><br>
45
+
46
+ ```ts
47
+ // rolldown.config.js
48
+ import Raw from 'unplugin-raw/rolldown'
49
+
50
+ export default {
51
+ plugins: [Raw()],
52
+ }
53
+ ```
54
+
55
+ <br></details>
56
+
41
57
  <details>
42
58
  <summary>esbuild</summary><br>
43
59
 
@@ -52,18 +68,28 @@ build({
52
68
 
53
69
  <br></details>
54
70
 
55
- <!-- <details>
56
- <summary>Webpack</summary><br>
71
+ ## Options
57
72
 
58
73
  ```ts
59
- // webpack.config.js
60
- module.exports = {
61
- /* ... */
62
- plugins: [require('unplugin-raw/webpack')()],
74
+ export interface TransformOptions {
75
+ /** @default [/\.[cm]?[jt]sx?$/] */
76
+ include?: FilterPattern
77
+ /** @default [/node_modules/] */
78
+ exclude?: FilterPattern
79
+ /** @default {} */
80
+ options?: EsbuildTransformOptions
63
81
  }
64
- ```
65
82
 
66
- <br></details> -->
83
+ export interface Options {
84
+ /** @default 'pre' */
85
+ enforce?: 'pre' | 'post' | undefined
86
+ /**
87
+ * Transform
88
+ * @default false
89
+ */
90
+ transform?: TransformOptions | boolean
91
+ }
92
+ ```
67
93
 
68
94
  ## Usage
69
95
 
package/dist/api.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ import { Options, OptionsResolved, TransformOptions, resolveOptions } from "./options-DupdgTT1.js";
2
+ import * as esbuild0 from "esbuild";
3
+ import { TransformOptions as TransformOptions$1 } from "esbuild";
4
+ import { Buffer } from "node:buffer";
5
+
6
+ //#region src/core/transform.d.ts
7
+ declare function transformRaw(file: string, isBytes: boolean, transform?: typeof esbuild0.transform, transformFilter?: (id: string | unknown) => boolean, options?: TransformOptions$1): Promise<string | Buffer>;
8
+ //#endregion
9
+ export { type Options, type OptionsResolved, type TransformOptions, resolveOptions, transformRaw };
package/dist/api.js ADDED
@@ -0,0 +1,3 @@
1
+ import { resolveOptions, transformRaw } from "./options-BTT0I76V.js";
2
+
3
+ export { resolveOptions, transformRaw };
package/dist/esbuild.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { unplugin } from "./index-CbkvuMB_.js";
1
+ import "./options-DupdgTT1.js";
2
+ import { unplugin } from "./index-BMXQlGcW.js";
2
3
 
3
4
  //#region src/esbuild.d.ts
4
5
 
package/dist/esbuild.js CHANGED
@@ -1,4 +1,5 @@
1
- import { src_default } from "./src-DE-h8IJW.js";
1
+ import "./options-BTT0I76V.js";
2
+ import { src_default } from "./src-BAcSYQaW.js";
2
3
 
3
4
  //#region src/esbuild.ts
4
5
  /**
@@ -0,0 +1,7 @@
1
+ import { Options } from "./options-DupdgTT1.js";
2
+ import { UnpluginInstance } from "unplugin";
3
+
4
+ //#region src/index.d.ts
5
+ declare const unplugin: UnpluginInstance<Options | undefined, false>;
6
+ //#endregion
7
+ export { unplugin };
package/dist/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
- import { guessLoader, unplugin } from "./index-CbkvuMB_.js";
2
- export { unplugin as default, guessLoader };
1
+ import "./options-DupdgTT1.js";
2
+ import { unplugin } from "./index-BMXQlGcW.js";
3
+ export { unplugin as default };
package/dist/index.js CHANGED
@@ -1,3 +1,4 @@
1
- import { guessLoader, src_default } from "./src-DE-h8IJW.js";
1
+ import "./options-BTT0I76V.js";
2
+ import { src_default } from "./src-BAcSYQaW.js";
2
3
 
3
- export { src_default as default, guessLoader };
4
+ export { src_default as default };
@@ -0,0 +1,55 @@
1
+ import { readFile } from "node:fs/promises";
2
+ import path from "node:path";
3
+
4
+ //#region src/core/transform.ts
5
+ async function transformRaw(file, isBytes, transform, transformFilter, options) {
6
+ let contents = await readFile(file, isBytes ? void 0 : "utf8");
7
+ if (!isBytes && transformFilter?.(file)) {
8
+ transform ||= (await import("esbuild")).transform;
9
+ contents = (await transform(contents, {
10
+ loader: guessLoader(file),
11
+ ...options
12
+ })).code;
13
+ }
14
+ return isBytes ? contents : `export default ${JSON.stringify(contents)}`;
15
+ }
16
+ const ExtToLoader = {
17
+ ".js": "js",
18
+ ".mjs": "js",
19
+ ".cjs": "js",
20
+ ".jsx": "jsx",
21
+ ".ts": "ts",
22
+ ".cts": "ts",
23
+ ".mts": "ts",
24
+ ".tsx": "tsx",
25
+ ".css": "css",
26
+ ".less": "css",
27
+ ".stylus": "css",
28
+ ".scss": "css",
29
+ ".sass": "css",
30
+ ".json": "json",
31
+ ".txt": "text"
32
+ };
33
+ function guessLoader(id) {
34
+ return ExtToLoader[path.extname(id).toLowerCase()] || "js";
35
+ }
36
+
37
+ //#endregion
38
+ //#region src/core/options.ts
39
+ function resolveOptions(options) {
40
+ let { transform = false } = options;
41
+ if (transform === true) transform = {};
42
+ return {
43
+ ...options,
44
+ enforce: "enforce" in options ? options.enforce : "pre",
45
+ transform: transform ? {
46
+ ...transform,
47
+ include: transform.include || [/\.[cm]?[jt]sx?$/],
48
+ exclude: transform.exclude || [/node_modules/],
49
+ options: transform.options || {}
50
+ } : false
51
+ };
52
+ }
53
+
54
+ //#endregion
55
+ export { resolveOptions, transformRaw };
@@ -0,0 +1,27 @@
1
+ import { FilterPattern } from "unplugin-utils";
2
+ import { TransformOptions } from "esbuild";
3
+
4
+ //#region src/core/options.d.ts
5
+ interface TransformOptions$1 {
6
+ /** @default [/\.[cm]?[jt]sx?$/] */
7
+ include?: FilterPattern;
8
+ /** @default [/node_modules/] */
9
+ exclude?: FilterPattern;
10
+ /** @default {} */
11
+ options?: TransformOptions;
12
+ }
13
+ interface Options {
14
+ /** @default 'pre' */
15
+ enforce?: "pre" | "post" | undefined;
16
+ /**
17
+ * Transform
18
+ * @default false
19
+ */
20
+ transform?: TransformOptions$1 | boolean;
21
+ }
22
+ type OptionsResolved = Pick<Options, "enforce"> & {
23
+ transform: TransformOptions$1 | false;
24
+ };
25
+ declare function resolveOptions(options: Options): OptionsResolved;
26
+ //#endregion
27
+ export { Options, OptionsResolved, TransformOptions$1 as TransformOptions, resolveOptions };
@@ -1,4 +1,5 @@
1
- import { unplugin } from "./index-CbkvuMB_.js";
1
+ import "./options-DupdgTT1.js";
2
+ import { unplugin } from "./index-BMXQlGcW.js";
2
3
 
3
4
  //#region src/rolldown.d.ts
4
5
 
package/dist/rolldown.js CHANGED
@@ -1,4 +1,5 @@
1
- import { src_default } from "./src-DE-h8IJW.js";
1
+ import "./options-BTT0I76V.js";
2
+ import { src_default } from "./src-BAcSYQaW.js";
2
3
 
3
4
  //#region src/rolldown.ts
4
5
  /**
package/dist/rollup.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { unplugin } from "./index-CbkvuMB_.js";
1
+ import "./options-DupdgTT1.js";
2
+ import { unplugin } from "./index-BMXQlGcW.js";
2
3
 
3
4
  //#region src/rollup.d.ts
4
5
 
package/dist/rollup.js CHANGED
@@ -1,4 +1,5 @@
1
- import { src_default } from "./src-DE-h8IJW.js";
1
+ import "./options-BTT0I76V.js";
2
+ import { src_default } from "./src-BAcSYQaW.js";
2
3
 
3
4
  //#region src/rollup.ts
4
5
  /**
package/dist/rspack.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { unplugin } from "./index-CbkvuMB_.js";
1
+ import "./options-DupdgTT1.js";
2
+ import { unplugin } from "./index-BMXQlGcW.js";
2
3
 
3
4
  //#region src/rspack.d.ts
4
5
 
package/dist/rspack.js CHANGED
@@ -1,4 +1,5 @@
1
- import { src_default } from "./src-DE-h8IJW.js";
1
+ import "./options-BTT0I76V.js";
2
+ import { src_default } from "./src-BAcSYQaW.js";
2
3
 
3
4
  //#region src/rspack.ts
4
5
  /**
@@ -0,0 +1,58 @@
1
+ import { resolveOptions, transformRaw } from "./options-BTT0I76V.js";
2
+ import { createUnplugin } from "unplugin";
3
+ import { createFilter } from "unplugin-utils";
4
+
5
+ //#region src/index.ts
6
+ const rawRE = /[&?]raw(?:&|$)/;
7
+ const postfixRE = /[#?].*$/s;
8
+ function cleanUrl(url) {
9
+ return url.replace(postfixRE, "");
10
+ }
11
+ const unplugin = createUnplugin((rawOptions = {}, meta) => {
12
+ const options = resolveOptions(rawOptions);
13
+ const transformFilter = options.transform ? createFilter(options.transform.include, options.transform.exclude) : void 0;
14
+ return {
15
+ name: "unplugin-raw",
16
+ enforce: options.enforce,
17
+ resolveId: [
18
+ "rollup",
19
+ "rolldown",
20
+ "vite"
21
+ ].includes(meta.framework) ? async function(id, importer, options$1) {
22
+ const attributeType = options$1?.attributes?.type;
23
+ if (attributeType === "text") id += `${id.includes("?") ? "&" : "?"}raw`;
24
+ else if (attributeType === "bytes") id += `${id.includes("?") ? "&" : "?"}bytes`;
25
+ if (!rawRE.test(id)) return;
26
+ const file = cleanUrl(id);
27
+ const resolved = await this.resolve(file, importer);
28
+ if (!resolved) return;
29
+ return id.replace(file, resolved.id);
30
+ } : void 0,
31
+ load: {
32
+ filter: { id: { include: rawRE } },
33
+ async handler(id) {
34
+ const file = cleanUrl(id);
35
+ const context = this.getNativeBuildContext?.();
36
+ const transform = context?.framework === "esbuild" ? context.build.esbuild.transform : void 0;
37
+ const contents = await transformRaw(file, false, transform, transformFilter, options.transform ? options.transform.options : void 0);
38
+ return contents;
39
+ }
40
+ },
41
+ esbuild: { setup(build) {
42
+ build.onLoad({ filter: /.*/ }, async (args) => {
43
+ const isBytes = args.with.type === "bytes";
44
+ if (args.with.type === "text" || isBytes) {
45
+ const contents = await transformRaw(args.path, isBytes, build.esbuild.transform, transformFilter, options.transform ? options.transform.options : void 0);
46
+ return {
47
+ contents,
48
+ loader: typeof contents === "string" ? "js" : "binary"
49
+ };
50
+ }
51
+ });
52
+ } }
53
+ };
54
+ });
55
+ var src_default = unplugin;
56
+
57
+ //#endregion
58
+ export { src_default };
package/dist/vite.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { unplugin } from "./index-CbkvuMB_.js";
1
+ import "./options-DupdgTT1.js";
2
+ import { unplugin } from "./index-BMXQlGcW.js";
2
3
 
3
4
  //#region src/vite.d.ts
4
5
 
package/dist/vite.js CHANGED
@@ -1,4 +1,5 @@
1
- import { src_default } from "./src-DE-h8IJW.js";
1
+ import "./options-BTT0I76V.js";
2
+ import { src_default } from "./src-BAcSYQaW.js";
2
3
 
3
4
  //#region src/vite.ts
4
5
  /**
package/dist/webpack.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { unplugin } from "./index-CbkvuMB_.js";
1
+ import "./options-DupdgTT1.js";
2
+ import { unplugin } from "./index-BMXQlGcW.js";
2
3
 
3
4
  //#region src/webpack.d.ts
4
5
 
package/dist/webpack.js CHANGED
@@ -1,4 +1,5 @@
1
- import { src_default } from "./src-DE-h8IJW.js";
1
+ import "./options-BTT0I76V.js";
2
+ import { src_default } from "./src-BAcSYQaW.js";
2
3
 
3
4
  //#region src/webpack.ts
4
5
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "unplugin-raw",
3
- "version": "0.5.1",
3
+ "version": "0.6.0",
4
4
  "description": "Transform file to a default-export string.",
5
5
  "type": "module",
6
6
  "keywords": [
@@ -48,23 +48,31 @@
48
48
  "publishConfig": {
49
49
  "access": "public"
50
50
  },
51
+ "peerDependencies": {
52
+ "esbuild": ">=0.25.0"
53
+ },
54
+ "peerDependenciesMeta": {
55
+ "esbuild": {
56
+ "optional": true
57
+ }
58
+ },
51
59
  "dependencies": {
52
- "esbuild": "^0.25.8",
53
60
  "unplugin": "^2.3.5",
54
- "unplugin-utils": "^0.2.4"
61
+ "unplugin-utils": "^0.2.5"
55
62
  },
56
63
  "devDependencies": {
57
64
  "@sxzz/eslint-config": "^7.1.2",
58
65
  "@sxzz/prettier-config": "^2.2.3",
59
- "@types/node": "^24.1.0",
60
- "bumpp": "^10.2.2",
61
- "eslint": "^9.32.0",
66
+ "@types/node": "^24.2.1",
67
+ "bumpp": "^10.2.3",
68
+ "esbuild": "^0.25.9",
69
+ "eslint": "^9.33.0",
62
70
  "prettier": "^3.6.2",
63
71
  "rollup": "^4.46.2",
64
- "tsdown": "^0.13.2",
65
- "tsx": "^4.20.3",
72
+ "tsdown": "^0.14.1",
73
+ "tsx": "^4.20.4",
66
74
  "typescript": "^5.9.2",
67
- "vite": "^7.0.6",
75
+ "vite": "^7.1.2",
68
76
  "vitest": "^3.2.4"
69
77
  },
70
78
  "engines": {
@@ -1,24 +0,0 @@
1
- import { UnpluginInstance } from "unplugin";
2
- import { FilterPattern } from "unplugin-utils";
3
- import { Loader, TransformOptions } from "esbuild";
4
-
5
- //#region src/core/options.d.ts
6
- interface Options {
7
- /** @default 'pre' */
8
- enforce?: "pre" | "post" | undefined;
9
- /** Transform */
10
- transform?: {
11
- /** @default [/\.[cm]?[jt]sx?$/] */
12
- include?: FilterPattern;
13
- /** @default [/node_modules/] */
14
- exclude?: FilterPattern;
15
- /** @default {} */
16
- options?: TransformOptions;
17
- };
18
- }
19
- //#endregion
20
- //#region src/index.d.ts
21
- declare const unplugin: UnpluginInstance<Options | undefined, false>;
22
- declare function guessLoader(id: string): Loader;
23
- //#endregion
24
- export { guessLoader, unplugin };
@@ -1,105 +0,0 @@
1
- import { readFile } from "node:fs/promises";
2
- import path from "node:path";
3
- import { createUnplugin } from "unplugin";
4
- import { createFilter } from "unplugin-utils";
5
-
6
- //#region src/core/options.ts
7
- function resolveOptions(options) {
8
- return {
9
- ...options,
10
- enforce: "enforce" in options ? options.enforce : "pre",
11
- transform: {
12
- ...options.transform,
13
- include: options.transform?.include || [/\.[cm]?[jt]sx?$/],
14
- exclude: options.transform?.exclude || [/node_modules/],
15
- options: options.transform?.options || {}
16
- }
17
- };
18
- }
19
-
20
- //#endregion
21
- //#region src/index.ts
22
- const unplugin = createUnplugin((rawOptions = {}, meta) => {
23
- const options = resolveOptions(rawOptions);
24
- const transformFilter = createFilter(options.transform.include, options.transform.exclude);
25
- return {
26
- name: "unplugin-raw",
27
- enforce: options.enforce,
28
- resolveId: [
29
- "rollup",
30
- "rolldown",
31
- "vite"
32
- ].includes(meta.framework) ? async function(id, importer, opt) {
33
- const attributeType = opt?.attributes?.type;
34
- if (attributeType === "text") id += `${id.includes("?") ? "&" : "?"}raw`;
35
- else if (attributeType === "bytes") id += `${id.includes("?") ? "&" : "?"}bytes`;
36
- if (!rawRE.test(id)) return;
37
- const file = cleanUrl(id);
38
- const resolved = await this.resolve(file, importer);
39
- if (!resolved) return;
40
- return id.replace(file, resolved.id);
41
- } : void 0,
42
- load: {
43
- filter: { id: { include: rawRE } },
44
- async handler(id) {
45
- const file = cleanUrl(id);
46
- const context = this.getNativeBuildContext?.();
47
- const transform = context?.framework === "esbuild" ? context.build.esbuild.transform : void 0;
48
- const contents = await transformRaw(file, transformFilter, false, options.transform.options, transform);
49
- return contents;
50
- }
51
- },
52
- esbuild: { setup(build) {
53
- build.onLoad({ filter: /.*/ }, async (args) => {
54
- const isBytes = args.with.type === "bytes";
55
- if (args.with.type === "text" || isBytes) {
56
- const contents = await transformRaw(args.path, transformFilter, isBytes, options.transform.options, build.esbuild.transform);
57
- return {
58
- contents,
59
- loader: typeof contents === "string" ? "js" : "binary"
60
- };
61
- }
62
- });
63
- } }
64
- };
65
- });
66
- var src_default = unplugin;
67
- const rawRE = /[&?]raw(?:&|$)/;
68
- const postfixRE = /[#?].*$/s;
69
- function cleanUrl(url) {
70
- return url.replace(postfixRE, "");
71
- }
72
- const ExtToLoader = {
73
- ".js": "js",
74
- ".mjs": "js",
75
- ".cjs": "js",
76
- ".jsx": "jsx",
77
- ".ts": "ts",
78
- ".cts": "ts",
79
- ".mts": "ts",
80
- ".tsx": "tsx",
81
- ".css": "css",
82
- ".less": "css",
83
- ".stylus": "css",
84
- ".scss": "css",
85
- ".sass": "css",
86
- ".json": "json",
87
- ".txt": "text"
88
- };
89
- function guessLoader(id) {
90
- return ExtToLoader[path.extname(id).toLowerCase()] || "js";
91
- }
92
- async function transformRaw(file, transformFilter, isBytes, options, transform) {
93
- let contents = await readFile(file, isBytes ? void 0 : "utf8");
94
- if (!isBytes && transformFilter(file)) {
95
- transform ||= (await import("esbuild")).transform;
96
- contents = (await transform(contents, {
97
- loader: guessLoader(file),
98
- ...options
99
- })).code;
100
- }
101
- return isBytes ? contents : `export default ${JSON.stringify(contents)}`;
102
- }
103
-
104
- //#endregion
105
- export { guessLoader, src_default };