unplugin-raw 0.5.1 → 0.6.1

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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright © 2023-PRESENT 三咲智子 (https://github.com/sxzz)
3
+ Copyright © 2023-PRESENT Kevin Deng (https://github.com/sxzz)
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -1,13 +1,17 @@
1
- # unplugin-raw [![npm](https://img.shields.io/npm/v/unplugin-raw.svg)](https://npmjs.com/package/unplugin-raw)
1
+ # unplugin-raw
2
2
 
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)
3
+ [![npm version][npm-version-src]][npm-version-href]
4
+ [![npm downloads][npm-downloads-src]][npm-downloads-href]
5
+ [![Unit Test][unit-test-src]][unit-test-href]
4
6
 
5
- Transform file to a default-export string. It will be transformed to JavaScript, then a string.
7
+ Transform file to a default-export string, and can be transformed by esbuild.
6
8
 
7
9
  ## Installation
8
10
 
9
11
  ```bash
10
12
  npm i -D unplugin-raw
13
+
14
+ npm i -D esbuild # Optional, if you want to transform TypeScript to JavaScript
11
15
  ```
12
16
 
13
17
  <details>
@@ -38,6 +42,20 @@ export default {
38
42
 
39
43
  <br></details>
40
44
 
45
+ <details>
46
+ <summary>Rolldown</summary><br>
47
+
48
+ ```ts
49
+ // rolldown.config.js
50
+ import Raw from 'unplugin-raw/rolldown'
51
+
52
+ export default {
53
+ plugins: [Raw()],
54
+ }
55
+ ```
56
+
57
+ <br></details>
58
+
41
59
  <details>
42
60
  <summary>esbuild</summary><br>
43
61
 
@@ -52,18 +70,28 @@ build({
52
70
 
53
71
  <br></details>
54
72
 
55
- <!-- <details>
56
- <summary>Webpack</summary><br>
73
+ ## Options
57
74
 
58
75
  ```ts
59
- // webpack.config.js
60
- module.exports = {
61
- /* ... */
62
- plugins: [require('unplugin-raw/webpack')()],
76
+ export interface TransformOptions {
77
+ /** @default [/\.[cm]?[jt]sx?$/] */
78
+ include?: FilterPattern
79
+ /** @default [/node_modules/] */
80
+ exclude?: FilterPattern
81
+ /** @default {} */
82
+ options?: EsbuildTransformOptions
63
83
  }
64
- ```
65
84
 
66
- <br></details> -->
85
+ export interface Options {
86
+ /** @default 'pre' */
87
+ enforce?: 'pre' | 'post' | undefined
88
+ /**
89
+ * Transform
90
+ * @default false
91
+ */
92
+ transform?: TransformOptions | boolean
93
+ }
94
+ ```
67
95
 
68
96
  ## Usage
69
97
 
@@ -84,4 +112,13 @@ import text4 from './with.js' with { type: 'text' }
84
112
 
85
113
  ## License
86
114
 
87
- [MIT](./LICENSE) License © 2023-PRESENT [三咲智子](https://github.com/sxzz)
115
+ [MIT](./LICENSE) License © 2023-PRESENT [Kevin Deng](https://github.com/sxzz)
116
+
117
+ <!-- Badges -->
118
+
119
+ [npm-version-src]: https://img.shields.io/npm/v/unplugin-raw.svg
120
+ [npm-version-href]: https://npmjs.com/package/unplugin-raw
121
+ [npm-downloads-src]: https://img.shields.io/npm/dm/unplugin-raw
122
+ [npm-downloads-href]: https://www.npmcharts.com/compare/unplugin-raw?interval=30
123
+ [unit-test-src]: https://github.com/unplugin/unplugin-raw/actions/workflows/unit-test.yml/badge.svg
124
+ [unit-test-href]: https://github.com/unplugin/unplugin-raw/actions/workflows/unit-test.yml
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 { unplugin } from "./src-Dwv4ealK.js";
2
3
 
3
4
  //#region src/esbuild.ts
4
5
  /**
@@ -14,8 +15,7 @@ import { src_default } from "./src-DE-h8IJW.js";
14
15
  * })
15
16
  * ```
16
17
  */
17
- const esbuild = src_default.esbuild;
18
- var esbuild_default = esbuild;
18
+ const esbuild = unplugin.esbuild;
19
19
 
20
20
  //#endregion
21
- export { esbuild_default as default, esbuild as "module.exports" };
21
+ export { esbuild as default, esbuild as "module.exports" };
@@ -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 { unplugin } from "./src-Dwv4ealK.js";
2
3
 
3
- export { src_default as default, guessLoader };
4
+ export { unplugin 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 { unplugin } from "./src-Dwv4ealK.js";
2
3
 
3
4
  //#region src/rolldown.ts
4
5
  /**
@@ -14,8 +15,7 @@ import { src_default } from "./src-DE-h8IJW.js";
14
15
  * }
15
16
  * ```
16
17
  */
17
- const rolldown = src_default.rolldown;
18
- var rolldown_default = rolldown;
18
+ const rolldown = unplugin.rolldown;
19
19
 
20
20
  //#endregion
21
- export { rolldown_default as default, rolldown as "module.exports" };
21
+ export { rolldown as default, rolldown as "module.exports" };
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 { unplugin } from "./src-Dwv4ealK.js";
2
3
 
3
4
  //#region src/rollup.ts
4
5
  /**
@@ -14,8 +15,7 @@ import { src_default } from "./src-DE-h8IJW.js";
14
15
  * }
15
16
  * ```
16
17
  */
17
- const rollup = src_default.rollup;
18
- var rollup_default = rollup;
18
+ const rollup = unplugin.rollup;
19
19
 
20
20
  //#endregion
21
- export { rollup_default as default, rollup as "module.exports" };
21
+ export { rollup as default, rollup as "module.exports" };
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 { unplugin } from "./src-Dwv4ealK.js";
2
3
 
3
4
  //#region src/rspack.ts
4
5
  /**
@@ -12,8 +13,7 @@ import { src_default } from "./src-DE-h8IJW.js";
12
13
  * }
13
14
  * ```
14
15
  */
15
- const rspack = src_default.rspack;
16
- var rspack_default = rspack;
16
+ const rspack = unplugin.rspack;
17
17
 
18
18
  //#endregion
19
- export { rspack_default as default, rspack as "module.exports" };
19
+ export { rspack as default, rspack as "module.exports" };
@@ -0,0 +1,57 @@
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
+
56
+ //#endregion
57
+ export { unplugin };
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 { unplugin } from "./src-Dwv4ealK.js";
2
3
 
3
4
  //#region src/vite.ts
4
5
  /**
@@ -14,8 +15,7 @@ import { src_default } from "./src-DE-h8IJW.js";
14
15
  * })
15
16
  * ```
16
17
  */
17
- const vite = src_default.vite;
18
- var vite_default = vite;
18
+ const vite = unplugin.vite;
19
19
 
20
20
  //#endregion
21
- export { vite_default as default, vite as "module.exports" };
21
+ export { vite as default, vite as "module.exports" };
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 { unplugin } from "./src-Dwv4ealK.js";
2
3
 
3
4
  //#region src/webpack.ts
4
5
  /**
@@ -12,8 +13,7 @@ import { src_default } from "./src-DE-h8IJW.js";
12
13
  * }
13
14
  * ```
14
15
  */
15
- const webpack = src_default.webpack;
16
- var webpack_default = webpack;
16
+ const webpack = unplugin.webpack;
17
17
 
18
18
  //#endregion
19
- export { webpack_default as default, webpack as "module.exports" };
19
+ export { webpack as default, webpack as "module.exports" };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "unplugin-raw",
3
- "version": "0.5.1",
3
+ "version": "0.6.1",
4
4
  "description": "Transform file to a default-export string.",
5
5
  "type": "module",
6
6
  "keywords": [
@@ -19,7 +19,7 @@
19
19
  "type": "git",
20
20
  "url": "git+https://github.com/unplugin/unplugin-raw.git"
21
21
  },
22
- "author": "三咲智子 Kevin Deng <sxzz@sxzz.moe>",
22
+ "author": "Kevin Deng <sxzz@sxzz.moe>",
23
23
  "funding": "https://github.com/sponsors/sxzz",
24
24
  "files": [
25
25
  "dist"
@@ -29,13 +29,14 @@
29
29
  "types": "./dist/index.d.ts",
30
30
  "exports": {
31
31
  ".": "./dist/index.js",
32
+ "./api": "./dist/api.js",
33
+ "./esbuild": "./dist/esbuild.js",
34
+ "./rolldown": "./dist/rolldown.js",
35
+ "./rollup": "./dist/rollup.js",
36
+ "./rspack": "./dist/rspack.js",
32
37
  "./vite": "./dist/vite.js",
33
38
  "./webpack": "./dist/webpack.js",
34
- "./rspack": "./dist/rspack.js",
35
- "./rollup": "./dist/rollup.js",
36
- "./rolldown": "./dist/rolldown.js",
37
- "./esbuild": "./dist/esbuild.js",
38
- "./*": "./*"
39
+ "./package.json": "./package.json"
39
40
  },
40
41
  "typesVersions": {
41
42
  "*": {
@@ -48,23 +49,30 @@
48
49
  "publishConfig": {
49
50
  "access": "public"
50
51
  },
52
+ "peerDependencies": {
53
+ "esbuild": ">=0.25.0"
54
+ },
55
+ "peerDependenciesMeta": {
56
+ "esbuild": {
57
+ "optional": true
58
+ }
59
+ },
51
60
  "dependencies": {
52
- "esbuild": "^0.25.8",
53
61
  "unplugin": "^2.3.5",
54
- "unplugin-utils": "^0.2.4"
62
+ "unplugin-utils": "^0.2.5"
55
63
  },
56
64
  "devDependencies": {
57
65
  "@sxzz/eslint-config": "^7.1.2",
58
66
  "@sxzz/prettier-config": "^2.2.3",
59
- "@types/node": "^24.1.0",
60
- "bumpp": "^10.2.2",
61
- "eslint": "^9.32.0",
67
+ "@types/node": "^24.2.1",
68
+ "bumpp": "^10.2.3",
69
+ "esbuild": "^0.25.9",
70
+ "eslint": "^9.33.0",
62
71
  "prettier": "^3.6.2",
63
72
  "rollup": "^4.46.2",
64
- "tsdown": "^0.13.2",
65
- "tsx": "^4.20.3",
73
+ "tsdown": "^0.14.1",
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": {
@@ -78,6 +86,6 @@
78
86
  "dev": "tsdown --watch",
79
87
  "test": "vitest",
80
88
  "typecheck": "tsc --noEmit",
81
- "release": "bumpp && pnpm publish"
89
+ "release": "bumpp"
82
90
  }
83
91
  }
@@ -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 };