@yr-lang/yr 0.4.0 → 0.4.1
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/lib/@react.yr +6 -3
- package/lib/Yrci/A1.yr +1 -1
- package/lib/Yrci/React.yr +2 -9
- package/lib/cdn.json +1 -0
- package/lib/index.js +7 -0
- package/package.json +1 -1
- package/yr.js +62 -5
package/lib/@react.yr
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
%%
|
|
2
2
|
|
|
3
|
-
_@renderReact(#query) {
|
|
4
|
-
|
|
3
|
+
_@renderReact(#query, #app=App) {
|
|
4
|
+
import { useState, useEffect } from "react";
|
|
5
|
+
function #app() {
|
|
5
6
|
___
|
|
6
|
-
|
|
7
|
+
}
|
|
8
|
+
ReactDOM.createRoot(document.querySelector(#query))
|
|
9
|
+
.render(<#app />);
|
|
7
10
|
@}
|
package/lib/Yrci/React.yr
CHANGED
|
@@ -6,13 +6,10 @@ _ #root
|
|
|
6
6
|
|
|
7
7
|
@@r
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
function App() {
|
|
9
|
+
_@renderReact('#root') {
|
|
11
10
|
const [count, setCount] = useState(0);
|
|
12
11
|
const [message, setMessage] = useState('Loading...');
|
|
13
|
-
useEffect(() => {
|
|
14
|
-
setMessage(`Count is ${count}`);
|
|
15
|
-
}, [count]);
|
|
12
|
+
useEffect(() => setMessage(`Count is ${count}`), [count]);
|
|
16
13
|
return (
|
|
17
14
|
<div>
|
|
18
15
|
<h1>Simple React App</h1>
|
|
@@ -22,8 +19,4 @@ function App() {
|
|
|
22
19
|
</button>
|
|
23
20
|
</div>
|
|
24
21
|
);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
_@renderReact('#root') {
|
|
28
|
-
<App />
|
|
29
22
|
@}
|
package/lib/cdn.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
[{"isDir":false,"category":"Ciyr","option":"A1"},{"isDir":false,"category":"Ciyr","option":"B1"},{"isDir":false,"category":"Ciyr","option":"B2"},{"isDir":false,"category":"Ciyr","option":"C1"},{"isDir":false,"category":"Ciyr","option":"C2"},{"isDir":false,"category":"Ciyr","option":"C2_2"},{"isDir":false,"category":"Ciyr","option":"a1"},{"isDir":false,"category":"Ciyr","option":"b1"},{"isDir":false,"category":"Ciyr","option":"c1"},{"isDir":false,"category":"Yrci","option":"&app"},{"isDir":false,"category":"Yrci","option":"&app2"},{"isDir":false,"category":"Yrci","option":"@m1"},{"isDir":false,"category":"Yrci","option":"@m2"},{"isDir":false,"category":"Yrci","option":"A1"},{"isDir":false,"category":"Yrci","option":"A2"},{"isDir":false,"category":"Yrci","option":"A3"},{"isDir":false,"category":"Yrci","option":"B1"},{"isDir":false,"category":"Yrci","option":"C1"},{"isDir":false,"category":"Yrci","option":"C1r"},{"isDir":false,"category":"Yrci","option":"C2"},{"isDir":false,"category":"Yrci","option":"C3"},{"isDir":false,"category":"Yrci","option":"C4"},{"isDir":false,"category":"Yrci","option":"C5"},{"isDir":false,"category":"Yrci","option":"C6"},{"isDir":false,"category":"Yrci","option":"C7"},{"isDir":false,"category":"Yrci","option":"C8"},{"isDir":false,"category":"Yrci","option":"C9"},{"isDir":false,"category":"Yrci","option":"Index_"},{"isDir":false,"category":"Yrci","option":"Login"},{"isDir":false,"category":"Yrci","option":"M1"},{"isDir":false,"category":"Yrci","option":"React"},{"isDir":false,"category":"Yrci","option":"Sp"},{"isDir":false,"category":"Yrci","option":"T1"},{"isDir":false,"category":"Yrci","option":"T2"},{"isDir":false,"category":"Yrci","option":"Ta1"},{"isDir":false,"category":"Yrci","option":"Ta2"},{"isDir":false,"category":"Yrci","option":"Tg"},{"isDir":false,"category":"Yrci","option":"Wr"},{"isDir":false,"category":"Yrci","option":"Wrapper"},{"isDir":false,"category":"Yrci","option":"Wrapper1"},{"isDir":false,"category":"Yrci","option":"Wrapper2"},{"isDir":false,"category":"Yrci","option":"Wrapper3"},{"isDir":false,"category":"Yrci","option":"Wrapperext"},{"isDir":false,"category":"Yrci","option":"Wrt"},{"isDir":false,"category":"Yrci","option":"Yrwm"},{"isDir":false,"category":"Yrci","option":"a1"},{"isDir":false,"category":"Yrci","option":"a2"},{"isDir":false,"category":"Yrci","option":"app"},{"isDir":false,"category":"Yrci","option":"b2"},{"isDir":false,"category":"Yrci","option":"j1"},{"isDir":false,"category":"Yrci","option":"j2"},{"isDir":false,"category":"Yrci","option":"j3"},{"isDir":false,"category":"Yrci","option":"modal"},{"isDir":false,"category":false,"option":"%build"},{"isDir":false,"category":false,"option":"%deploy"},{"isDir":false,"category":false,"option":"%gh-pages"},{"isDir":false,"category":false,"option":"%nvm"},{"isDir":false,"category":false,"option":"%serve"},{"isDir":false,"category":false,"option":"@react"},{"isDir":false,"category":false,"option":"@try"},{"isDir":false,"category":false,"option":"@wrapper"},{"isDir":false,"category":false,"option":"bodyblock"},{"isDir":false,"category":false,"option":"ci"},{"isDir":false,"category":false,"option":"contenteditable"},{"isDir":false,"category":false,"option":"crypto"},{"isDir":false,"category":false,"option":"element"},{"isDir":false,"category":false,"option":"hidden"},{"isDir":false,"category":false,"option":"utils"}]
|
package/lib/index.js
ADDED
package/package.json
CHANGED
package/yr.js
CHANGED
|
@@ -73,13 +73,34 @@ for (let item of Object.keys(namespaces)) {
|
|
|
73
73
|
}
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
-
let viewType;
|
|
76
|
+
let viewType, scriptUrl, builtInLib = {};
|
|
77
77
|
function transformReact(code, sections, section, config) {
|
|
78
|
-
code =
|
|
78
|
+
code = core.macros(sections, section);
|
|
79
79
|
|
|
80
80
|
if (viewType === 'cdn') {
|
|
81
|
-
if (!window.Babel)
|
|
82
|
-
|
|
81
|
+
if (!window.Babel) return 'alert("Babel unavailable");';
|
|
82
|
+
const transformed = window.Babel.transform(code, {
|
|
83
|
+
presets: [['react', { runtime: 'classic' }]],
|
|
84
|
+
plugins: ['transform-modules-commonjs']
|
|
85
|
+
}).code;
|
|
86
|
+
|
|
87
|
+
const shim = `
|
|
88
|
+
const require = (function () {
|
|
89
|
+
const modules = {
|
|
90
|
+
'react': window.React,
|
|
91
|
+
'react-dom': window.ReactDOM,
|
|
92
|
+
'react-dom/client': window.ReactDOM
|
|
93
|
+
};
|
|
94
|
+
return function (name) {
|
|
95
|
+
if (modules[name]) return modules[name];
|
|
96
|
+
throw new Error('Módulo não disponível no runtime cdn: ' + name);
|
|
97
|
+
};
|
|
98
|
+
})();
|
|
99
|
+
const exports = {};
|
|
100
|
+
const module = { exports };
|
|
101
|
+
`;
|
|
102
|
+
|
|
103
|
+
return `(function(){\n${shim}\n${transformed}\n})();`;
|
|
83
104
|
}
|
|
84
105
|
|
|
85
106
|
if (viewType === 'npm') {
|
|
@@ -108,7 +129,7 @@ function transformReact(code, sections, section, config) {
|
|
|
108
129
|
if (!unresolved) throw error;
|
|
109
130
|
|
|
110
131
|
if (sections) sections.reactBuildFailed = true;
|
|
111
|
-
return '';
|
|
132
|
+
return 'alert("esbuild error");';
|
|
112
133
|
}
|
|
113
134
|
}
|
|
114
135
|
}
|
|
@@ -672,6 +693,12 @@ const parsers = {
|
|
|
672
693
|
const core = {
|
|
673
694
|
set(libFn) { this.lib = libFn },
|
|
674
695
|
lib(category=false, option=false, parseConfig=false) {
|
|
696
|
+
if (typeof window !== 'undefined') {
|
|
697
|
+
return (category && option) ? builtInLib[`${
|
|
698
|
+
(category === '__') ? '' : category + '/'
|
|
699
|
+
}${option}`.replace(/\.yr/g, '')] : builtInLib;
|
|
700
|
+
}
|
|
701
|
+
|
|
675
702
|
return { yr: '++\n\n_' };
|
|
676
703
|
},
|
|
677
704
|
macros(sections, key, obfuscate=false) {
|
|
@@ -1910,6 +1937,36 @@ if (typeof module !== 'undefined' && module.exports) {
|
|
|
1910
1937
|
if (typeof window !== 'undefined' && !window.yr) {
|
|
1911
1938
|
viewType = 'cdn';
|
|
1912
1939
|
window.yr = core.parse.bind(core);
|
|
1940
|
+
scriptUrl = document.currentScript?.src;
|
|
1941
|
+
const scripts = [
|
|
1942
|
+
'https://unpkg.com/react@18/umd/react.production.min.js',
|
|
1943
|
+
'https://unpkg.com/react-dom@18/umd/react-dom.production.min.js',
|
|
1944
|
+
'https://unpkg.com/@babel/standalone/babel.min.js'
|
|
1945
|
+
];
|
|
1946
|
+
scripts.forEach(src => {
|
|
1947
|
+
const script = document.createElement('script');
|
|
1948
|
+
script.src = src;
|
|
1949
|
+
document.head.appendChild(script);
|
|
1950
|
+
});
|
|
1951
|
+
function getWrapperName(item) {
|
|
1952
|
+
return (item.category && item.category !== 'yr')
|
|
1953
|
+
? `${item.category}/${item.option}` : item.option;
|
|
1954
|
+
}
|
|
1955
|
+
async function setBuiltInLib() {
|
|
1956
|
+
const base = new URL('./lib/', scriptUrl);
|
|
1957
|
+
let lib = await fetch(new URL('./cdn.json', base));
|
|
1958
|
+
lib = await lib.json();
|
|
1959
|
+
for (let item of lib) {
|
|
1960
|
+
const wrapperName = getWrapperName(item);
|
|
1961
|
+
delete item.isDir;
|
|
1962
|
+
const res = await
|
|
1963
|
+
fetch(new URL(encodeURIComponent(`./${wrapperName}.yr`), base));
|
|
1964
|
+
item.yr = await res.text()
|
|
1965
|
+
//item.output = core.parse(item.yr);
|
|
1966
|
+
builtInLib[wrapperName] = item;
|
|
1967
|
+
}
|
|
1968
|
+
}
|
|
1969
|
+
setBuiltInLib();
|
|
1913
1970
|
|
|
1914
1971
|
//const createLog = (text) => {
|
|
1915
1972
|
// const parsed = parse(text);
|