-
Notifications
You must be signed in to change notification settings - Fork 0
/
transform-vite-meta.js
112 lines (100 loc) · 3.04 KB
/
transform-vite-meta.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
const IMPORT_META_ENV_NAME = "__IMPORT_META_ENV";
const IMPORT_META_HOT_NAME = "__IMPORT_META_HOT";
function getImportMetaPropPath({ path, property, t }) {
const isImportMeta =
t.isIdentifier(path.node.meta, { name: "import" }) &&
t.isIdentifier(path.node.property, { name: "meta" });
const isImportMetaProp =
isImportMeta &&
t.isMemberExpression(path.parent, { object: path.node }) &&
t.isIdentifier(path.parent.property, { name: property });
return isImportMetaProp ? path.parentPath : undefined;
}
function getImportMetaPropAssignmentPath({ path, property, t }) {
const importMetaEnvPath = getImportMetaPropPath({ path, property, t });
if (importMetaEnvPath === undefined) {
return undefined;
}
const isImportMetaEnvAssignment = t.isAssignmentExpression(
importMetaEnvPath.parent,
{
operator: "=",
left: importMetaEnvPath.node,
}
);
return isImportMetaEnvAssignment ? path.parentPath.parentPath : undefined;
}
function replaceImportMetaUsageWithVariable({ property, identifier, path, t }) {
const importMetaPropPath = getImportMetaPropPath({ path, property, t });
if (importMetaPropPath !== undefined) {
importMetaPropPath.replaceWith(identifier);
}
}
function replaceImportMetaAssignmentWithVariable({
property,
identifier,
path,
t,
}) {
const importMetaEnvAssignmentPath = getImportMetaPropAssignmentPath({
property,
path,
t,
});
if (importMetaEnvAssignmentPath !== undefined) {
const { right } = importMetaEnvAssignmentPath.node;
const importMetaEnvDeclaration = t.variableDeclaration("const", [
t.variableDeclarator(identifier, right),
]);
importMetaEnvAssignmentPath.parentPath.replaceWith(
importMetaEnvDeclaration
);
}
}
/** @param {babelCore} options */
function viteMetaTransformPlugin({ types: t }) {
const IMPORT_META_ENV_IDENTIFIER = t.identifier(IMPORT_META_ENV_NAME);
const IMPORT_META_HOT_IDENTIFIER = t.identifier(IMPORT_META_HOT_NAME);
/** @type {PluginObj} */
const plugin = {
name: "vite-meta",
visitor: {
MetaProperty(path) {
replaceImportMetaAssignmentWithVariable({
property: "env",
identifier: IMPORT_META_ENV_IDENTIFIER,
path,
t,
});
replaceImportMetaUsageWithVariable({
property: "env",
identifier: IMPORT_META_ENV_IDENTIFIER,
path,
t,
});
replaceImportMetaAssignmentWithVariable({
property: "hot",
identifier: IMPORT_META_HOT_IDENTIFIER,
path,
t,
});
replaceImportMetaUsageWithVariable({
property: "hot",
identifier: IMPORT_META_HOT_IDENTIFIER,
path,
t,
});
},
},
};
return plugin;
}
module.exports = {
viteMetaTransformPlugin,
};
/**
* @typedef {import('@babel/core')} babelCore
* @typedef {import('@babel/core').PluginObj} PluginObj
* @typedef {import('@babel/core').NodePath} NodePath
* @typedef {import('@babel/types').AssignmentExpression} AssignmentExpression
*/