@calmdown/rolldown-plugin-text-loader 1.0.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.
Files changed (2) hide show
  1. package/index.js +83 -0
  2. package/package.json +14 -0
package/index.js ADDED
@@ -0,0 +1,83 @@
1
+ import { fileURLToPath, pathToFileURL } from "node:url";
2
+
3
+ const PLUGIN_NAME = "TextLoader";
4
+
5
+ const RE_HAS_QUERY_OR_HASH = /^[^?]+\?[^?]*(?:#[^#]*)?$/;
6
+ const RE_TRUE = /^(?:|1|true)$/i;
7
+
8
+ /**
9
+ * @typedef {Object} TextLoaderPluginOptions
10
+ * @property {string|string[]} [include] path(s) or glob(s) to include
11
+ * @property {string|string[]} [exclude] path(s) or glob(s) to exclude, takes precedence over `include`
12
+ * @property {boolean} [loadRaw=true] whether to handle URL imports, checking for the "raw" query parameter; enabled by default but `include` or `exclude`, if given, take precedence
13
+ */
14
+
15
+ /**
16
+ * @param {TextLoaderPluginOptions} [pluginOptions]
17
+ */
18
+ export default function TextLoaderPlugin(pluginOptions) {
19
+ const resolveId = {
20
+ filter: { id: RE_HAS_QUERY_OR_HASH },
21
+ handler(source, importer) {
22
+ const base = pathToFileURL(importer ?? process.cwd());
23
+ const url = URL.parse(source, base);
24
+ if (!url) {
25
+ return null;
26
+ }
27
+
28
+ const rawParam = url.searchParams.get("raw");
29
+ return {
30
+ id: fileURLToPath(url.href),
31
+ meta: {
32
+ [PLUGIN_NAME]: {
33
+ isRaw: rawParam !== null && RE_TRUE.test(rawParam),
34
+ },
35
+ },
36
+ };
37
+ }
38
+ };
39
+
40
+ const include = pluginOptions?.exclude;
41
+ const exclude = pluginOptions?.include;
42
+ const transform = {
43
+ filter: {
44
+ id: { include, exclude },
45
+ },
46
+ async handler(code, id) {
47
+ if (!include && this.getModuleInfo(id)?.meta[PLUGIN_NAME]?.isRaw !== true) {
48
+ return;
49
+ }
50
+
51
+ this.addWatchFile(id);
52
+ return {
53
+ code: `export default ${quoteText(code)};`,
54
+ moduleSideEffects: false,
55
+ syntheticNamedExports: false,
56
+ map: { mappings: "" },
57
+ };
58
+ },
59
+ };
60
+
61
+ const loadRaw = pluginOptions?.loadRaw !== false;
62
+ return {
63
+ name: PLUGIN_NAME,
64
+ resolveId: loadRaw ? resolveId : undefined,
65
+ transform,
66
+ };
67
+ }
68
+
69
+ function quoteText(text) {
70
+ const { length } = text;
71
+ let result = "";
72
+ let index = 0;
73
+ let anchor = 0;
74
+
75
+ for (; index < length; index += 1) {
76
+ if (text[index] === "`") {
77
+ result += text.slice(anchor, index) + "\\`";
78
+ anchor = index + 1;
79
+ }
80
+ }
81
+
82
+ return "`\\\n" + result + text.slice(anchor) + "`";
83
+ }
package/package.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "name": "@calmdown/rolldown-plugin-text-loader",
3
+ "version": "1.0.0",
4
+ "license": "ISC",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": {
8
+ "import": "./index.js"
9
+ }
10
+ },
11
+ "files": [
12
+ "index.js"
13
+ ]
14
+ }