one
Version:
One is a new React Framework that makes Vite serve both native and web.
44 lines (43 loc) • 1.06 kB
JavaScript
import { extname } from "node:path";
import * as swc from "@swc/core";
function useDOMPlugin() {
return {
name: "one-vite-dom-plugin",
async transform(code, id, options) {
if (!code.includes("use dom")) {
return;
}
const ext = extname(id);
const mod = swc.parseSync(code, parseOpts(ext));
let hasUseDom = false;
for (let i = 0; i < mod.body.length; ++i) {
const item = mod.body[i];
if (item.type === "ExpressionStatement") {
if (item.expression.type === "StringLiteral") {
if (item.expression.value === "use dom") {
hasUseDom = true;
break;
}
}
} else {}
}
if (!hasUseDom) {
return;
}
}
};
}
const parseOpts = ext => {
if (ext === ".ts" || ext === ".tsx") {
return {
syntax: "typescript",
tsx: ext.endsWith("x")
};
}
return {
syntax: "ecmascript",
jsx: ext.endsWith("x")
};
};
export { useDOMPlugin };
//# sourceMappingURL=useDomPlugin.mjs.map