import.meta.location 0.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.
- package/README.md +93 -0
- package/dist/bun/index.d.mts +8 -0
- package/dist/bun/index.mjs +2 -0
- package/dist/bun/preload.d.mts +1 -0
- package/dist/bun/preload.mjs +5 -0
- package/dist/bun-Bn5rpU4O.mjs +216 -0
- package/dist/plugin/index.cjs +54 -0
- package/dist/plugin/index.d.cts +2571 -0
- package/dist/plugin/index.d.mts +2571 -0
- package/dist/plugin/index.mjs +54 -0
- package/package.json +124 -0
- package/src/types/index.d.ts +17 -0
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { basename } from "node:path";
|
|
2
|
+
//#region src/plugin/index.ts
|
|
3
|
+
/**
|
|
4
|
+
* Babel plugin: rewrites `import.meta.location` into an object literal with
|
|
5
|
+
* the source location baked in at build time.
|
|
6
|
+
*
|
|
7
|
+
* import.meta.location
|
|
8
|
+
*
|
|
9
|
+
* becomes:
|
|
10
|
+
*
|
|
11
|
+
* ({ line: 12, col: 3, file: "entry.ts" })
|
|
12
|
+
*/
|
|
13
|
+
const NEEDLE = "import.meta.location";
|
|
14
|
+
const resolveFilename = (state, override) => {
|
|
15
|
+
if (override !== void 0 && override !== "") return override;
|
|
16
|
+
const { filename } = state;
|
|
17
|
+
if (filename === void 0 || filename === "") return;
|
|
18
|
+
return basename(filename);
|
|
19
|
+
};
|
|
20
|
+
const isImportMetaLocation = (node) => {
|
|
21
|
+
if (node.type !== "MemberExpression" && node.type !== "OptionalMemberExpression") return false;
|
|
22
|
+
if (node.computed || node.property.type !== "Identifier" || node.property.name !== "location") return false;
|
|
23
|
+
const obj = node.object;
|
|
24
|
+
return obj.type === "MetaProperty" && obj.meta.name === "import" && obj.property.name === "meta";
|
|
25
|
+
};
|
|
26
|
+
const createLocationObject = (t, filename, line, column) => t.objectExpression([
|
|
27
|
+
t.objectProperty(t.identifier("line"), t.numericLiteral(line)),
|
|
28
|
+
t.objectProperty(t.identifier("col"), t.numericLiteral(column)),
|
|
29
|
+
t.objectProperty(t.identifier("file"), t.stringLiteral(filename))
|
|
30
|
+
]);
|
|
31
|
+
const plugin = ({ types: t }) => ({
|
|
32
|
+
name: "transform-import-meta-location",
|
|
33
|
+
visitor: { Program(programPath, state) {
|
|
34
|
+
const source = state.file.code;
|
|
35
|
+
if (typeof source !== "string" || !source.includes(NEEDLE)) return;
|
|
36
|
+
const filename = resolveFilename(state, (state.opts ?? {}).filename);
|
|
37
|
+
if (filename === void 0) return;
|
|
38
|
+
const rewrite = (locationPath) => {
|
|
39
|
+
const { node } = locationPath;
|
|
40
|
+
if (!isImportMetaLocation(node) || !node.loc) return;
|
|
41
|
+
locationPath.replaceWith(createLocationObject(t, filename, node.loc.start.line, node.loc.start.column + 1));
|
|
42
|
+
};
|
|
43
|
+
programPath.traverse({
|
|
44
|
+
MemberExpression(path) {
|
|
45
|
+
rewrite(path);
|
|
46
|
+
},
|
|
47
|
+
OptionalMemberExpression(path) {
|
|
48
|
+
rewrite(path);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
} }
|
|
52
|
+
});
|
|
53
|
+
//#endregion
|
|
54
|
+
export { plugin as default };
|
package/package.json
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "import.meta.location",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Build-time transform for import.meta.location source objects.",
|
|
5
|
+
"homepage": "https://github.com/leonsilicon/import.meta.location#readme",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/leonsilicon/import.meta.location/issues"
|
|
8
|
+
},
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"author": {
|
|
11
|
+
"name": "Leon Si",
|
|
12
|
+
"email": "leon@leonsilicon.com"
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/leonsilicon/import.meta.location.git"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist",
|
|
20
|
+
"src/types"
|
|
21
|
+
],
|
|
22
|
+
"type": "module",
|
|
23
|
+
"typesVersions": {
|
|
24
|
+
"*": {
|
|
25
|
+
"types": [
|
|
26
|
+
"./src/types/index.d.ts"
|
|
27
|
+
],
|
|
28
|
+
"plugin": [
|
|
29
|
+
"./dist/plugin/index.d.mts"
|
|
30
|
+
],
|
|
31
|
+
"bun": [
|
|
32
|
+
"./dist/bun/index.d.mts"
|
|
33
|
+
],
|
|
34
|
+
"bun/preload": [
|
|
35
|
+
"./dist/bun/preload.d.mts"
|
|
36
|
+
]
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"exports": {
|
|
40
|
+
".": {
|
|
41
|
+
"import": {
|
|
42
|
+
"types": "./dist/plugin/index.d.mts",
|
|
43
|
+
"default": "./dist/plugin/index.mjs"
|
|
44
|
+
},
|
|
45
|
+
"require": {
|
|
46
|
+
"types": "./dist/plugin/index.d.cts",
|
|
47
|
+
"default": "./dist/plugin/index.cjs"
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
"./plugin": {
|
|
51
|
+
"import": {
|
|
52
|
+
"types": "./dist/plugin/index.d.mts",
|
|
53
|
+
"default": "./dist/plugin/index.mjs"
|
|
54
|
+
},
|
|
55
|
+
"require": {
|
|
56
|
+
"types": "./dist/plugin/index.d.cts",
|
|
57
|
+
"default": "./dist/plugin/index.cjs"
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
"./types": {
|
|
61
|
+
"types": "./src/types/index.d.ts",
|
|
62
|
+
"default": "./src/types/index.d.ts"
|
|
63
|
+
},
|
|
64
|
+
"./bun": {
|
|
65
|
+
"import": {
|
|
66
|
+
"types": "./dist/bun/index.d.mts",
|
|
67
|
+
"default": "./dist/bun/index.mjs"
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
"./bun/preload": {
|
|
71
|
+
"import": {
|
|
72
|
+
"types": "./dist/bun/preload.d.mts",
|
|
73
|
+
"default": "./dist/bun/preload.mjs"
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
"./package.json": "./package.json"
|
|
77
|
+
},
|
|
78
|
+
"publishConfig": {
|
|
79
|
+
"access": "public"
|
|
80
|
+
},
|
|
81
|
+
"scripts": {
|
|
82
|
+
"build": "vp pack",
|
|
83
|
+
"dev": "vp pack --watch",
|
|
84
|
+
"test": "vp test run",
|
|
85
|
+
"check": "vp check",
|
|
86
|
+
"prepublishOnly": "vp run build",
|
|
87
|
+
"prepare": "vp config"
|
|
88
|
+
},
|
|
89
|
+
"devDependencies": {
|
|
90
|
+
"@babel/core": "^7.29.0",
|
|
91
|
+
"@babel/types": "^7.29.7",
|
|
92
|
+
"@types/babel__core": "^7.20.5",
|
|
93
|
+
"@types/bun": "^1.3.14",
|
|
94
|
+
"@types/node": "^25.6.2",
|
|
95
|
+
"@typescript/native-preview": "7.0.0-dev.20260509.2",
|
|
96
|
+
"bumpp": "^11.1.0",
|
|
97
|
+
"typescript": "^6.0.3",
|
|
98
|
+
"vite": "npm:@voidzero-dev/vite-plus-core@latest",
|
|
99
|
+
"vite-plus": "latest"
|
|
100
|
+
},
|
|
101
|
+
"peerDependencies": {
|
|
102
|
+
"@babel/core": "^7.10.0",
|
|
103
|
+
"@types/babel__core": "^7.20.5"
|
|
104
|
+
},
|
|
105
|
+
"peerDependenciesMeta": {
|
|
106
|
+
"@babel/core": {
|
|
107
|
+
"optional": true
|
|
108
|
+
},
|
|
109
|
+
"@types/babel__core": {
|
|
110
|
+
"optional": true
|
|
111
|
+
}
|
|
112
|
+
},
|
|
113
|
+
"overrides": {
|
|
114
|
+
"vite": "npm:@voidzero-dev/vite-plus-core@latest",
|
|
115
|
+
"vitest": "4.1.9"
|
|
116
|
+
},
|
|
117
|
+
"devEngines": {
|
|
118
|
+
"packageManager": {
|
|
119
|
+
"name": "bun",
|
|
120
|
+
"version": "1.3.14",
|
|
121
|
+
"onFail": "download"
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/consistent-type-definitions */
|
|
2
|
+
|
|
3
|
+
declare global {
|
|
4
|
+
interface ImportMetaLocation {
|
|
5
|
+
readonly line: number;
|
|
6
|
+
readonly col: number;
|
|
7
|
+
readonly file: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
interface ImportMeta {
|
|
11
|
+
readonly location?: ImportMetaLocation;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export {};
|
|
16
|
+
|
|
17
|
+
/* eslint-enable @typescript-eslint/consistent-type-definitions */
|