@cocojs/compiler 0.1.0-beta.202601082202 → 0.1.0-beta.202601082203

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/README.md CHANGED
@@ -1,10 +1,3 @@
1
- # @cocojs/compiler
2
-
3
- 基于 typescript api 的转译程序。
4
-
5
- ✨ **功能**
6
- - 组件添加`static $$id`属性
7
- - `@constructorParams()`装饰器添加入参
8
- - `@autowired()`装饰器添加入参
9
- - `@component()`装饰器添加入参
1
+ # coconut-framework [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/cocojs-org/coconut-framework/blob/main/LICENSE)
10
2
 
3
+ `@cocojs/bundle-webpack`属于[coconut-framework](https://github.com/cocojs-org/coconut-framework)代码仓,更多内容见[cocojs.dev](https://cocojs.dev)。
package/dist/index.cjs.js CHANGED
@@ -186,6 +186,7 @@ function createImport(className, importPath) {
186
186
  }
187
187
  function updateTypeImports(sourceFile) {
188
188
  var identifyList = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
189
+ var importConstructorInjectDecorator = arguments.length > 2 ? arguments[2] : undefined;
189
190
  if (!identifyList.length) {
190
191
  return sourceFile;
191
192
  }
@@ -239,11 +240,14 @@ function updateTypeImports(sourceFile) {
239
240
  } finally {
240
241
  _iterator.f();
241
242
  }
243
+ if (importConstructorInjectDecorator) {
244
+ newStatements.push(ts__namespace.factory.createImportDeclaration(undefined, ts__namespace.factory.createImportClause(undefined, undefined, ts__namespace.factory.createNamedImports([ts__namespace.factory.createImportSpecifier(false, undefined, ts__namespace.factory.createIdentifier('constructorInject'))])), ts__namespace.factory.createStringLiteral(importConstructorInjectDecorator.module), undefined));
245
+ }
242
246
  return ts__namespace.factory.updateSourceFile(sourceFile, [].concat(newStatements, _toConsumableArray(sourceFile.statements)));
243
247
  }
244
248
 
245
249
  function isConstructParamsDecorator(decorator) {
246
- return isDecoratorExp(decorator, 'constructorParam');
250
+ return isDecoratorExp(decorator, 'constructorInject');
247
251
  }
248
252
  function extractIdentifiersFromConstructor(ctor) {
249
253
  var identifiers = [];
@@ -268,7 +272,7 @@ function extractIdentifiersFromConstructor(ctor) {
268
272
  }
269
273
  return identifiers;
270
274
  }
271
- function updateConstructorParamDecorator(classDeclaration) {
275
+ function updateConstructorInjectDecorator(classDeclaration) {
272
276
  var modifiers = classDeclaration.modifiers;
273
277
  if (!modifiers) {
274
278
  return {
@@ -284,15 +288,8 @@ function updateConstructorParamDecorator(classDeclaration) {
284
288
  };
285
289
  }
286
290
  var constructParams = decorators.find(isConstructParamsDecorator);
287
- if (!constructParams) {
288
- return {
289
- updated: false,
290
- modifiers: classDeclaration.modifiers
291
- };
292
- }
293
- var call = constructParams.expression;
294
- // 已有参数,不处理
295
- if (call.arguments.length > 0) {
291
+ if (constructParams) {
292
+ // 用户自定义,不处理
296
293
  return {
297
294
  updated: false,
298
295
  modifiers: classDeclaration.modifiers
@@ -315,15 +312,12 @@ function updateConstructorParamDecorator(classDeclaration) {
315
312
  modifiers: classDeclaration.modifiers
316
313
  };
317
314
  }
318
- // 构造数组:[Api, User]
319
- var arrayLiteral = ts__namespace.factory.createArrayLiteralExpression(identifiers, false);
320
- var newModifiers = modifiers.map(function (m) {
321
- return ts__namespace.isDecorator(m) && m === constructParams ? ts__namespace.factory.createDecorator(ts__namespace.factory.updateCallExpression(call, call.expression, call.typeArguments, [arrayLiteral])) : m;
322
- });
315
+ var constructorInjectDecorator = ts__namespace.factory.createDecorator(ts__namespace.factory.createCallExpression(ts__namespace.factory.createIdentifier('constructorInject'), undefined, [ts__namespace.factory.createArrayLiteralExpression(identifiers, false)]));
316
+ var newModifiers = [].concat(_toConsumableArray(modifiers), [constructorInjectDecorator]);
323
317
  return {
324
318
  updated: true,
325
319
  modifiers: newModifiers,
326
- constructorParamTypeList: identifiers
320
+ constructorInjectTypeList: identifiers
327
321
  };
328
322
  }
329
323
 
@@ -459,26 +453,32 @@ function updateMembers(classDeclaration) {
459
453
  componentList: componentList
460
454
  };
461
455
  }
456
+ /**
457
+ * 转换工具工厂函数
458
+ * @param idPrefix id前缀
459
+ * @param addConstructorInjectImportStmt 遇到构造函数有参数时,是否添加import { constructorInject } from 'xxx'语句
460
+ */
462
461
  function transformerFactory() {
463
- var prefix = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
462
+ var idPrefix = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
463
+ var addConstructorInjectImportStmt = arguments.length > 1 ? arguments[1] : undefined;
464
464
  return function (context) {
465
- var constructorParamList = [];
465
+ var constructorInjectList = [];
466
466
  var autowiredList = [];
467
467
  var componentList = [];
468
468
  var _visit = function visit(node) {
469
469
  if (ts__namespace.isClassDeclaration(node)) {
470
470
  if (hasClassKindDecorator(node)) {
471
- var $$idProperty = ifNeedAdd$$idProperty(node, prefix);
471
+ var $$idProperty = ifNeedAdd$$idProperty(node, idPrefix);
472
472
  var _updateMembers = updateMembers(node),
473
473
  members = _updateMembers.members,
474
474
  membersUpdated = _updateMembers.updated,
475
475
  _autowiredList = _updateMembers.autowiredList,
476
476
  _componentList = _updateMembers.componentList;
477
- var _updateConstructorPar = updateConstructorParamDecorator(node),
478
- modifiers = _updateConstructorPar.modifiers,
479
- constructorParamsUpdated = _updateConstructorPar.updated,
480
- _constructorParamTypeList = _updateConstructorPar.constructorParamTypeList;
481
- if (!membersUpdated && !constructorParamsUpdated && !$$idProperty) {
477
+ var _updateConstructorInj = updateConstructorInjectDecorator(node),
478
+ modifiers = _updateConstructorInj.modifiers,
479
+ constructorInjectUpdated = _updateConstructorInj.updated,
480
+ _constructorInjectTypeList = _updateConstructorInj.constructorInjectTypeList;
481
+ if (!membersUpdated && !constructorInjectUpdated && !$$idProperty) {
482
482
  return node;
483
483
  } else {
484
484
  if (_autowiredList) {
@@ -487,8 +487,8 @@ function transformerFactory() {
487
487
  if (_componentList) {
488
488
  componentList = _componentList;
489
489
  }
490
- if (constructorParamsUpdated) {
491
- constructorParamList = _constructorParamTypeList;
490
+ if (constructorInjectUpdated) {
491
+ constructorInjectList = _constructorInjectTypeList;
492
492
  }
493
493
  return ts__namespace.factory.updateClassDeclaration(node, modifiers, node.name, node.typeParameters, node.heritageClauses, $$idProperty ? [$$idProperty].concat(_toConsumableArray(members)) : members);
494
494
  }
@@ -501,10 +501,20 @@ function transformerFactory() {
501
501
  // @ts-ignore
502
502
  return function (sourceFile) {
503
503
  var updatedSourceFile = ts__namespace.visitNode(sourceFile, _visit);
504
- if (!constructorParamList.length && !autowiredList.length && !componentList.length) {
504
+ if (!constructorInjectList.length && !autowiredList.length && !componentList.length) {
505
505
  return updatedSourceFile;
506
506
  }
507
- return updateTypeImports(updatedSourceFile, [].concat(_toConsumableArray(constructorParamList), _toConsumableArray(autowiredList), _toConsumableArray(componentList)));
507
+ var importConstructorInjectDecorator;
508
+ if (!!constructorInjectList.length && addConstructorInjectImportStmt) {
509
+ if (addConstructorInjectImportStmt === 'coco-ioc-container' || addConstructorInjectImportStmt === '@cocojs/mvc') {
510
+ importConstructorInjectDecorator = {
511
+ module: addConstructorInjectImportStmt
512
+ };
513
+ } else {
514
+ console.error('未知的addConstructorInjectImportStmt', addConstructorInjectImportStmt);
515
+ }
516
+ }
517
+ return updateTypeImports(updatedSourceFile, [].concat(_toConsumableArray(constructorInjectList), _toConsumableArray(autowiredList), _toConsumableArray(componentList)), importConstructorInjectDecorator);
508
518
  };
509
519
  };
510
520
  }
@@ -516,13 +526,14 @@ var commonCompilerOptions = {
516
526
  };
517
527
  function compileOneFile(code, fileName) {
518
528
  var prefix = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
529
+ var addConstructorInjectImportStmt = arguments.length > 3 ? arguments[3] : undefined;
519
530
  var result = ts__namespace.transpileModule(code, {
520
531
  fileName: fileName,
521
532
  compilerOptions: _objectSpread2(_objectSpread2({}, commonCompilerOptions), {}, {
522
533
  sourceMap: true
523
534
  }),
524
535
  transformers: {
525
- before: [transformerFactory(prefix)]
536
+ before: [transformerFactory(prefix, addConstructorInjectImportStmt)]
526
537
  }
527
538
  });
528
539
  return {
package/dist/index.d.ts CHANGED
@@ -2,11 +2,16 @@ import * as ts from 'typescript';
2
2
 
3
3
  export declare const commonCompilerOptions: ts.CompilerOptions;
4
4
 
5
- export declare function compileOneFile(code: string, fileName: string, prefix?: string): {
5
+ export declare function compileOneFile(code: string, fileName: string, prefix?: string, addConstructorInjectImportStmt?: 'coco-ioc-container' | '@cocojs/mvc'): {
6
6
  code: string;
7
7
  map?: string;
8
8
  };
9
9
 
10
- export declare function transformerFactory(prefix?: string): ts.TransformerFactory<ts.SourceFile>;
10
+ /**
11
+ * 转换工具工厂函数
12
+ * @param idPrefix id前缀
13
+ * @param addConstructorInjectImportStmt 遇到构造函数有参数时,是否添加import { constructorInject } from 'xxx'语句
14
+ */
15
+ export declare function transformerFactory(idPrefix?: string, addConstructorInjectImportStmt?: 'coco-ioc-container' | '@cocojs/mvc'): ts.TransformerFactory<ts.SourceFile>;
11
16
 
12
17
  export { }
package/dist/index.esm.js CHANGED
@@ -165,6 +165,7 @@ function createImport(className, importPath) {
165
165
  }
166
166
  function updateTypeImports(sourceFile) {
167
167
  var identifyList = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
168
+ var importConstructorInjectDecorator = arguments.length > 2 ? arguments[2] : undefined;
168
169
  if (!identifyList.length) {
169
170
  return sourceFile;
170
171
  }
@@ -218,11 +219,14 @@ function updateTypeImports(sourceFile) {
218
219
  } finally {
219
220
  _iterator.f();
220
221
  }
222
+ if (importConstructorInjectDecorator) {
223
+ newStatements.push(ts.factory.createImportDeclaration(undefined, ts.factory.createImportClause(undefined, undefined, ts.factory.createNamedImports([ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier('constructorInject'))])), ts.factory.createStringLiteral(importConstructorInjectDecorator.module), undefined));
224
+ }
221
225
  return ts.factory.updateSourceFile(sourceFile, [].concat(newStatements, _toConsumableArray(sourceFile.statements)));
222
226
  }
223
227
 
224
228
  function isConstructParamsDecorator(decorator) {
225
- return isDecoratorExp(decorator, 'constructorParam');
229
+ return isDecoratorExp(decorator, 'constructorInject');
226
230
  }
227
231
  function extractIdentifiersFromConstructor(ctor) {
228
232
  var identifiers = [];
@@ -247,7 +251,7 @@ function extractIdentifiersFromConstructor(ctor) {
247
251
  }
248
252
  return identifiers;
249
253
  }
250
- function updateConstructorParamDecorator(classDeclaration) {
254
+ function updateConstructorInjectDecorator(classDeclaration) {
251
255
  var modifiers = classDeclaration.modifiers;
252
256
  if (!modifiers) {
253
257
  return {
@@ -263,15 +267,8 @@ function updateConstructorParamDecorator(classDeclaration) {
263
267
  };
264
268
  }
265
269
  var constructParams = decorators.find(isConstructParamsDecorator);
266
- if (!constructParams) {
267
- return {
268
- updated: false,
269
- modifiers: classDeclaration.modifiers
270
- };
271
- }
272
- var call = constructParams.expression;
273
- // 已有参数,不处理
274
- if (call.arguments.length > 0) {
270
+ if (constructParams) {
271
+ // 用户自定义,不处理
275
272
  return {
276
273
  updated: false,
277
274
  modifiers: classDeclaration.modifiers
@@ -294,15 +291,12 @@ function updateConstructorParamDecorator(classDeclaration) {
294
291
  modifiers: classDeclaration.modifiers
295
292
  };
296
293
  }
297
- // 构造数组:[Api, User]
298
- var arrayLiteral = ts.factory.createArrayLiteralExpression(identifiers, false);
299
- var newModifiers = modifiers.map(function (m) {
300
- return ts.isDecorator(m) && m === constructParams ? ts.factory.createDecorator(ts.factory.updateCallExpression(call, call.expression, call.typeArguments, [arrayLiteral])) : m;
301
- });
294
+ var constructorInjectDecorator = ts.factory.createDecorator(ts.factory.createCallExpression(ts.factory.createIdentifier('constructorInject'), undefined, [ts.factory.createArrayLiteralExpression(identifiers, false)]));
295
+ var newModifiers = [].concat(_toConsumableArray(modifiers), [constructorInjectDecorator]);
302
296
  return {
303
297
  updated: true,
304
298
  modifiers: newModifiers,
305
- constructorParamTypeList: identifiers
299
+ constructorInjectTypeList: identifiers
306
300
  };
307
301
  }
308
302
 
@@ -438,26 +432,32 @@ function updateMembers(classDeclaration) {
438
432
  componentList: componentList
439
433
  };
440
434
  }
435
+ /**
436
+ * 转换工具工厂函数
437
+ * @param idPrefix id前缀
438
+ * @param addConstructorInjectImportStmt 遇到构造函数有参数时,是否添加import { constructorInject } from 'xxx'语句
439
+ */
441
440
  function transformerFactory() {
442
- var prefix = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
441
+ var idPrefix = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
442
+ var addConstructorInjectImportStmt = arguments.length > 1 ? arguments[1] : undefined;
443
443
  return function (context) {
444
- var constructorParamList = [];
444
+ var constructorInjectList = [];
445
445
  var autowiredList = [];
446
446
  var componentList = [];
447
447
  var _visit = function visit(node) {
448
448
  if (ts.isClassDeclaration(node)) {
449
449
  if (hasClassKindDecorator(node)) {
450
- var $$idProperty = ifNeedAdd$$idProperty(node, prefix);
450
+ var $$idProperty = ifNeedAdd$$idProperty(node, idPrefix);
451
451
  var _updateMembers = updateMembers(node),
452
452
  members = _updateMembers.members,
453
453
  membersUpdated = _updateMembers.updated,
454
454
  _autowiredList = _updateMembers.autowiredList,
455
455
  _componentList = _updateMembers.componentList;
456
- var _updateConstructorPar = updateConstructorParamDecorator(node),
457
- modifiers = _updateConstructorPar.modifiers,
458
- constructorParamsUpdated = _updateConstructorPar.updated,
459
- _constructorParamTypeList = _updateConstructorPar.constructorParamTypeList;
460
- if (!membersUpdated && !constructorParamsUpdated && !$$idProperty) {
456
+ var _updateConstructorInj = updateConstructorInjectDecorator(node),
457
+ modifiers = _updateConstructorInj.modifiers,
458
+ constructorInjectUpdated = _updateConstructorInj.updated,
459
+ _constructorInjectTypeList = _updateConstructorInj.constructorInjectTypeList;
460
+ if (!membersUpdated && !constructorInjectUpdated && !$$idProperty) {
461
461
  return node;
462
462
  } else {
463
463
  if (_autowiredList) {
@@ -466,8 +466,8 @@ function transformerFactory() {
466
466
  if (_componentList) {
467
467
  componentList = _componentList;
468
468
  }
469
- if (constructorParamsUpdated) {
470
- constructorParamList = _constructorParamTypeList;
469
+ if (constructorInjectUpdated) {
470
+ constructorInjectList = _constructorInjectTypeList;
471
471
  }
472
472
  return ts.factory.updateClassDeclaration(node, modifiers, node.name, node.typeParameters, node.heritageClauses, $$idProperty ? [$$idProperty].concat(_toConsumableArray(members)) : members);
473
473
  }
@@ -480,10 +480,20 @@ function transformerFactory() {
480
480
  // @ts-ignore
481
481
  return function (sourceFile) {
482
482
  var updatedSourceFile = ts.visitNode(sourceFile, _visit);
483
- if (!constructorParamList.length && !autowiredList.length && !componentList.length) {
483
+ if (!constructorInjectList.length && !autowiredList.length && !componentList.length) {
484
484
  return updatedSourceFile;
485
485
  }
486
- return updateTypeImports(updatedSourceFile, [].concat(_toConsumableArray(constructorParamList), _toConsumableArray(autowiredList), _toConsumableArray(componentList)));
486
+ var importConstructorInjectDecorator;
487
+ if (!!constructorInjectList.length && addConstructorInjectImportStmt) {
488
+ if (addConstructorInjectImportStmt === 'coco-ioc-container' || addConstructorInjectImportStmt === '@cocojs/mvc') {
489
+ importConstructorInjectDecorator = {
490
+ module: addConstructorInjectImportStmt
491
+ };
492
+ } else {
493
+ console.error('未知的addConstructorInjectImportStmt', addConstructorInjectImportStmt);
494
+ }
495
+ }
496
+ return updateTypeImports(updatedSourceFile, [].concat(_toConsumableArray(constructorInjectList), _toConsumableArray(autowiredList), _toConsumableArray(componentList)), importConstructorInjectDecorator);
487
497
  };
488
498
  };
489
499
  }
@@ -495,13 +505,14 @@ var commonCompilerOptions = {
495
505
  };
496
506
  function compileOneFile(code, fileName) {
497
507
  var prefix = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
508
+ var addConstructorInjectImportStmt = arguments.length > 3 ? arguments[3] : undefined;
498
509
  var result = ts.transpileModule(code, {
499
510
  fileName: fileName,
500
511
  compilerOptions: _objectSpread2(_objectSpread2({}, commonCompilerOptions), {}, {
501
512
  sourceMap: true
502
513
  }),
503
514
  transformers: {
504
- before: [transformerFactory(prefix)]
515
+ before: [transformerFactory(prefix, addConstructorInjectImportStmt)]
505
516
  }
506
517
  });
507
518
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocojs/compiler",
3
- "version": "0.1.0-beta.202601082202",
3
+ "version": "0.1.0-beta.202601082203",
4
4
  "description": "A Coco component transpiler built on the TypeScript compiler API.",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.esm.js",