@love-sqjm/magic 2026.4.15
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/LICENSE +674 -0
- package/README.md +93 -0
- package/app.js +51 -0
- package/doc/api/examples.md +563 -0
- package/doc/api/index.md +563 -0
- package/doc/architecture.md +322 -0
- package/doc/config-reference.md +646 -0
- package/doc/data-model.md +622 -0
- package/doc/development-guide.md +582 -0
- package/doc/magic-file/index.md +610 -0
- package/doc/user-guide/index.md +485 -0
- package/doc/workflow.md +548 -0
- package/index.js +157 -0
- package/magic.bat +2 -0
- package/magic.ps1 +5 -0
- package/package.json +44 -0
- package/script/build-project.js +16 -0
- package/script/config.js +23 -0
- package/script/create-project.js +73 -0
- package/script/global/printf.js +13 -0
- package/script/global/project-build-config.js +161 -0
- package/script/global/support-platform.js +5 -0
- package/script/module/compiler/global.js +43 -0
- package/script/module/compiler/id-generate.js +18 -0
- package/script/module/compiler/index-dom.js +78 -0
- package/script/module/compiler/macro-replace.js +22 -0
- package/script/module/compiler/macro.js +6 -0
- package/script/module/compiler/start.js +10 -0
- package/script/module/compiler/step/1.js +253 -0
- package/script/module/compiler/step/2.js +79 -0
- package/script/module/compiler/step/3.js +37 -0
- package/script/module/compiler/step/4.js +20 -0
- package/script/module/compiler/step/5.js +634 -0
- package/script/module/compiler/step/6.js +304 -0
- package/script/module/compiler/step/end.js +124 -0
- package/script/run-project.js +249 -0
- package/script/util/bun-fs.js +40 -0
- package/script/util/copy-dir.js +21 -0
- package/script/util/create-simple-dom-element.js +23 -0
- package/script/util/file-util.js +95 -0
- package/script/util/filtration-file.js +20 -0
- package/script/util/get-dir-all-file.js +28 -0
- package/script/util/get-first-object-key.js +9 -0
- package/script/util/is-empty-object.js +8 -0
- package/script/util/is-string-over-size.js +4 -0
- package/script/util/is.js +18 -0
- package/script/util/logging.js +142 -0
- package/script/util/task.js +16 -0
- package/script/util/traversal.js +28 -0
- package/template/platform-config/node-webkit +23 -0
- package/template/platform-config/web +1 -0
- package/template/project-base/app.xml +5 -0
- package/template/project-base/build.module.toml +37 -0
- package/template/project-base/build.toml +43 -0
- package/template/runtime/runtime.css +3 -0
- package/template/runtime/runtime.js +895 -0
|
@@ -0,0 +1,634 @@
|
|
|
1
|
+
import generate from "@babel/generator";
|
|
2
|
+
import * as parser from "@babel/parser";
|
|
3
|
+
import traverse from "@babel/traverse";
|
|
4
|
+
import * as t from "@babel/types";
|
|
5
|
+
import { parse } from 'node-html-parser';
|
|
6
|
+
import node_path from "node:path";
|
|
7
|
+
import postcss from "postcss";
|
|
8
|
+
import { printf } from "../../../global/printf.js";
|
|
9
|
+
import { is } from "../../../util/is.js";
|
|
10
|
+
import { task } from "../../../util/task.js";
|
|
11
|
+
import { traversal } from "../../../util/traversal.js";
|
|
12
|
+
import { project } from "../global.js";
|
|
13
|
+
import { idGenerate } from "../id-generate.js";
|
|
14
|
+
import { macroReplace } from "../macro-replace.js";
|
|
15
|
+
import { _6 } from "./6.js";
|
|
16
|
+
import { bunFS } from "../../../util/bun-fs.js";
|
|
17
|
+
|
|
18
|
+
function convertArrowFunctionToNormal( name, arrowFuncCode ) {
|
|
19
|
+
try {
|
|
20
|
+
const ast = parser.parse( arrowFuncCode, {
|
|
21
|
+
sourceType : "script"
|
|
22
|
+
} );
|
|
23
|
+
|
|
24
|
+
traverse( ast, {
|
|
25
|
+
ArrowFunctionExpression( path ) {
|
|
26
|
+
const node = path.node;
|
|
27
|
+
const params = node.params;
|
|
28
|
+
|
|
29
|
+
let body;
|
|
30
|
+
if ( t.isBlockStatement( node.body ) ) {
|
|
31
|
+
body = node.body;
|
|
32
|
+
} else {
|
|
33
|
+
body = t.blockStatement( [
|
|
34
|
+
t.returnStatement( node.body )
|
|
35
|
+
] );
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
let funcId = null;
|
|
39
|
+
if ( name && typeof name === 'string' && name.trim() ) {
|
|
40
|
+
funcId = t.identifier( name.trim() );
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const normalFunc = t.functionExpression(
|
|
44
|
+
funcId,
|
|
45
|
+
params,
|
|
46
|
+
body,
|
|
47
|
+
false,
|
|
48
|
+
false
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
path.replaceWith( normalFunc );
|
|
52
|
+
}
|
|
53
|
+
} );
|
|
54
|
+
|
|
55
|
+
const output = generate( ast, {
|
|
56
|
+
concise : false,
|
|
57
|
+
quotes : "double",
|
|
58
|
+
retainLines : true
|
|
59
|
+
} );
|
|
60
|
+
|
|
61
|
+
let code = output.code.trim().replace( /;$/, "" );
|
|
62
|
+
code = code.replace( /^\s*\(/, '' ).replace( /\)\s*$/, '' );
|
|
63
|
+
return code;
|
|
64
|
+
} catch ( error ) {
|
|
65
|
+
throw "转换失败:" + error.message;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
function pretreatmentMagicMacro( code, spmm = {} ) {
|
|
71
|
+
let ast;
|
|
72
|
+
try {
|
|
73
|
+
ast = parser.parse( code, {
|
|
74
|
+
sourceType : 'module',
|
|
75
|
+
plugins : [ "asyncGenerators" ],
|
|
76
|
+
locations : true
|
|
77
|
+
} );
|
|
78
|
+
} catch ( e ) {
|
|
79
|
+
throw `code: ${ e.code }\n错误原因: ${ e.reasonCode }\n行 ${ e.loc.line },列 ${ e.loc.column }\n`;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
traverse( ast, {
|
|
83
|
+
CallExpression( path ) {
|
|
84
|
+
if ( path.node.callee.type === "Identifier" && path.node.callee.name === "magic_define_include" ) {
|
|
85
|
+
const filePath = path.node.arguments.at( 0 );
|
|
86
|
+
|
|
87
|
+
if ( filePath.type === "StringLiteral" ) {
|
|
88
|
+
const fp = node_path.normalize( macroReplace( filePath.value ) );
|
|
89
|
+
if ( !bunFS.existsSync( fp ) ) throw `该文件不存在 [path:${ filePath.value }]`;
|
|
90
|
+
const data = bunFS.readFileSync( fp );
|
|
91
|
+
path.replaceWith( parser.parseExpression( data ) );
|
|
92
|
+
} else if ( filePath.type === "Identifier" ) {
|
|
93
|
+
throw `magic 宏定义 不允许使用动态变量 ${ filePath.name }`;
|
|
94
|
+
} else {
|
|
95
|
+
throw `magic_define_include 第一个参数类型应该为 string ,实际是 ${ filePath.type }`;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if ( path.node.callee.type === "Identifier" && path.node.callee.name === "magic_define_ui_data" ) {
|
|
100
|
+
const parent = path.parent;
|
|
101
|
+
|
|
102
|
+
if ( t.isVariableDeclarator( parent ) && parent.id ) {
|
|
103
|
+
if ( t.isIdentifier( parent.id ) ) {
|
|
104
|
+
spmm.uiDataName = parent.id.name;
|
|
105
|
+
} else if ( t.isObjectPattern( parent.id ) || t.isArrayPattern( parent.id ) ) {
|
|
106
|
+
throw new Error( 'magic_define_ui_data 不能使用解构赋值' );
|
|
107
|
+
}
|
|
108
|
+
} else {
|
|
109
|
+
throw new Error( 'magic_define_ui_data 必须用作变量声明' );
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if ( spmm.createUiData ) {
|
|
113
|
+
throw new Error( `不能创建多个 magic_define_ui_data` );
|
|
114
|
+
} else {
|
|
115
|
+
spmm.createUiData = true;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const data = path.node.arguments.at( 0 );
|
|
119
|
+
|
|
120
|
+
if ( t.isObjectExpression( data ) ) {
|
|
121
|
+
path.node.callee = t.identifier( 'magic.createUiData' );
|
|
122
|
+
path.node.arguments.push( t.identifier( '_args' ) );
|
|
123
|
+
} else {
|
|
124
|
+
throw new Error( `magic_define_ui_data 第一个参数类型应该为 object, 实际是 ${ data.type }` );
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if ( path.node.callee.type === "Identifier" && path.node.callee.name === "magic_dynamic_value_bind" ) {
|
|
129
|
+
if ( !spmm.createUiData ) {
|
|
130
|
+
throw new Error( `你必须创建 magic_define_ui_data 才能使用动态值绑定` );
|
|
131
|
+
}
|
|
132
|
+
path.node.callee = t.identifier( 'magic.DynamicValueBind' );
|
|
133
|
+
path.node.arguments.push( t.identifier( spmm.uiDataName ) );
|
|
134
|
+
}
|
|
135
|
+
},
|
|
136
|
+
VariableDeclaration( path ) {
|
|
137
|
+
const declarations = path.node.declarations;
|
|
138
|
+
if ( declarations.length === 1 ) {
|
|
139
|
+
const declaration = declarations[ 0 ];
|
|
140
|
+
if ( t.isObjectPattern( declaration.id ) ) {
|
|
141
|
+
const init = declaration.init;
|
|
142
|
+
|
|
143
|
+
if ( t.isCallExpression( init ) &&
|
|
144
|
+
t.isIdentifier( init.callee ) &&
|
|
145
|
+
init.callee.name === '$id' ) {
|
|
146
|
+
|
|
147
|
+
const properties = declaration.id.properties;
|
|
148
|
+
properties.forEach( prop => {
|
|
149
|
+
spmm.elementIDList.add( prop.key.name );
|
|
150
|
+
} );
|
|
151
|
+
path.remove();
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
} );
|
|
157
|
+
|
|
158
|
+
const { code : newCode } = generate( ast, {}, code );
|
|
159
|
+
return {
|
|
160
|
+
code : newCode,
|
|
161
|
+
options : {}
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function extractFunctions( code ) {
|
|
166
|
+
const ast = parser.parse( code, {
|
|
167
|
+
sourceType : 'module',
|
|
168
|
+
plugins : [ "asyncGenerators" ]
|
|
169
|
+
} );
|
|
170
|
+
|
|
171
|
+
const functions = {};
|
|
172
|
+
|
|
173
|
+
traverse( ast, {
|
|
174
|
+
AssignmentExpression( path ) {
|
|
175
|
+
if ( path.node.left.type === 'Identifier' && ( path.node.right.type === 'ArrowFunctionExpression' || path.node.right.type === 'FunctionExpression' ) ) {
|
|
176
|
+
const functionName = path.node.left.name;
|
|
177
|
+
functions[ functionName ] = generate( path.node.right ).code;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
} );
|
|
181
|
+
|
|
182
|
+
return functions;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function extractArrowFunctionNames( code ) {
|
|
186
|
+
const ast = parser.parse( code, {
|
|
187
|
+
sourceType : 'module',
|
|
188
|
+
plugins : [ "asyncGenerators" ]
|
|
189
|
+
} );
|
|
190
|
+
|
|
191
|
+
const functionNames = [];
|
|
192
|
+
|
|
193
|
+
traverse( ast, {
|
|
194
|
+
AssignmentExpression( path ) {
|
|
195
|
+
if ( path.node.left.type === 'Identifier' && path.node.right.type === 'ArrowFunctionExpression' ) {
|
|
196
|
+
functionNames.push( path.node.left.name );
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
} );
|
|
200
|
+
|
|
201
|
+
return functionNames;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function pretreatmentMagicEvent( code ) {
|
|
205
|
+
const functions = extractFunctions( code );
|
|
206
|
+
return `this.__magic_event = {${ Object.keys( functions ).map( key => `${ key }: ${ functions[ key ] }` ).join( ', ' ) }}`;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function pretreatmentMagicListen( code ) {
|
|
210
|
+
const functions = extractFunctions( code );
|
|
211
|
+
return `${ Object.keys( functions ).map( key => convertArrowFunctionToNormal( `__magic_listen_${ key }`, functions[ key ] ) ).join( "\n" ) }`;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function pretreatmentMagicInterface( code ) {
|
|
215
|
+
const functions = extractFunctions( code );
|
|
216
|
+
return `this.__magic_interface = {${ Object.keys( functions ).map( key => `${ key }: ${ functions[ key ] }` ).join( ', ' ) }}`;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function getMagicDefineScript( code ) {
|
|
220
|
+
return extractArrowFunctionNames( code );
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
let CSS_VAR = ":root { \n";
|
|
224
|
+
let CSS_VAR_COUNT = 0;
|
|
225
|
+
|
|
226
|
+
const mSource = ( () => {
|
|
227
|
+
const s = [];
|
|
228
|
+
|
|
229
|
+
return {
|
|
230
|
+
has( p ) {
|
|
231
|
+
return s.includes( p );
|
|
232
|
+
},
|
|
233
|
+
init( arr ) {
|
|
234
|
+
arr.forEach( ( item ) => {
|
|
235
|
+
s.push( item.relative().slice( 0, -2 ).replace( /\\/g, '/' ) );
|
|
236
|
+
} );
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
} )();
|
|
240
|
+
|
|
241
|
+
function trimString( str, t, rawAttrs ) {
|
|
242
|
+
if ( rawAttrs && rawAttrs.length > 0 )
|
|
243
|
+
str = str.slice( t.length + rawAttrs.length + 3 );
|
|
244
|
+
else
|
|
245
|
+
str = str.slice( t.length + 2 );
|
|
246
|
+
str = str.slice( 0, -( t.length + 3 ) || str.length );
|
|
247
|
+
return str;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function println() {
|
|
251
|
+
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
class mData {
|
|
255
|
+
#source;
|
|
256
|
+
#dateObject = {
|
|
257
|
+
"import" : {
|
|
258
|
+
"~global" : {}
|
|
259
|
+
},
|
|
260
|
+
"template" : {
|
|
261
|
+
var : {},
|
|
262
|
+
sh : [],
|
|
263
|
+
fragment : true
|
|
264
|
+
},
|
|
265
|
+
"script" : "",
|
|
266
|
+
"before" : "",
|
|
267
|
+
"global" : "",
|
|
268
|
+
"event" : "",
|
|
269
|
+
"interface" : "",
|
|
270
|
+
"listen" : "",
|
|
271
|
+
"once_interface" : [],
|
|
272
|
+
"css" : "",
|
|
273
|
+
"expose-event" : {},
|
|
274
|
+
"use-element-id-list" : []
|
|
275
|
+
};
|
|
276
|
+
#name;
|
|
277
|
+
#id;
|
|
278
|
+
#cssScope = {};
|
|
279
|
+
#originalFile;
|
|
280
|
+
#once_interface_args = {};
|
|
281
|
+
|
|
282
|
+
constructor( s ) {
|
|
283
|
+
this.#source = s;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
get data() {
|
|
287
|
+
return {
|
|
288
|
+
...this.#dateObject,
|
|
289
|
+
name : this.#name,
|
|
290
|
+
cssScope : this.#cssScope,
|
|
291
|
+
once_interface_args : this.#once_interface_args,
|
|
292
|
+
originalFile : this.#originalFile.replaceAll( '\\', '/' )
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
init() {
|
|
297
|
+
const absPath = this.#source.absolute();
|
|
298
|
+
const fileContent = bunFS.readFileSync( absPath );
|
|
299
|
+
|
|
300
|
+
const dom = parse( `<root>${ fileContent }</root>` );
|
|
301
|
+
|
|
302
|
+
this.#originalFile = node_path.normalize( absPath ).substring( project.srcDir.length );
|
|
303
|
+
if ( project.build_config.build.platform.target === "module" )
|
|
304
|
+
this.#name = ( project.build_config.config.name + this.#originalFile ).slice( 0, -2 )
|
|
305
|
+
.replace( /[^a-zA-Z]/g, '' ).toLowerCase();
|
|
306
|
+
else
|
|
307
|
+
this.#name = this.#originalFile.slice( 0, -2 )
|
|
308
|
+
.replace( /[^a-zA-Z]/g, '' ).toLowerCase();
|
|
309
|
+
|
|
310
|
+
const FILE_NAME = this.#name;
|
|
311
|
+
|
|
312
|
+
printf.outFile.info( `预处理 [path:${ absPath }] [name:${ FILE_NAME }]` );
|
|
313
|
+
|
|
314
|
+
if ( this.#name === "" ) {
|
|
315
|
+
println = console.log
|
|
316
|
+
} else {
|
|
317
|
+
println = () => {}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
task( () => {
|
|
321
|
+
function isModule( tag ) {
|
|
322
|
+
return function () {
|
|
323
|
+
if ( tag.substring( 0, 7 ) === "module:" )
|
|
324
|
+
return {
|
|
325
|
+
bool : true,
|
|
326
|
+
result : tag.substring( 7 )
|
|
327
|
+
};
|
|
328
|
+
return {
|
|
329
|
+
bool : false,
|
|
330
|
+
result : tag
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
dom.querySelectorAll( "root>import" ).forEach( ( e ) => {
|
|
336
|
+
const namespace = e.getAttribute( "namespace" ) || "~global";
|
|
337
|
+
const root = e.getAttribute( "root" ) || "";
|
|
338
|
+
|
|
339
|
+
if ( !this.#dateObject.import.hasOwnProperty( namespace ) )
|
|
340
|
+
this.#dateObject.import[ namespace ] = {};
|
|
341
|
+
const module = this.#dateObject.import[ namespace ];
|
|
342
|
+
|
|
343
|
+
e.childNodes.forEach( ( node ) => {
|
|
344
|
+
if ( node.nodeType === 3 ) return;
|
|
345
|
+
is( isModule( node.rawTagName ),
|
|
346
|
+
( tag ) => {
|
|
347
|
+
const s = tag + '/';
|
|
348
|
+
node.childNodes.forEach( ( _node ) => {
|
|
349
|
+
if ( _node.nodeType === 3 ) return;
|
|
350
|
+
const newTag = _node.rawTagName;
|
|
351
|
+
is( isModule( newTag ), () => {
|
|
352
|
+
throw `模块里面不能在嵌套一个模块了! [${ node.rawTagName } > ${ newTag }]`;
|
|
353
|
+
} );
|
|
354
|
+
const src = _node.hasAttribute( "src" ) ? _node.getAttribute( "src" ) + '/' : "";
|
|
355
|
+
module[ newTag ] = `${ root }/${ s }${ src }${ newTag }`;
|
|
356
|
+
} );
|
|
357
|
+
},
|
|
358
|
+
( tag ) => {
|
|
359
|
+
const src = node.hasAttribute( "src" ) ? node.getAttribute( "src" ) + '/' : "";
|
|
360
|
+
module[ tag ] = `${ root }/${ src }${ tag }`;
|
|
361
|
+
}
|
|
362
|
+
);
|
|
363
|
+
} );
|
|
364
|
+
} );
|
|
365
|
+
|
|
366
|
+
traversal.object( this.#dateObject.import, o => {
|
|
367
|
+
traversal.object( o, v => {
|
|
368
|
+
if ( !mSource.has( v ) ) printf.outFile.log( `外部导入 ${ v }` );
|
|
369
|
+
} );
|
|
370
|
+
} );
|
|
371
|
+
}, `预处理 [import:${ absPath }]` );
|
|
372
|
+
|
|
373
|
+
task( () => {
|
|
374
|
+
const $template = dom.querySelector( "root>template" );
|
|
375
|
+
|
|
376
|
+
traversal.object( this.#dateObject.import, ( namespace, i, k ) => {
|
|
377
|
+
const namespaceTag = ( k === "~global" ) ? "" : k + '\\:';
|
|
378
|
+
traversal.object( namespace, ( src, i, tag ) => {
|
|
379
|
+
$template.querySelectorAll( `${ namespaceTag }${ tag }` ).forEach( ( node ) => {
|
|
380
|
+
node.setAttribute( "#import", src );
|
|
381
|
+
} );
|
|
382
|
+
} );
|
|
383
|
+
} );
|
|
384
|
+
|
|
385
|
+
let count = 0;
|
|
386
|
+
const it = ( rootElement, parentNode ) => {
|
|
387
|
+
let segment = `append(${ parentNode }`;
|
|
388
|
+
rootElement.childNodes.forEach( ( element ) => {
|
|
389
|
+
let varName = "";
|
|
390
|
+
if ( element.nodeType === 1 ) {
|
|
391
|
+
const attribs = {
|
|
392
|
+
attribs : {},
|
|
393
|
+
event : {},
|
|
394
|
+
args : {},
|
|
395
|
+
keyword : {}
|
|
396
|
+
};
|
|
397
|
+
traversal.object( element.attrs, ( v, i, k ) => {
|
|
398
|
+
const c = k.at( 0 );
|
|
399
|
+
const n = k.substring( 1 );
|
|
400
|
+
if ( c === "#" ) {
|
|
401
|
+
if ( n === "import" )
|
|
402
|
+
attribs[ "import" ] = v;
|
|
403
|
+
else if ( n.includes( ':' ) ) {
|
|
404
|
+
const [ kn, vn ] = n.split( ':' );
|
|
405
|
+
if ( !attribs.keyword.hasOwnProperty( kn ) )
|
|
406
|
+
attribs.keyword[ kn ] = {};
|
|
407
|
+
attribs.keyword[ kn ][ vn ] = v;
|
|
408
|
+
} else
|
|
409
|
+
attribs.keyword[ n ] = v;
|
|
410
|
+
} else if ( c === "@" ) {
|
|
411
|
+
const ind = v.indexOf( ':' );
|
|
412
|
+
if ( ind !== -1 ) {
|
|
413
|
+
const eventName = v.substring( 0, ind );
|
|
414
|
+
attribs.event[ n ] = [ eventName, v.substring( eventName.length + 1 ) ];
|
|
415
|
+
} else attribs.event[ n ] = [ v, "{}" ];
|
|
416
|
+
} else if ( c === ":" ) {
|
|
417
|
+
attribs.args[ n ] = v;
|
|
418
|
+
} else {
|
|
419
|
+
attribs.attribs[ k ] = v;
|
|
420
|
+
}
|
|
421
|
+
} );
|
|
422
|
+
attribs[ "import" ] ? varName = `i_${ count++ }` : varName = `e_${ count++ }`;
|
|
423
|
+
this.#dateObject.template.var[ varName ] = {
|
|
424
|
+
type : attribs[ "import" ] ? "import" : "element",
|
|
425
|
+
tagName : element.tagName.toLowerCase(),
|
|
426
|
+
...attribs
|
|
427
|
+
};
|
|
428
|
+
|
|
429
|
+
const contents = element.childNodes;
|
|
430
|
+
if ( contents.length > 0 )
|
|
431
|
+
it( element, varName );
|
|
432
|
+
} else if ( element.nodeType === 3 ) {
|
|
433
|
+
if ( element.text.trim() === "" ) return;
|
|
434
|
+
varName = `t_${ count++ }`;
|
|
435
|
+
|
|
436
|
+
this.#dateObject.template.var[ varName ] = {
|
|
437
|
+
type : "text",
|
|
438
|
+
content : element.text
|
|
439
|
+
};
|
|
440
|
+
} else {
|
|
441
|
+
return;
|
|
442
|
+
}
|
|
443
|
+
segment += `,${ varName }`;
|
|
444
|
+
} );
|
|
445
|
+
const temp = segment.substring( 33 ).split( "," );
|
|
446
|
+
|
|
447
|
+
if ( temp.length === 1 ) {
|
|
448
|
+
this.#dateObject.template.fragment = false;
|
|
449
|
+
}
|
|
450
|
+
this.#dateObject.template.sh.push( segment + ");" );
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
it( $template, "this.__magic_element_root" );
|
|
454
|
+
|
|
455
|
+
this.#dateObject[ "templateArgs" ] = {
|
|
456
|
+
inline : $template.hasAttribute( "inline" )
|
|
457
|
+
}
|
|
458
|
+
}, `预处理 [template:${ absPath }]` );
|
|
459
|
+
|
|
460
|
+
const _rs_before = [],
|
|
461
|
+
_rs_global = [],
|
|
462
|
+
_rs_script = [],
|
|
463
|
+
_rs_event = [],
|
|
464
|
+
_rs_listen = [],
|
|
465
|
+
_rs_interface = [];
|
|
466
|
+
|
|
467
|
+
dom.querySelectorAll( `root > script` ).forEach( ( e ) => {
|
|
468
|
+
if ( e.attributes[ "code" ] === "before" ) {
|
|
469
|
+
_rs_before.push( e );
|
|
470
|
+
} else if ( e.attributes[ "code" ] === "event" ) {
|
|
471
|
+
_rs_event.push( e );
|
|
472
|
+
} else if ( e.attributes[ "code" ] === "event" ) {
|
|
473
|
+
_rs_event.push( e );
|
|
474
|
+
} else if ( e.attributes[ "code" ] === "interface" ) {
|
|
475
|
+
_rs_interface.push( e );
|
|
476
|
+
} else if ( e.attributes[ "code" ] === "listen" ) {
|
|
477
|
+
_rs_listen.push( e );
|
|
478
|
+
} else {
|
|
479
|
+
_rs_script.push( e );
|
|
480
|
+
}
|
|
481
|
+
} );
|
|
482
|
+
|
|
483
|
+
task( () => {
|
|
484
|
+
dom.querySelectorAll( `root > expose-event` ).forEach( ( e ) => {
|
|
485
|
+
e.childNodes.forEach( child => {
|
|
486
|
+
if ( child.nodeType !== 1 ) return;
|
|
487
|
+
const nodeName = child.tagName.toLowerCase();
|
|
488
|
+
this.#dateObject[ "expose-event" ][ nodeName ] = Object.keys( child.attributes ).map( attrName => {
|
|
489
|
+
return attrName.replace( /^:/, '' );
|
|
490
|
+
} );
|
|
491
|
+
} );
|
|
492
|
+
} );
|
|
493
|
+
this.#dateObject[ "expose-event" ] = JSON.stringify( this.#dateObject[ "expose-event" ] );
|
|
494
|
+
}, `预处理 [expose-event:${ absPath }]` );
|
|
495
|
+
const scopePretreatmentMagicMacro = {
|
|
496
|
+
createUiData : false,
|
|
497
|
+
uiDataName : "",
|
|
498
|
+
elementIDList : new Set()
|
|
499
|
+
};
|
|
500
|
+
|
|
501
|
+
task( () => {
|
|
502
|
+
_rs_before.forEach( ( e ) => {
|
|
503
|
+
this.#dateObject.before += `${ trimString( e.outerHTML, "script", e.rawAttrs ) }`;
|
|
504
|
+
} );
|
|
505
|
+
|
|
506
|
+
this.#dateObject.before = pretreatmentMagicMacro( this.#dateObject.before, scopePretreatmentMagicMacro );
|
|
507
|
+
}, `预处理 [before:${ absPath }]` );
|
|
508
|
+
|
|
509
|
+
task( () => {
|
|
510
|
+
_rs_global.forEach( ( e ) => {
|
|
511
|
+
this.#dateObject.global += `${ trimString( e.outerHTML, "script", e.rawAttrs ) }`;
|
|
512
|
+
} );
|
|
513
|
+
|
|
514
|
+
this.#dateObject.global = pretreatmentMagicMacro( this.#dateObject.global, scopePretreatmentMagicMacro );
|
|
515
|
+
}, `预处理 [global:${ absPath }]` );
|
|
516
|
+
|
|
517
|
+
task( () => {
|
|
518
|
+
_rs_script.forEach( ( e ) => { this.#dateObject.script += `${ trimString( e.outerHTML, "script", e.rawAttrs ) }`; } );
|
|
519
|
+
|
|
520
|
+
this.#dateObject.script = pretreatmentMagicMacro( this.#dateObject.script, scopePretreatmentMagicMacro );
|
|
521
|
+
}, `预处理 [script:${ absPath }]` );
|
|
522
|
+
|
|
523
|
+
this.#dateObject[ "use-element-id-list" ] = [ ...scopePretreatmentMagicMacro.elementIDList ];
|
|
524
|
+
|
|
525
|
+
task( () => {
|
|
526
|
+
_rs_event.forEach( ( e ) => { this.#dateObject.event += `${ trimString( e.outerHTML, "script", e.rawAttrs ) }`; } );
|
|
527
|
+
|
|
528
|
+
const oo = this.#dateObject.event;
|
|
529
|
+
this.#dateObject.event = {
|
|
530
|
+
code : pretreatmentMagicEvent( oo ),
|
|
531
|
+
list : getMagicDefineScript( oo )
|
|
532
|
+
};
|
|
533
|
+
}, `预处理 [event:${ absPath }]` );
|
|
534
|
+
|
|
535
|
+
task( () => {
|
|
536
|
+
_rs_listen.forEach( ( e ) => { this.#dateObject.listen += `${ trimString( e.outerHTML, "script", e.rawAttrs ) }`; } );
|
|
537
|
+
|
|
538
|
+
const oo = this.#dateObject.listen;
|
|
539
|
+
this.#dateObject.listen = {
|
|
540
|
+
code : pretreatmentMagicListen( oo ),
|
|
541
|
+
list : getMagicDefineScript( oo )
|
|
542
|
+
};
|
|
543
|
+
}, `预处理 [listen:${ absPath }]` );
|
|
544
|
+
|
|
545
|
+
task( () => {
|
|
546
|
+
_rs_interface.forEach( ( e ) => {
|
|
547
|
+
const code = trimString( e.outerHTML, "script", e.rawAttrs );
|
|
548
|
+
this.#dateObject.interface += code;
|
|
549
|
+
|
|
550
|
+
if ( e.hasAttribute( "once" ) ) {
|
|
551
|
+
const li = getMagicDefineScript( code );
|
|
552
|
+
this.#dateObject.once_interface.push( ...li );
|
|
553
|
+
}
|
|
554
|
+
} );
|
|
555
|
+
const oo = this.#dateObject.interface;
|
|
556
|
+
this.#dateObject.interface = {
|
|
557
|
+
code : pretreatmentMagicInterface( oo ),
|
|
558
|
+
list : getMagicDefineScript( oo )
|
|
559
|
+
};
|
|
560
|
+
}, `预处理 [interface:${ absPath }]` );
|
|
561
|
+
|
|
562
|
+
task( () => {
|
|
563
|
+
const cssDefaultThemeTransitionVar = () => {
|
|
564
|
+
return {
|
|
565
|
+
postcssPlugin : 'css-default-theme-transition-var',
|
|
566
|
+
Once( root ) {
|
|
567
|
+
root.walkAtRules( ( rule ) => {
|
|
568
|
+
if ( rule.name === 'keyframes' || rule.name === 'media' ) {
|
|
569
|
+
rule.remove();
|
|
570
|
+
}
|
|
571
|
+
} );
|
|
572
|
+
|
|
573
|
+
root.walkRules( ( rule ) => {
|
|
574
|
+
const nt = rule.selector
|
|
575
|
+
.replace( /[&>.="*\[\]]/g, "" )
|
|
576
|
+
.replace( /\s+/g, " " )
|
|
577
|
+
.replace( /[^a-zA-Z0-9]/g, '-' );
|
|
578
|
+
rule.walkDecls( ( decl ) => {
|
|
579
|
+
const varName = `--${ FILE_NAME }-${ nt }-${ decl.prop.replace( /-/g, '-' ) }-${ CSS_VAR_COUNT++ }`;
|
|
580
|
+
CSS_VAR += `${ varName }: ${ decl.value }; \n`;
|
|
581
|
+
decl.value = `var(${ varName })`;
|
|
582
|
+
} );
|
|
583
|
+
} );
|
|
584
|
+
}
|
|
585
|
+
};
|
|
586
|
+
};
|
|
587
|
+
cssDefaultThemeTransitionVar.postcss = true;
|
|
588
|
+
|
|
589
|
+
dom.querySelectorAll( `root > css` ).forEach( ( e ) => {
|
|
590
|
+
const code = trimString( e.outerHTML, "css", e.rawAttrs );
|
|
591
|
+
|
|
592
|
+
let scope = e.hasAttribute( "scope" ) ? e.getAttribute( "scope" ) : "&";
|
|
593
|
+
if ( scope.trim() === "" ) {
|
|
594
|
+
return;
|
|
595
|
+
}
|
|
596
|
+
if ( scope.length > 5 && scope.substring( 0, 4 ) === "#id:" ) {
|
|
597
|
+
scope = scope.substring( 4 );
|
|
598
|
+
if ( this.#cssScope.hasOwnProperty( scope ) ) {
|
|
599
|
+
scope = `.${ this.#cssScope[ scope ] }`;
|
|
600
|
+
} else {
|
|
601
|
+
const id = "m-css-scope-" + idGenerate( 6 );
|
|
602
|
+
this.#cssScope[ scope ] = id;
|
|
603
|
+
scope = `.${ id }`;
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
if ( e.hasAttribute( "default-theme" ) && project.build_config.build.optimize[ "out-default-theme" ] ) {
|
|
608
|
+
const result = postcss( [ cssDefaultThemeTransitionVar() ] ).process( code );
|
|
609
|
+
this.#dateObject.css += `${ scope } { ${ result.css } }\n`;
|
|
610
|
+
} else this.#dateObject.css += `${ scope } { ${ code } }\n`;
|
|
611
|
+
} );
|
|
612
|
+
}, `预处理 [css:${ absPath }]` );
|
|
613
|
+
return this;
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
export function _5( m ) {
|
|
618
|
+
const promises = [];
|
|
619
|
+
|
|
620
|
+
mSource.init( m );
|
|
621
|
+
|
|
622
|
+
m.forEach( s => {
|
|
623
|
+
promises.push( new Promise( ( resolve, reject ) => {
|
|
624
|
+
const data = new mData( s );
|
|
625
|
+
data.init();
|
|
626
|
+
resolve( data );
|
|
627
|
+
} ) );
|
|
628
|
+
} );
|
|
629
|
+
|
|
630
|
+
Promise.all( promises ).then( ms => {
|
|
631
|
+
CSS_VAR += "}";
|
|
632
|
+
_6( ms, CSS_VAR );
|
|
633
|
+
} );
|
|
634
|
+
}
|