vite-plugin-simple-macro 0.9.1 → 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.
- package/dist/index.d.ts +5 -0
- package/dist/index.mjs +46 -8
- package/package.json +6 -8
- package/dist/index.js +0 -56
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { FilterExpression } from '@rolldown/pluginutils';
|
|
2
2
|
import { ModuleType } from 'rolldown';
|
|
3
3
|
import { NodePath } from '@babel/traverse';
|
|
4
|
+
import { NodePath as NodePath_2 } from '@babel/core';
|
|
4
5
|
import { Plugin } from 'rolldown';
|
|
5
6
|
import { types } from '@babel/core';
|
|
6
7
|
|
|
@@ -12,6 +13,10 @@ export declare interface Context {
|
|
|
12
13
|
ssr?: boolean;
|
|
13
14
|
}
|
|
14
15
|
|
|
16
|
+
export declare function ensureGlobal(path: NodePath_2<types.CallExpression>, name: string): boolean;
|
|
17
|
+
|
|
18
|
+
export declare function ensureImport(path: NodePath_2<types.CallExpression>, module: string, name?: string): boolean;
|
|
19
|
+
|
|
15
20
|
export declare interface Macro {
|
|
16
21
|
filter?: FilterExpression;
|
|
17
22
|
transform(this: Context, path: NodePath<types.CallExpression>, ctx: Context, t: typeof types): boolean;
|
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { and, exprInterpreter, id, include, makeIdFiltersToMatchWithQuery, or } from "@rolldown/pluginutils";
|
|
2
2
|
import { parseAsync, traverse, types } from "@babel/core";
|
|
3
3
|
import { generate } from "@babel/generator";
|
|
4
|
-
//#region src/
|
|
4
|
+
//#region src/util.ts
|
|
5
5
|
var PREREQUISITE = id(makeIdFiltersToMatchWithQuery(/\.[mc]?[tj]sx?$/));
|
|
6
6
|
var VISITOR = { CallExpression(path, ctx) {
|
|
7
7
|
for (const macro of ctx.macro) {
|
|
@@ -10,6 +10,50 @@ var VISITOR = { CallExpression(path, ctx) {
|
|
|
10
10
|
break;
|
|
11
11
|
}
|
|
12
12
|
} };
|
|
13
|
+
function addSpecificFilter(macro, inner) {
|
|
14
|
+
const out = [];
|
|
15
|
+
for (const { filter } of macro) if (filter) out.push(filter);
|
|
16
|
+
else return inner;
|
|
17
|
+
return and(inner, or(...out));
|
|
18
|
+
}
|
|
19
|
+
//#endregion
|
|
20
|
+
//#region src/filter.ts
|
|
21
|
+
var DEFAULT_IMPORT_NAME = "default";
|
|
22
|
+
function ensureGlobal(path, name) {
|
|
23
|
+
const callee = path.get("callee");
|
|
24
|
+
if (!callee.isIdentifier()) return false;
|
|
25
|
+
if (callee.node.name !== name) return false;
|
|
26
|
+
return !path.scope.getBinding(name);
|
|
27
|
+
}
|
|
28
|
+
function ensureImport(path, module, name = DEFAULT_IMPORT_NAME) {
|
|
29
|
+
const callee = path.get("callee");
|
|
30
|
+
if (callee.isMemberExpression()) return ensureImportNamespace(callee, module, name);
|
|
31
|
+
if (!callee.isIdentifier()) return false;
|
|
32
|
+
const specifier = callee.scope.getBinding(callee.node.name)?.path;
|
|
33
|
+
if (!specifier) return false;
|
|
34
|
+
if (specifier.isImportDefaultSpecifier()) return name === DEFAULT_IMPORT_NAME && ensureModule(specifier, module);
|
|
35
|
+
if (!specifier.isImportSpecifier()) return false;
|
|
36
|
+
if (!ensureContent(specifier.get("imported"), name)) return false;
|
|
37
|
+
return ensureModule(specifier, module);
|
|
38
|
+
}
|
|
39
|
+
function ensureImportNamespace(path, module, name) {
|
|
40
|
+
if (!ensureContent(path.get("property"), name)) return false;
|
|
41
|
+
const obj = path.get("object");
|
|
42
|
+
if (!obj.isIdentifier()) return false;
|
|
43
|
+
const specifier = path.scope.getBinding(obj.node.name)?.path;
|
|
44
|
+
if (!specifier?.isImportNamespaceSpecifier()) return false;
|
|
45
|
+
return ensureModule(specifier, module);
|
|
46
|
+
}
|
|
47
|
+
function ensureModule(path, module) {
|
|
48
|
+
const declaration = path.parentPath;
|
|
49
|
+
if (!declaration.isImportDeclaration()) throw declaration.buildCodeFrameError("The parent of an import specifier should always be an import declaration");
|
|
50
|
+
return declaration.get("source").node.value === module;
|
|
51
|
+
}
|
|
52
|
+
function ensureContent(path, content) {
|
|
53
|
+
return path.isIdentifier() && path.node.name === content || path.isStringLiteral() && path.node.value === content;
|
|
54
|
+
}
|
|
55
|
+
//#endregion
|
|
56
|
+
//#region src/index.ts
|
|
13
57
|
function macroPlugin(opts) {
|
|
14
58
|
const { filter, macro } = opts;
|
|
15
59
|
const inner = filter ? and(PREREQUISITE, filter) : PREREQUISITE;
|
|
@@ -46,11 +90,5 @@ function macroPlugin(opts) {
|
|
|
46
90
|
}
|
|
47
91
|
};
|
|
48
92
|
}
|
|
49
|
-
function addSpecificFilter(macro, inner) {
|
|
50
|
-
const out = [];
|
|
51
|
-
for (const { filter } of macro) if (filter) out.push(filter);
|
|
52
|
-
else return inner;
|
|
53
|
-
return and(inner, or(...out));
|
|
54
|
-
}
|
|
55
93
|
//#endregion
|
|
56
|
-
export { macroPlugin as default };
|
|
94
|
+
export { macroPlugin as default, ensureGlobal, ensureImport };
|
package/package.json
CHANGED
|
@@ -1,23 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-simple-macro",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "A simple macro plugin for Vite",
|
|
5
5
|
"author": "AFatNiBBa",
|
|
6
6
|
"license": "ISC",
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
8
|
-
"
|
|
9
|
-
"require": "./dist/index.js",
|
|
10
|
-
"import": "./dist/index.mjs",
|
|
11
|
-
"types": "./dist/index.d.ts"
|
|
12
|
-
},
|
|
8
|
+
"main": "./dist/index.mjs",
|
|
13
9
|
"scripts": {
|
|
14
10
|
"build": "vite build",
|
|
11
|
+
"clean": "git clean -Xdf",
|
|
15
12
|
"save": "npm run build && npm publish"
|
|
16
13
|
},
|
|
17
14
|
"devDependencies": {
|
|
18
15
|
"@microsoft/api-extractor": "^7.58.9",
|
|
19
16
|
"@types/babel__core": "^7.20.5",
|
|
20
|
-
"@types/node": "^20.6.0",
|
|
21
17
|
"@typescript/typescript6": "^6.0.2",
|
|
22
18
|
"unplugin-dts": "^1.0.3",
|
|
23
19
|
"vite": "^8.1.3"
|
|
@@ -25,7 +21,9 @@
|
|
|
25
21
|
"dependencies": {
|
|
26
22
|
"@babel/core": "^7.0.0",
|
|
27
23
|
"@babel/plugin-syntax-typescript": "^7.0.0",
|
|
28
|
-
"@rolldown/pluginutils": "^1.0.1"
|
|
24
|
+
"@rolldown/pluginutils": "^1.0.1"
|
|
25
|
+
},
|
|
26
|
+
"peerDependencies": {
|
|
29
27
|
"rolldown": "^1.1.4"
|
|
30
28
|
},
|
|
31
29
|
"repository": {
|
package/dist/index.js
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
let _rolldown_pluginutils = require("@rolldown/pluginutils");
|
|
2
|
-
let _babel_core = require("@babel/core");
|
|
3
|
-
let _babel_generator = require("@babel/generator");
|
|
4
|
-
//#region src/index.ts
|
|
5
|
-
var PREREQUISITE = (0, _rolldown_pluginutils.id)((0, _rolldown_pluginutils.makeIdFiltersToMatchWithQuery)(/\.[mc]?[tj]sx?$/));
|
|
6
|
-
var VISITOR = { CallExpression(path, ctx) {
|
|
7
|
-
for (const macro of ctx.macro) {
|
|
8
|
-
if (!macro.transform.call(ctx, path, ctx, _babel_core.types)) continue;
|
|
9
|
-
ctx.changed++;
|
|
10
|
-
break;
|
|
11
|
-
}
|
|
12
|
-
} };
|
|
13
|
-
function macroPlugin(opts) {
|
|
14
|
-
const { filter, macro } = opts;
|
|
15
|
-
const inner = filter ? (0, _rolldown_pluginutils.and)(PREREQUISITE, filter) : PREREQUISITE;
|
|
16
|
-
return {
|
|
17
|
-
name: "vite-plugin-simple-macro",
|
|
18
|
-
transform: {
|
|
19
|
-
order: "pre",
|
|
20
|
-
filter: [(0, _rolldown_pluginutils.include)(addSpecificFilter(macro, inner))],
|
|
21
|
-
async handler(code, id, info) {
|
|
22
|
-
if (!(0, _rolldown_pluginutils.exprInterpreter)(inner, code, id, info.moduleType)) return;
|
|
23
|
-
const filtered = macro.filter(({ filter }) => !filter || (0, _rolldown_pluginutils.exprInterpreter)(filter, code, id, info.moduleType));
|
|
24
|
-
if (!filtered.length) return;
|
|
25
|
-
const ast = await (0, _babel_core.parseAsync)(code, {
|
|
26
|
-
filename: id,
|
|
27
|
-
plugins: [["@babel/plugin-syntax-typescript", { isTSX: true }]]
|
|
28
|
-
});
|
|
29
|
-
const ctx = {
|
|
30
|
-
id,
|
|
31
|
-
macro: filtered,
|
|
32
|
-
changed: 0,
|
|
33
|
-
...info
|
|
34
|
-
};
|
|
35
|
-
(0, _babel_core.traverse)(ast, VISITOR, void 0, ctx);
|
|
36
|
-
if (!ctx.changed) return;
|
|
37
|
-
const out = (0, _babel_generator.generate)(ast, {
|
|
38
|
-
sourceMaps: true,
|
|
39
|
-
sourceFileName: id
|
|
40
|
-
});
|
|
41
|
-
return {
|
|
42
|
-
code: out.code,
|
|
43
|
-
map: out.map
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
function addSpecificFilter(macro, inner) {
|
|
50
|
-
const out = [];
|
|
51
|
-
for (const { filter } of macro) if (filter) out.push(filter);
|
|
52
|
-
else return inner;
|
|
53
|
-
return (0, _rolldown_pluginutils.and)(inner, (0, _rolldown_pluginutils.or)(...out));
|
|
54
|
-
}
|
|
55
|
-
//#endregion
|
|
56
|
-
module.exports = macroPlugin;
|