@deshlo/loader 0.1.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/index.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ declare function sourceInspectorLoader(
2
+ this: unknown,
3
+ source: string,
4
+ inputSourceMap?: unknown
5
+ ): void;
6
+
7
+ export = sourceInspectorLoader;
@@ -0,0 +1,308 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") {
10
+ for (let key of __getOwnPropNames(from))
11
+ if (!__hasOwnProp.call(to, key) && key !== except)
12
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
13
+ }
14
+ return to;
15
+ };
16
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
17
+ // If the importer is in node compatibility mode or this is not an ESM
18
+ // file that has been converted to a CommonJS file using a Babel-
19
+ // compatible transform (i.e. "__esModule" has not been set), then set
20
+ // "default" to the CommonJS "module.exports" for node compatibility.
21
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
+ mod
23
+ ));
24
+
25
+ // src/constants.ts
26
+ var DEFAULT_ATTRIBUTE_NAME = "data-src-loc";
27
+ var DEFAULT_REVISION_ATTRIBUTE_NAME = "data-src-rev";
28
+ var BABEL_PARSER_PLUGINS = [
29
+ "jsx",
30
+ "typescript",
31
+ "classProperties",
32
+ "classPrivateProperties",
33
+ "classPrivateMethods",
34
+ "dynamicImport",
35
+ "importMeta",
36
+ "topLevelAwait"
37
+ ];
38
+
39
+ // src/transform.ts
40
+ var parser = __toESM(require("@babel/parser"));
41
+ var import_generator = __toESM(require("@babel/generator"));
42
+ var import_traverse = __toESM(require("@babel/traverse"));
43
+ var t2 = __toESM(require("@babel/types"));
44
+
45
+ // src/utils/jsx.ts
46
+ var t = __toESM(require("@babel/types"));
47
+ function isHostElement(nameNode) {
48
+ return t.isJSXIdentifier(nameNode) && /^[a-z]/.test(nameNode.name);
49
+ }
50
+ function hasJsxElementChildren(node) {
51
+ return node.children.some((child) => t.isJSXElement(child) || t.isJSXFragment(child));
52
+ }
53
+
54
+ // src/utils/path.ts
55
+ var import_node_path = __toESM(require("node:path"));
56
+ function toRelativePath(absolutePath, cwd = process.cwd()) {
57
+ return import_node_path.default.relative(cwd, absolutePath).replace(/\\/g, "/");
58
+ }
59
+
60
+ // src/utils/text.ts
61
+ function splitMeaningfulText(value) {
62
+ let firstNonWhitespace = -1;
63
+ for (let index = 0; index < value.length; index += 1) {
64
+ if (value[index].trim() !== "") {
65
+ firstNonWhitespace = index;
66
+ break;
67
+ }
68
+ }
69
+ if (firstNonWhitespace === -1) {
70
+ return null;
71
+ }
72
+ let lastNonWhitespace = -1;
73
+ for (let index = value.length - 1; index >= 0; index -= 1) {
74
+ if (value[index].trim() !== "") {
75
+ lastNonWhitespace = index;
76
+ break;
77
+ }
78
+ }
79
+ if (lastNonWhitespace === -1) {
80
+ return null;
81
+ }
82
+ return {
83
+ leading: value.slice(0, firstNonWhitespace),
84
+ text: value.slice(firstNonWhitespace, lastNonWhitespace + 1),
85
+ trailing: value.slice(lastNonWhitespace + 1)
86
+ };
87
+ }
88
+ function advanceLocation(start, value) {
89
+ let line = start.line;
90
+ let column = start.column;
91
+ for (const character of value) {
92
+ if (character === "\n") {
93
+ line += 1;
94
+ column = 0;
95
+ continue;
96
+ }
97
+ if (character === "\r") {
98
+ continue;
99
+ }
100
+ column += 1;
101
+ }
102
+ return { line, column };
103
+ }
104
+
105
+ // src/transform.ts
106
+ function transformSource(source, resourcePath, options) {
107
+ if (typeof source !== "string" || !source.includes("<")) {
108
+ return { code: source, map: null, changed: false };
109
+ }
110
+ let ast;
111
+ try {
112
+ ast = parser.parse(source, {
113
+ sourceType: "unambiguous",
114
+ errorRecovery: true,
115
+ plugins: [...BABEL_PARSER_PLUGINS]
116
+ });
117
+ } catch {
118
+ return { code: source, map: null, changed: false };
119
+ }
120
+ const relativePath = toRelativePath(resourcePath);
121
+ let changed = false;
122
+ (0, import_traverse.default)(ast, {
123
+ JSXOpeningElement(openingPath) {
124
+ const nameNode = openingPath.node.name;
125
+ if (!isHostElement(nameNode)) {
126
+ return;
127
+ }
128
+ if (!openingPath.node.loc) {
129
+ return;
130
+ }
131
+ if (options.annotateLeafNodesOnly) {
132
+ const parentNode = openingPath.parentPath.node;
133
+ if (t2.isJSXElement(parentNode) && hasJsxElementChildren(parentNode)) {
134
+ return;
135
+ }
136
+ }
137
+ const hasSourceAttribute = openingPath.node.attributes.some(
138
+ (attribute) => t2.isJSXAttribute(attribute) && t2.isJSXIdentifier(attribute.name, { name: options.attributeName })
139
+ );
140
+ const hasRevisionAttribute = openingPath.node.attributes.some(
141
+ (attribute) => t2.isJSXAttribute(attribute) && t2.isJSXIdentifier(attribute.name, { name: options.revisionAttributeName })
142
+ );
143
+ if (hasSourceAttribute && hasRevisionAttribute) {
144
+ return;
145
+ }
146
+ const { line, column } = openingPath.node.loc.start;
147
+ const locationValue = `${relativePath}:${line}:${column + 1}`;
148
+ if (!hasSourceAttribute) {
149
+ openingPath.node.attributes.push(
150
+ t2.jsxAttribute(t2.jsxIdentifier(options.attributeName), t2.stringLiteral(locationValue))
151
+ );
152
+ changed = true;
153
+ }
154
+ if (!hasRevisionAttribute) {
155
+ openingPath.node.attributes.push(
156
+ t2.jsxAttribute(
157
+ t2.jsxIdentifier(options.revisionAttributeName),
158
+ t2.stringLiteral(options.revisionValue)
159
+ )
160
+ );
161
+ changed = true;
162
+ }
163
+ },
164
+ JSXElement(elementPath) {
165
+ if (!options.wrapLooseTextNodes) {
166
+ return;
167
+ }
168
+ const nameNode = elementPath.node.openingElement.name;
169
+ if (!isHostElement(nameNode)) {
170
+ return;
171
+ }
172
+ const children = elementPath.node.children;
173
+ if (children.length === 0) {
174
+ return;
175
+ }
176
+ const hasNonTextSiblings = children.some(
177
+ (child) => t2.isJSXElement(child) || t2.isJSXFragment(child) || t2.isJSXExpressionContainer(child)
178
+ );
179
+ if (!hasNonTextSiblings) {
180
+ return;
181
+ }
182
+ const nextChildren = [];
183
+ let wrappedAnyTextNode = false;
184
+ for (const child of children) {
185
+ if (!t2.isJSXText(child) || !child.loc) {
186
+ nextChildren.push(child);
187
+ continue;
188
+ }
189
+ const parts = splitMeaningfulText(child.value);
190
+ if (!parts) {
191
+ nextChildren.push(child);
192
+ continue;
193
+ }
194
+ const textStart = advanceLocation(
195
+ { line: child.loc.start.line, column: child.loc.start.column },
196
+ parts.leading
197
+ );
198
+ const locationValue = `${relativePath}:${textStart.line}:${textStart.column + 1}`;
199
+ if (parts.leading.length > 0) {
200
+ nextChildren.push(t2.jsxText(parts.leading));
201
+ }
202
+ nextChildren.push(
203
+ t2.jsxElement(
204
+ t2.jsxOpeningElement(t2.jsxIdentifier("span"), [
205
+ t2.jsxAttribute(t2.jsxIdentifier(options.attributeName), t2.stringLiteral(locationValue)),
206
+ t2.jsxAttribute(
207
+ t2.jsxIdentifier(options.revisionAttributeName),
208
+ t2.stringLiteral(options.revisionValue)
209
+ )
210
+ ]),
211
+ t2.jsxClosingElement(t2.jsxIdentifier("span")),
212
+ [t2.jsxText(parts.text)],
213
+ false
214
+ )
215
+ );
216
+ if (parts.trailing.length > 0) {
217
+ nextChildren.push(t2.jsxText(parts.trailing));
218
+ }
219
+ wrappedAnyTextNode = true;
220
+ changed = true;
221
+ }
222
+ if (wrappedAnyTextNode) {
223
+ elementPath.node.children = nextChildren;
224
+ }
225
+ }
226
+ });
227
+ if (!changed) {
228
+ return { code: source, map: null, changed: false };
229
+ }
230
+ const output = (0, import_generator.default)(
231
+ ast,
232
+ {
233
+ sourceMaps: true,
234
+ sourceFileName: relativePath,
235
+ jsescOption: { minimal: true }
236
+ },
237
+ source
238
+ );
239
+ return {
240
+ code: output.code,
241
+ map: output.map,
242
+ changed: true
243
+ };
244
+ }
245
+
246
+ // src/utils/commit.ts
247
+ var import_node_child_process = require("node:child_process");
248
+ var COMMIT_ENV_KEYS = [
249
+ "COMMIT_SHA",
250
+ "SOURCE_VERSION",
251
+ "GITHUB_SHA",
252
+ "CI_COMMIT_SHA",
253
+ "GIT_COMMIT",
254
+ "VERCEL_GIT_COMMIT_SHA",
255
+ "RENDER_GIT_COMMIT"
256
+ ];
257
+ var cachedCommitSha = null;
258
+ function resolveCommitFromEnv() {
259
+ for (const key of COMMIT_ENV_KEYS) {
260
+ const value = process.env[key]?.trim();
261
+ if (value) {
262
+ return value;
263
+ }
264
+ }
265
+ return null;
266
+ }
267
+ function resolveCommitFromGit() {
268
+ try {
269
+ const value = (0, import_node_child_process.execSync)("git rev-parse HEAD", {
270
+ encoding: "utf8",
271
+ stdio: ["ignore", "pipe", "ignore"]
272
+ }).trim();
273
+ return value.length > 0 ? value : null;
274
+ } catch {
275
+ return null;
276
+ }
277
+ }
278
+ function resolveBuildCommitSha() {
279
+ if (cachedCommitSha) {
280
+ return cachedCommitSha;
281
+ }
282
+ const resolved = resolveCommitFromEnv() ?? resolveCommitFromGit() ?? "unknown";
283
+ cachedCommitSha = resolved;
284
+ return resolved;
285
+ }
286
+
287
+ // src/index.ts
288
+ function normalizeOptions(options) {
289
+ return {
290
+ attributeName: typeof options.attributeName === "string" && options.attributeName.trim().length > 0 ? options.attributeName.trim() : DEFAULT_ATTRIBUTE_NAME,
291
+ revisionAttributeName: DEFAULT_REVISION_ATTRIBUTE_NAME,
292
+ revisionValue: resolveBuildCommitSha(),
293
+ wrapLooseTextNodes: options.wrapLooseTextNodes === true,
294
+ annotateLeafNodesOnly: options.annotateLeafNodesOnly === true
295
+ };
296
+ }
297
+ function jsxSourceLoader(source, inputSourceMap) {
298
+ const callback = this.async();
299
+ const loaderOptions = typeof this.getOptions === "function" ? this.getOptions() ?? {} : {};
300
+ const normalizedOptions = normalizeOptions(loaderOptions);
301
+ const output = transformSource(source, this.resourcePath, normalizedOptions);
302
+ if (!output.changed) {
303
+ callback(null, source, inputSourceMap);
304
+ return;
305
+ }
306
+ callback(null, output.code, output.map ?? inputSourceMap);
307
+ }
308
+ module.exports = jsxSourceLoader;
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@deshlo/loader",
3
+ "version": "0.1.0",
4
+ "description": "Source inspector webpack loader runtime",
5
+ "main": "jsx-source-loader.cjs",
6
+ "types": "index.d.ts",
7
+ "sideEffects": false,
8
+ "exports": {
9
+ ".": {
10
+ "types": "./index.d.ts",
11
+ "require": "./jsx-source-loader.cjs",
12
+ "default": "./jsx-source-loader.cjs"
13
+ }
14
+ },
15
+ "files": [
16
+ "jsx-source-loader.cjs",
17
+ "index.d.ts"
18
+ ],
19
+ "scripts": {
20
+ "clean": "rm -rf .build",
21
+ "build": "node ./scripts/build-loader.mjs",
22
+ "test": "echo \"No tests yet\""
23
+ },
24
+ "keywords": [
25
+ "webpack-loader",
26
+ "source-map",
27
+ "debug",
28
+ "inspector"
29
+ ],
30
+ "author": "Nikos Gerontakis",
31
+ "license": "MIT",
32
+ "dependencies": {
33
+ "@babel/generator": "^7.28.3",
34
+ "@babel/parser": "^7.28.4",
35
+ "@babel/traverse": "^7.29.0",
36
+ "@babel/types": "^7.28.4"
37
+ },
38
+ "devDependencies": {
39
+ "@types/node": "^24.10.1",
40
+ "esbuild": "^0.25.9"
41
+ }
42
+ }