@makano/rew 1.2.74 → 1.2.75
Sign up to get free protection for your applications and to get access to all the features.
- package/lib/rew/const/opt.js +3 -1
- package/lib/rew/modules/compiler.js +82 -4
- package/package.json +1 -1
package/lib/rew/const/opt.js
CHANGED
@@ -6,7 +6,7 @@ const { getFile, file } = require('./fs');
|
|
6
6
|
const babel = require('@babel/core');
|
7
7
|
const path = require('path');
|
8
8
|
const babelReact = require('@babel/preset-react');
|
9
|
-
const { readFileSync } = require('fs');
|
9
|
+
const { readFileSync, existsSync } = require('fs');
|
10
10
|
const { wait } = require('../functions/wait');
|
11
11
|
const { REW_FILE_TYPE } = require('../const/ext');
|
12
12
|
const { USING_DEFAULT } = require('../const/usage');
|
@@ -122,12 +122,43 @@ const fnextToken = (i, tokens, type, value) => {
|
|
122
122
|
.find((t) => t.type == type && (value ? t.value == value : true));
|
123
123
|
};
|
124
124
|
|
125
|
+
function declareAlias(aliases, token) {
|
126
|
+
const regex = /^#declare(\*)?\s+(\w+)\s+"([^"]+)"\s*=\s*([\s\S]*);$/;
|
127
|
+
const match = token.value.trim().match(regex);
|
128
|
+
|
129
|
+
if (match) {
|
130
|
+
const isPublic = !!match[1];
|
131
|
+
const type = match[2] == "key" ? 'IDENTIFIER' : match[2];
|
132
|
+
const name = match[3];
|
133
|
+
const value = match[4].trim();
|
134
|
+
|
135
|
+
const aliasValue = value.startsWith('${')
|
136
|
+
? new Function('token', 'tokens', 'code', value.slice(2, -1))
|
137
|
+
: value;
|
138
|
+
|
139
|
+
aliases[type] = aliases[type] || {};
|
140
|
+
aliases[type][name] = aliasValue;
|
141
|
+
|
142
|
+
if(isPublic){
|
143
|
+
execOptions._syntaxAliases[type] = execOptions._syntaxAliases[type] || {};
|
144
|
+
execOptions._syntaxAliases[type][name] = aliasValue;
|
145
|
+
}
|
146
|
+
}
|
147
|
+
}
|
148
|
+
|
125
149
|
function compileRewStuff(content, options) {
|
126
150
|
const tokens = tokenizeCoffeeScript(content);
|
127
151
|
let result = '';
|
152
|
+
let multilineDeclareBuffer = [];
|
153
|
+
let multilineDeclare = false;
|
128
154
|
|
129
155
|
let hooks = [];
|
130
156
|
|
157
|
+
|
158
|
+
const aliases = {
|
159
|
+
...execOptions._syntaxAliases
|
160
|
+
}
|
161
|
+
|
131
162
|
for (let i = 0; i < tokens.length; i++) {
|
132
163
|
const token = tokens[i];
|
133
164
|
let { token: nextToken, n } = gnextToken(i, 1, tokens) || {};
|
@@ -136,6 +167,30 @@ function compileRewStuff(content, options) {
|
|
136
167
|
continue;
|
137
168
|
}
|
138
169
|
|
170
|
+
if ((token.type === "COMMENT" && multilineDeclare) || (token.type !== "COMMENT" && multilineDeclare)) {
|
171
|
+
if(token.type === "COMMENT"){
|
172
|
+
multilineDeclareBuffer.push(token.value.startsWith('###') ? token.value.slice(3) : token.value.slice(1));
|
173
|
+
if (token.value.includes(';')) {
|
174
|
+
multilineDeclare = false;
|
175
|
+
const combinedDeclaration = multilineDeclareBuffer.join('\n');
|
176
|
+
declareAlias(aliases, { ...token, value: combinedDeclaration });
|
177
|
+
multilineDeclareBuffer = [];
|
178
|
+
}
|
179
|
+
} else {
|
180
|
+
multilineDeclare = false;
|
181
|
+
multilineDeclareBuffer = [];
|
182
|
+
}
|
183
|
+
}
|
184
|
+
|
185
|
+
if (token.type === "COMMENT" && token.value.startsWith('#declare')) {
|
186
|
+
if (token.value.includes(';')) {
|
187
|
+
declareAlias(aliases, token);
|
188
|
+
} else {
|
189
|
+
multilineDeclare = true;
|
190
|
+
multilineDeclareBuffer.push(token.value);
|
191
|
+
}
|
192
|
+
}
|
193
|
+
|
139
194
|
if (token.type === 'IDENTIFIER' && token.value === 'opt.set') {
|
140
195
|
const { token: nextNextToken } = gnextToken(i, 2, tokens) || {};
|
141
196
|
if (nextNextToken && nextNextToken.value.slice(1).slice(0, -1) == 'jsxPragma') {
|
@@ -269,6 +324,17 @@ function compileRewStuff(content, options) {
|
|
269
324
|
});
|
270
325
|
}
|
271
326
|
|
327
|
+
const aliasType = aliases[token.type];
|
328
|
+
if (aliasType && aliasType[token.value]) {
|
329
|
+
const aliasValue = aliasType[token.value];
|
330
|
+
if (typeof aliasValue === 'function') {
|
331
|
+
result += aliasValue(token, tokens, result) || "";
|
332
|
+
} else {
|
333
|
+
result += aliasValue;
|
334
|
+
}
|
335
|
+
continue;
|
336
|
+
}
|
337
|
+
|
272
338
|
// if(token.type === 'TRIPLE_STRING'){
|
273
339
|
// token.value = '```'+token.value.slice(3).slice(0, -3).replace(/\#\{/g, '${')+'```';
|
274
340
|
// }
|
@@ -282,10 +348,19 @@ function compileRewStuff(content, options) {
|
|
282
348
|
}
|
283
349
|
});
|
284
350
|
}
|
351
|
+
|
352
|
+
if (token.type === "COMMENT" && token.value.startsWith('#include')) {
|
353
|
+
const includeContent = token.value.split(' ')[1] || '';
|
354
|
+
const filename = options.filename ? path.resolve(path.dirname(options.filename), includeContent) : includeContent;
|
355
|
+
if (existsSync(filename)) {
|
356
|
+
result += '\n'+ compileRewStuff(readFileSync(filename).toString(), {
|
357
|
+
...options,
|
358
|
+
filename
|
359
|
+
});
|
360
|
+
}
|
361
|
+
}
|
285
362
|
}
|
286
363
|
|
287
|
-
// console.log(result)
|
288
|
-
|
289
364
|
return result;
|
290
365
|
}
|
291
366
|
|
@@ -298,7 +373,10 @@ const compileCivetStuff = (file, options) => {
|
|
298
373
|
js: true
|
299
374
|
};
|
300
375
|
|
301
|
-
const prepared = compileRewStuff(file.content,
|
376
|
+
const prepared = compileRewStuff(file.content, {
|
377
|
+
filename: file.path,
|
378
|
+
...options
|
379
|
+
});
|
302
380
|
let compiled = options.async ? compileCivet(prepared, compileOptions) : wait(compileCivet, prepared, compileOptions);
|
303
381
|
|
304
382
|
return {
|