vite-plugin-enter-dev 0.0.12 → 0.0.13
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/configure.js +44 -5
- package/package.json +1 -1
package/dist/configure.js
CHANGED
|
@@ -31,12 +31,15 @@ function configureVite(source) {
|
|
|
31
31
|
if (!types.isIdentifier(config.callee) || !defineNames.includes(config.callee.name) || config.arguments.length !== 1) throw new Error("Unsupported Vite config wrapper");
|
|
32
32
|
config = config.arguments[0];
|
|
33
33
|
}
|
|
34
|
+
let setup = [];
|
|
34
35
|
if (types.isArrowFunctionExpression(config) || types.isFunctionExpression(config)) {
|
|
35
36
|
if (types.isBlockStatement(config.body)) {
|
|
36
|
-
|
|
37
|
+
const last = config.body.body.at(-1);
|
|
38
|
+
if (!types.isReturnStatement(last)) {
|
|
37
39
|
throw new Error("Unsupported Vite config: expected a single unconditional return");
|
|
38
40
|
}
|
|
39
|
-
|
|
41
|
+
setup = config.body.body.slice(0, -1);
|
|
42
|
+
config = last.argument ?? void 0;
|
|
40
43
|
} else {
|
|
41
44
|
config = config.body;
|
|
42
45
|
}
|
|
@@ -54,10 +57,12 @@ function configureVite(source) {
|
|
|
54
57
|
config.properties.push(plugins);
|
|
55
58
|
changed = true;
|
|
56
59
|
}
|
|
57
|
-
if (!types.isObjectProperty(plugins)
|
|
60
|
+
if (!types.isObjectProperty(plugins)) throw new Error("Unsupported plugins property");
|
|
61
|
+
const pluginArrays = resolvePluginArrays(plugins.value, setup);
|
|
62
|
+
if (!pluginArrays.length) {
|
|
58
63
|
throw new Error("Unsupported Vite config: expected a literal plugins array");
|
|
59
64
|
}
|
|
60
|
-
const calls =
|
|
65
|
+
const calls = pluginArrays.flatMap((array) => array.elements).map((node) => types.isSpreadElement(node) ? node.argument : node);
|
|
61
66
|
if (calls.some((node) => !types.isCallExpression(node) && node !== null && !types.isBooleanLiteral(node))) {
|
|
62
67
|
throw new Error("Unsupported Vite config: dynamic plugin list");
|
|
63
68
|
}
|
|
@@ -77,7 +82,7 @@ function configureVite(source) {
|
|
|
77
82
|
let call = matching[0];
|
|
78
83
|
if (!call) {
|
|
79
84
|
call = types.callExpression(types.identifier(local), []);
|
|
80
|
-
|
|
85
|
+
pluginArrays[0].elements.push(types.spreadElement(call));
|
|
81
86
|
changed = true;
|
|
82
87
|
}
|
|
83
88
|
if (name !== "enterDevPlugin" || !hostReact || !types.isCallExpression(call)) continue;
|
|
@@ -119,5 +124,39 @@ function configureViteFile(path, check = false) {
|
|
|
119
124
|
}
|
|
120
125
|
return true;
|
|
121
126
|
}
|
|
127
|
+
function resolvePluginArrays(value, setup) {
|
|
128
|
+
if (setup.length === 0 && types.isArrayExpression(value)) return [value];
|
|
129
|
+
if (types.isTSAsExpression(value) || types.isTSSatisfiesExpression(value)) value = value.expression;
|
|
130
|
+
if (types.isCallExpression(value) && types.isMemberExpression(value.callee) && !value.callee.computed && types.isIdentifier(value.callee.property, { name: "filter" }) && value.arguments.length === 1 && types.isIdentifier(value.arguments[0], { name: "Boolean" })) value = value.callee.object;
|
|
131
|
+
if (!types.isIdentifier(value)) throw new Error("Unsupported dynamic plugins reference");
|
|
132
|
+
const name = value.name;
|
|
133
|
+
const first = setup[0];
|
|
134
|
+
if (!types.isVariableDeclaration(first, { kind: "const" }) || first.declarations.length !== 1) {
|
|
135
|
+
throw new Error("Unsupported plugins initializer");
|
|
136
|
+
}
|
|
137
|
+
const declaration = first.declarations[0];
|
|
138
|
+
if (!types.isIdentifier(declaration.id, { name }) || !types.isArrayExpression(declaration.init)) {
|
|
139
|
+
throw new Error("Unsupported plugins initializer");
|
|
140
|
+
}
|
|
141
|
+
const arrays = [declaration.init];
|
|
142
|
+
const visit = (statement) => {
|
|
143
|
+
if (types.isIfStatement(statement)) {
|
|
144
|
+
visit(statement.consequent);
|
|
145
|
+
if (statement.alternate) visit(statement.alternate);
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
if (types.isBlockStatement(statement)) {
|
|
149
|
+
statement.body.forEach(visit);
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
const call = types.isExpressionStatement(statement) ? statement.expression : void 0;
|
|
153
|
+
if (!types.isCallExpression(call) || !types.isMemberExpression(call.callee) || call.callee.computed || !types.isIdentifier(call.callee.object, { name }) || !types.isIdentifier(call.callee.property, { name: "push" }) || call.arguments.some((argument) => types.isJSXNamespacedName(argument) || types.isArgumentPlaceholder(argument))) {
|
|
154
|
+
throw new Error("Unsupported plugins mutation");
|
|
155
|
+
}
|
|
156
|
+
arrays.push(types.arrayExpression(call.arguments));
|
|
157
|
+
};
|
|
158
|
+
setup.slice(1).forEach(visit);
|
|
159
|
+
return arrays;
|
|
160
|
+
}
|
|
122
161
|
|
|
123
162
|
export { configureVite, configureViteFile };
|