lunet-transpiler 0.0.2 → 0.0.3
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.cjs +181 -1
- package/dist/index.js +137 -1
- package/package.json +1 -2
package/dist/index.cjs
CHANGED
|
@@ -1 +1,181 @@
|
|
|
1
|
-
var
|
|
1
|
+
var import_node_module = require("node:module");
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __toESM = (mod, isNodeMode, target) => {
|
|
9
|
+
target = mod != null ? __create(__getProtoOf(mod)) : {};
|
|
10
|
+
const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
|
|
11
|
+
for (let key of __getOwnPropNames(mod))
|
|
12
|
+
if (!__hasOwnProp.call(to, key))
|
|
13
|
+
__defProp(to, key, {
|
|
14
|
+
get: () => mod[key],
|
|
15
|
+
enumerable: true
|
|
16
|
+
});
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __moduleCache = /* @__PURE__ */ new WeakMap;
|
|
20
|
+
var __toCommonJS = (from) => {
|
|
21
|
+
var entry = __moduleCache.get(from), desc;
|
|
22
|
+
if (entry)
|
|
23
|
+
return entry;
|
|
24
|
+
entry = __defProp({}, "__esModule", { value: true });
|
|
25
|
+
if (from && typeof from === "object" || typeof from === "function")
|
|
26
|
+
__getOwnPropNames(from).map((key) => !__hasOwnProp.call(entry, key) && __defProp(entry, key, {
|
|
27
|
+
get: () => from[key],
|
|
28
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
29
|
+
}));
|
|
30
|
+
__moduleCache.set(from, entry);
|
|
31
|
+
return entry;
|
|
32
|
+
};
|
|
33
|
+
var __export = (target, all) => {
|
|
34
|
+
for (var name in all)
|
|
35
|
+
__defProp(target, name, {
|
|
36
|
+
get: all[name],
|
|
37
|
+
enumerable: true,
|
|
38
|
+
configurable: true,
|
|
39
|
+
set: (newValue) => all[name] = () => newValue
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
// src/index.ts
|
|
44
|
+
var exports_src = {};
|
|
45
|
+
__export(exports_src, {
|
|
46
|
+
transpile: () => transpile,
|
|
47
|
+
rollup_lunet: () => rollup_lunet,
|
|
48
|
+
bun_lunet: () => bun_lunet
|
|
49
|
+
});
|
|
50
|
+
module.exports = __toCommonJS(exports_src);
|
|
51
|
+
var import_promises = require("fs/promises");
|
|
52
|
+
var import_fs = require("fs");
|
|
53
|
+
|
|
54
|
+
// src/transpile.ts
|
|
55
|
+
var import_parser = require("@babel/parser");
|
|
56
|
+
var import_traverse = __toESM(require("@babel/traverse"));
|
|
57
|
+
var import_generator = __toESM(require("@babel/generator"));
|
|
58
|
+
var t = __toESM(require("@babel/types"));
|
|
59
|
+
var jsxAttr2Object = (ast) => t.objectExpression(ast.map((e) => e.type === "JSXAttribute" ? t.objectProperty(t.identifier(e.name.type === "JSXIdentifier" ? e.name.name : e.name.name.name), e.value ? e.value.type === "StringLiteral" ? e.value : jsx2Expression(e.value, true) : t.booleanLiteral(true)) : t.spreadElement(e.argument)));
|
|
60
|
+
var jsxMemberToMemberExpression = (ast) => ast.type === "JSXIdentifier" ? t.identifier(ast.name) : t.memberExpression(jsxMemberToMemberExpression(ast.object), jsxMemberToMemberExpression(ast.property));
|
|
61
|
+
var filterEmptyString = (ast) => !(ast.type === "StringLiteral" && !ast.value);
|
|
62
|
+
var jsx2Elements = (ast, dontTrim) => {
|
|
63
|
+
if (ast.type === "JSXSpreadChild")
|
|
64
|
+
return t.spreadElement(ast.expression);
|
|
65
|
+
else
|
|
66
|
+
return jsx2Expression(ast, dontTrim);
|
|
67
|
+
};
|
|
68
|
+
var jsx2Expression = (ast, dontTrim) => {
|
|
69
|
+
switch (ast.type) {
|
|
70
|
+
case "JSXText":
|
|
71
|
+
return t.stringLiteral(dontTrim ? ast.value : ast.value.trim());
|
|
72
|
+
case "JSXFragment":
|
|
73
|
+
return t.arrayExpression([
|
|
74
|
+
t.nullLiteral(),
|
|
75
|
+
t.objectExpression([]),
|
|
76
|
+
...ast.children.map((child) => jsx2Elements(child)).filter(filterEmptyString)
|
|
77
|
+
]);
|
|
78
|
+
case "JSXElement":
|
|
79
|
+
const tag = ast.openingElement.name;
|
|
80
|
+
if (tag.type === "JSXIdentifier" && /^[a-z]/.test(tag.name)) {
|
|
81
|
+
return t.arrayExpression([
|
|
82
|
+
t.stringLiteral(tag.name),
|
|
83
|
+
t.objectExpression(ast.openingElement.attributes.map((attr) => {
|
|
84
|
+
if (attr.type === "JSXAttribute") {
|
|
85
|
+
if (attr.name.type === "JSXNamespacedName")
|
|
86
|
+
console.warn("Warning: JSXNamespacedName is not supported");
|
|
87
|
+
const { name } = attr.name;
|
|
88
|
+
return t.objectProperty(t.identifier(typeof name === "string" ? name : name.name), attr.value ? attr.value.type === "StringLiteral" ? attr.value : jsx2Expression(attr.value) : t.booleanLiteral(true));
|
|
89
|
+
} else {
|
|
90
|
+
return t.spreadElement(attr.argument);
|
|
91
|
+
}
|
|
92
|
+
})),
|
|
93
|
+
...ast.children.map((child) => jsx2Elements(child)).filter(filterEmptyString)
|
|
94
|
+
]);
|
|
95
|
+
} else
|
|
96
|
+
switch (tag.type) {
|
|
97
|
+
case "JSXIdentifier":
|
|
98
|
+
return t.callExpression(t.identifier(tag.name), [
|
|
99
|
+
jsxAttr2Object(ast.openingElement.attributes)
|
|
100
|
+
]);
|
|
101
|
+
case "JSXMemberExpression":
|
|
102
|
+
return t.callExpression(jsxMemberToMemberExpression(tag), [
|
|
103
|
+
jsxAttr2Object(ast.openingElement.attributes)
|
|
104
|
+
]);
|
|
105
|
+
case "JSXNamespacedName":
|
|
106
|
+
console.warn("Warning: JSXNamespacedName is not supported");
|
|
107
|
+
return t.callExpression(t.identifier(tag.name.name), [
|
|
108
|
+
jsxAttr2Object(ast.openingElement.attributes)
|
|
109
|
+
]);
|
|
110
|
+
}
|
|
111
|
+
case "JSXExpressionContainer":
|
|
112
|
+
switch (ast.expression.type) {
|
|
113
|
+
case "JSXEmptyExpression":
|
|
114
|
+
return t.booleanLiteral(true);
|
|
115
|
+
case "JSXElement":
|
|
116
|
+
case "JSXFragment":
|
|
117
|
+
return jsx2Expression(ast.expression);
|
|
118
|
+
default:
|
|
119
|
+
return ast.expression;
|
|
120
|
+
}
|
|
121
|
+
case "JSXSpreadChild":
|
|
122
|
+
return t.arrayExpression([
|
|
123
|
+
t.nullLiteral(),
|
|
124
|
+
t.objectExpression([]),
|
|
125
|
+
t.spreadElement(ast.expression)
|
|
126
|
+
]);
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
var transpile = (code, isTypeScript) => {
|
|
130
|
+
const ast = import_parser.parse(code, {
|
|
131
|
+
sourceType: "module",
|
|
132
|
+
plugins: isTypeScript ? ["typescript", "jsx"] : ["jsx"]
|
|
133
|
+
});
|
|
134
|
+
import_traverse.default(ast, {
|
|
135
|
+
JSXElement(ast2) {
|
|
136
|
+
ast2.replaceWith(jsx2Expression(ast2.node));
|
|
137
|
+
},
|
|
138
|
+
JSXFragment(ast2) {
|
|
139
|
+
ast2.replaceWith(jsx2Expression(ast2.node));
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
return import_generator.default(ast).code;
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
// src/index.ts
|
|
146
|
+
var readFile = process.isBun ? (path) => Bun.file(path).text() : (path) => import_promises.readFile(path).then((e) => e.toString());
|
|
147
|
+
var bun_lunet = () => ({
|
|
148
|
+
name: "bun-lunet",
|
|
149
|
+
setup(build) {
|
|
150
|
+
build.onLoad({ filter: /\.jsx$/ }, async (args) => {
|
|
151
|
+
const code = await readFile(args.path);
|
|
152
|
+
return {
|
|
153
|
+
contents: transpile(code, false),
|
|
154
|
+
loader: "jsx"
|
|
155
|
+
};
|
|
156
|
+
});
|
|
157
|
+
build.onLoad({ filter: /\.tsx$/ }, async (args) => {
|
|
158
|
+
const code = await readFile(args.path);
|
|
159
|
+
return {
|
|
160
|
+
contents: transpile(code, true),
|
|
161
|
+
loader: "tsx"
|
|
162
|
+
};
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
var rollup_lunet = () => ({
|
|
167
|
+
name: "rollup-lunet",
|
|
168
|
+
load(id) {
|
|
169
|
+
if (id.endsWith(".jsx")) {
|
|
170
|
+
const code = import_fs.readFileSync(id).toString();
|
|
171
|
+
return {
|
|
172
|
+
code: transpile(code, false)
|
|
173
|
+
};
|
|
174
|
+
} else if (id.endsWith(".tsx")) {
|
|
175
|
+
const code = import_fs.readFileSync(id).toString();
|
|
176
|
+
return {
|
|
177
|
+
code: transpile(code, true)
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
});
|
package/dist/index.js
CHANGED
|
@@ -1 +1,137 @@
|
|
|
1
|
-
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { readFile as NodeReadFile } from "fs/promises";
|
|
3
|
+
import { readFileSync } from "fs";
|
|
4
|
+
|
|
5
|
+
// src/transpile.ts
|
|
6
|
+
import { parse } from "@babel/parser";
|
|
7
|
+
import traverse from "@babel/traverse";
|
|
8
|
+
import generate from "@babel/generator";
|
|
9
|
+
import * as t from "@babel/types";
|
|
10
|
+
var jsxAttr2Object = (ast) => t.objectExpression(ast.map((e) => e.type === "JSXAttribute" ? t.objectProperty(t.identifier(e.name.type === "JSXIdentifier" ? e.name.name : e.name.name.name), e.value ? e.value.type === "StringLiteral" ? e.value : jsx2Expression(e.value, true) : t.booleanLiteral(true)) : t.spreadElement(e.argument)));
|
|
11
|
+
var jsxMemberToMemberExpression = (ast) => ast.type === "JSXIdentifier" ? t.identifier(ast.name) : t.memberExpression(jsxMemberToMemberExpression(ast.object), jsxMemberToMemberExpression(ast.property));
|
|
12
|
+
var filterEmptyString = (ast) => !(ast.type === "StringLiteral" && !ast.value);
|
|
13
|
+
var jsx2Elements = (ast, dontTrim) => {
|
|
14
|
+
if (ast.type === "JSXSpreadChild")
|
|
15
|
+
return t.spreadElement(ast.expression);
|
|
16
|
+
else
|
|
17
|
+
return jsx2Expression(ast, dontTrim);
|
|
18
|
+
};
|
|
19
|
+
var jsx2Expression = (ast, dontTrim) => {
|
|
20
|
+
switch (ast.type) {
|
|
21
|
+
case "JSXText":
|
|
22
|
+
return t.stringLiteral(dontTrim ? ast.value : ast.value.trim());
|
|
23
|
+
case "JSXFragment":
|
|
24
|
+
return t.arrayExpression([
|
|
25
|
+
t.nullLiteral(),
|
|
26
|
+
t.objectExpression([]),
|
|
27
|
+
...ast.children.map((child) => jsx2Elements(child)).filter(filterEmptyString)
|
|
28
|
+
]);
|
|
29
|
+
case "JSXElement":
|
|
30
|
+
const tag = ast.openingElement.name;
|
|
31
|
+
if (tag.type === "JSXIdentifier" && /^[a-z]/.test(tag.name)) {
|
|
32
|
+
return t.arrayExpression([
|
|
33
|
+
t.stringLiteral(tag.name),
|
|
34
|
+
t.objectExpression(ast.openingElement.attributes.map((attr) => {
|
|
35
|
+
if (attr.type === "JSXAttribute") {
|
|
36
|
+
if (attr.name.type === "JSXNamespacedName")
|
|
37
|
+
console.warn("Warning: JSXNamespacedName is not supported");
|
|
38
|
+
const { name } = attr.name;
|
|
39
|
+
return t.objectProperty(t.identifier(typeof name === "string" ? name : name.name), attr.value ? attr.value.type === "StringLiteral" ? attr.value : jsx2Expression(attr.value) : t.booleanLiteral(true));
|
|
40
|
+
} else {
|
|
41
|
+
return t.spreadElement(attr.argument);
|
|
42
|
+
}
|
|
43
|
+
})),
|
|
44
|
+
...ast.children.map((child) => jsx2Elements(child)).filter(filterEmptyString)
|
|
45
|
+
]);
|
|
46
|
+
} else
|
|
47
|
+
switch (tag.type) {
|
|
48
|
+
case "JSXIdentifier":
|
|
49
|
+
return t.callExpression(t.identifier(tag.name), [
|
|
50
|
+
jsxAttr2Object(ast.openingElement.attributes)
|
|
51
|
+
]);
|
|
52
|
+
case "JSXMemberExpression":
|
|
53
|
+
return t.callExpression(jsxMemberToMemberExpression(tag), [
|
|
54
|
+
jsxAttr2Object(ast.openingElement.attributes)
|
|
55
|
+
]);
|
|
56
|
+
case "JSXNamespacedName":
|
|
57
|
+
console.warn("Warning: JSXNamespacedName is not supported");
|
|
58
|
+
return t.callExpression(t.identifier(tag.name.name), [
|
|
59
|
+
jsxAttr2Object(ast.openingElement.attributes)
|
|
60
|
+
]);
|
|
61
|
+
}
|
|
62
|
+
case "JSXExpressionContainer":
|
|
63
|
+
switch (ast.expression.type) {
|
|
64
|
+
case "JSXEmptyExpression":
|
|
65
|
+
return t.booleanLiteral(true);
|
|
66
|
+
case "JSXElement":
|
|
67
|
+
case "JSXFragment":
|
|
68
|
+
return jsx2Expression(ast.expression);
|
|
69
|
+
default:
|
|
70
|
+
return ast.expression;
|
|
71
|
+
}
|
|
72
|
+
case "JSXSpreadChild":
|
|
73
|
+
return t.arrayExpression([
|
|
74
|
+
t.nullLiteral(),
|
|
75
|
+
t.objectExpression([]),
|
|
76
|
+
t.spreadElement(ast.expression)
|
|
77
|
+
]);
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
var transpile = (code, isTypeScript) => {
|
|
81
|
+
const ast = parse(code, {
|
|
82
|
+
sourceType: "module",
|
|
83
|
+
plugins: isTypeScript ? ["typescript", "jsx"] : ["jsx"]
|
|
84
|
+
});
|
|
85
|
+
traverse(ast, {
|
|
86
|
+
JSXElement(ast2) {
|
|
87
|
+
ast2.replaceWith(jsx2Expression(ast2.node));
|
|
88
|
+
},
|
|
89
|
+
JSXFragment(ast2) {
|
|
90
|
+
ast2.replaceWith(jsx2Expression(ast2.node));
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
return generate(ast).code;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
// src/index.ts
|
|
97
|
+
var readFile = process.isBun ? (path) => Bun.file(path).text() : (path) => NodeReadFile(path).then((e) => e.toString());
|
|
98
|
+
var bun_lunet = () => ({
|
|
99
|
+
name: "bun-lunet",
|
|
100
|
+
setup(build) {
|
|
101
|
+
build.onLoad({ filter: /\.jsx$/ }, async (args) => {
|
|
102
|
+
const code = await readFile(args.path);
|
|
103
|
+
return {
|
|
104
|
+
contents: transpile(code, false),
|
|
105
|
+
loader: "jsx"
|
|
106
|
+
};
|
|
107
|
+
});
|
|
108
|
+
build.onLoad({ filter: /\.tsx$/ }, async (args) => {
|
|
109
|
+
const code = await readFile(args.path);
|
|
110
|
+
return {
|
|
111
|
+
contents: transpile(code, true),
|
|
112
|
+
loader: "tsx"
|
|
113
|
+
};
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
var rollup_lunet = () => ({
|
|
118
|
+
name: "rollup-lunet",
|
|
119
|
+
load(id) {
|
|
120
|
+
if (id.endsWith(".jsx")) {
|
|
121
|
+
const code = readFileSync(id).toString();
|
|
122
|
+
return {
|
|
123
|
+
code: transpile(code, false)
|
|
124
|
+
};
|
|
125
|
+
} else if (id.endsWith(".tsx")) {
|
|
126
|
+
const code = readFileSync(id).toString();
|
|
127
|
+
return {
|
|
128
|
+
code: transpile(code, true)
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
export {
|
|
134
|
+
transpile,
|
|
135
|
+
rollup_lunet,
|
|
136
|
+
bun_lunet
|
|
137
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lunet-transpiler",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"author": "TNTSuperMan",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"description": "より柔軟なWebフロントエンドライブラリ lunetのトランスパイラー。",
|
|
@@ -31,7 +31,6 @@
|
|
|
31
31
|
"external": ["@babel/generator", "@babel/parser", "@babel/traverse", "@babel/types"],
|
|
32
32
|
"format": ["esm", "cjs"],
|
|
33
33
|
"target": "node",
|
|
34
|
-
"minify": true,
|
|
35
34
|
"outDir": "dist"
|
|
36
35
|
},
|
|
37
36
|
"scripts": {
|