@vaadin/hilla-generator-plugin-signals 24.9.13 → 24.9.14
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/SignalProcessor.js.map +1 -1
- package/package.json +6 -6
package/SignalProcessor.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"AAAA,SAAS,SAAS,0BAA2B;AAE7C,SAAS,0EAA2E;AACpF,SAAS,sDAAuD;AAChE,OAAO,yEAA0E;AACjF,OAAO,wFAAyF;AAChG,OAAO,4EAA6E;AACpF,OAAO,OAAO,uCAAwC;AACtD,SACE,SACA,qBACA,cAOA,YACA,YACA,WACA,uBACA,qBACA,mCAEkB;AACpB,SAAS,aAAa,oBAAoB,mCAAoC;AAE9E,MAAM,sBAAsB;AAC5B,MAAM,iBAAiB;AAEvB,eAAe,MAAM,gBAAgB;CACnC,AAASA;CACT,AAASC;CACT,AAASC;CACT,AAASC;CACT,AAASC;CAET,YAAYC,SAAiBC,SAA8BC,YAAwBC,OAAe;AAChG,OAAKN,WAAW;AAChB,OAAKC,WAAW;AAChB,OAAKC,cAAc;AACnB,OAAKH,SAAS;AACd,OAAKD,qBAAqB,IAAI,kBAAkB,IAAI,YAAY,EAAE,WAAW,MAAO;AACpF,OAAKA,mBAAmB,QAAQ,SAAS,KAAKI,YAAY;CAC3D;CAED,UAAsB;AACpB,OAAKH,OAAO,OAAO,OAAO,sBAAsB,KAAKC,SAAS,EAAE;EAChE,MAAM,EAAE,SAAS,GAAG,KAAKF;EAEzB,MAAM,GAAG,gBAAgB,GAAG,SAAS,KAAK,QAAQ,QAAQ,CAAC,KAAK,CAAC,CAAC,KAAK,KAAK,KAAK,SAAS,iBAAiB,CAAC;EAE5G,MAAM,aAAa,QAAQ,MAAM,cAAc,0BAA0B,sBAAsB;EAC/F,IAAI,qBAAqB;EAEzB,MAAM,CAAC,KAAK,GAAG,UAAsB,KAAKI,aAAa,CACrD,kBAAkB,CAAC,SAAS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AAa,KAAK,EAAE;AAGtB,OAAI,YAAY,SAAS,KAAK,KAAK,EAAE;AACnC,WACE,QAAQ,MAAM,cAAc,gBAAgB,aAAa,IAAI,QAAQ,MAAM,IAAI,gBAAgB,aAAa;GAE/G;GAGD,MAAM,CAAC,KAAK,GAAG,SAAS,KAAK,QAAQ,QAAQ,CAAC,KAAK,CAAC,GAAG,GAAG,KAAK,OAAO,KAAK,IAAI,CAAE;AAEjF,QAAK,MAAM;AACT,UAAM,IAAI,OAAO,sBAAsB,KAAK,KAAK;GAClD;GAED,MAAM,aAAa,EAAE,KAAK,KAAK;GAC/B,MAAM,aAAa,EAAE,QAAQ,KAAK,CAAC,GAAG,UAAU,EAAE,QAAQ,KAAK,CAAC;AAEhE,UAAO,QAAQ,QAAQ,cAAc,UAAU,IAAI,QAAQ,QAAQ,IAAI,WAAW,UAAU;EAC7F,WAAU,WAAW,KAAK,EAAE;GAG3B,IAAIc;AAEJ,WAAQ,KAAK,MAAb;IACE,KAAK,WAAW;AACd,iBAAY;AACZ;IACF,KAAK,WAAW;AACd,iBAAY;AACZ;IACF,KAAK,WAAW;AACd,iBAAY;AACZ;IACF,KAAK,WAAW;AACd,iBAAY;AACZ;IACF,QACE,QAAO;GACV;AAED,UAAO,QAAQ,MAAM,cAAc,gBAAgB,UAAU,IAAI,QAAQ,MAAM,IAAI,gBAAgB,UAAU;EAC9G;AAED,SAAO;CACR;AACF","names":["#dependencyManager","#owner","#service","#methods","#sourceFile","service: string","methods: Map<string, string>","sourceFile: SourceFile","owner: Plugin","#createDefaultValue","signalClass: string","returnType?: TypeNode","#getModelId","node: Node","modelName: string | undefined"],"sources":["/opt/agent/work/649c11185a3798db/packages/ts/generator-plugin-signals/src/SignalProcessor.ts"],"sourcesContent":["import { dirname, extname } from 'node:path';\nimport type Plugin from '@vaadin/hilla-generator-core/Plugin.js';\nimport { simplifyFullyQualifiedName } from '@vaadin/hilla-generator-core/Schema.js';\nimport { traverse } from '@vaadin/hilla-generator-utils/ast.js';\nimport createSourceFile from '@vaadin/hilla-generator-utils/createSourceFile.js';\nimport DependencyManager from '@vaadin/hilla-generator-utils/dependencies/DependencyManager.js';\nimport PathManager from '@vaadin/hilla-generator-utils/dependencies/PathManager.js';\nimport ast, { createTransformer } from 'tsc-template';\nimport {\n factory,\n isTypeReferenceNode,\n isIdentifier,\n type ReturnStatement,\n type SourceFile,\n type TypeNode,\n type ParameterDeclaration,\n type BinaryExpression,\n type Node,\n SyntaxKind,\n isTypeNode,\n transform,\n isFunctionDeclaration,\n isImportDeclaration,\n isUnionTypeNode,\n type PropertyAccessExpression,\n} from 'typescript';\nimport { ARRAY_TYPES, COLLECTION_SIGNALS, GENERIC_SIGNALS } from './utils.js';\n\nconst HILLA_REACT_SIGNALS = '@vaadin/hilla-react-signals';\nconst HILLA_LIT_FORM = '@vaadin/hilla-lit-form';\n\nexport default class SignalProcessor {\n readonly #dependencyManager: DependencyManager;\n readonly #owner: Plugin;\n readonly #service: string;\n readonly #methods: Map<string, string>;\n readonly #sourceFile: SourceFile;\n\n constructor(service: string, methods: Map<string, string>, sourceFile: SourceFile, owner: Plugin) {\n this.#service = service;\n this.#methods = methods;\n this.#sourceFile = sourceFile;\n this.#owner = owner;\n this.#dependencyManager = new DependencyManager(new PathManager({ extension: '.js' }));\n this.#dependencyManager.imports.fromCode(this.#sourceFile);\n }\n\n process(): SourceFile {\n this.#owner.logger.debug(`Processing signals: ${this.#service}`);\n const { imports } = this.#dependencyManager;\n\n const [, connectClientId] = Iterator.from(imports.default).find(([path]) => path.includes('connect-client'))!;\n\n const initTypeId = imports.named.getIdentifier('@vaadin/hilla-frontend', 'EndpointRequestInit');\n let initTypeUsageCount = 0;\n\n const [file] = transform<SourceFile>(this.#sourceFile, [\n createTransformer((node) => {\n if (isFunctionDeclaration(node)) {\n // Check if the function is a signal method.\n if (node.name && this.#methods.has(node.name.text)) {\n // Get the signal class ID that was already imported in\n // TransferTypes plugin.\n const signalId = imports.named.getIdentifier(\n HILLA_REACT_SIGNALS,\n simplifyFullyQualifiedName(this.#methods.get(node.name.text)!),\n )!;\n\n // Remove the `init` parameter.\n const params = node.parameters.filter(\n (p) => !(p.type && isTypeReferenceNode(p.type) && p.type.typeName === initTypeId),\n );\n\n // Calculate the default value for the signal class.\n const { defaultValue, defaultValueParameter } = this.#createDefaultValue(signalId.text, node.type);\n\n // Remove the `async` modifier if present.\n const modifiers = node.modifiers?.filter((m) => m.kind !== SyntaxKind.AsyncKeyword);\n\n // Remove the `Promise` type if present.\n const type =\n node.type &&\n isTypeReferenceNode(node.type) &&\n isIdentifier(node.type.typeName) &&\n node.type.typeName.text === 'Promise'\n ? node.type.typeArguments?.[0]\n : node.type;\n\n // Get the variable parameter names to be passed to the signal\n // class.\n const paramNames = params\n .map((p) => p.name)\n .filter((n) => isIdentifier(n))\n .map((n) => n.text);\n\n // Create the signal class method body applying all the data we've\n // gathered.\n const result = ast`function dummy() { %{\n return new ${signalId}(${defaultValue}${defaultValue ? ',' : ''}{\n client: ${connectClientId},\n endpoint: '${this.#service}',\n method: '${node.name.text}'\n ${paramNames.length ? `, params: { ${paramNames.join('\\n')} }` : ''} });\n }% });`;\n\n // Update the function declaration accordingly.\n return factory.updateFunctionDeclaration(\n node,\n modifiers,\n node.asteriskToken,\n node.name,\n node.typeParameters,\n defaultValueParameter ? [...params, defaultValueParameter] : params,\n type,\n factory.createBlock([result.node as ReturnStatement]),\n );\n } else if (\n node.parameters.some((p) => p.type && isTypeReferenceNode(p.type) && p.type.typeName === initTypeId)\n ) {\n // Count the number of times the `init` parameter is used to check\n // if the type import is necessary to be removed.\n initTypeUsageCount += 1;\n }\n }\n\n return node;\n }),\n ]).transformed;\n\n // Remove the `EndpointRequestInit` import if it is not used anymore.\n if (initTypeUsageCount === 0) {\n imports.named.remove('@vaadin/hilla-frontend', 'EndpointRequestInit');\n }\n\n return createSourceFile(\n [\n ...this.#dependencyManager.imports.toCode(),\n ...file.statements.filter((statement) => !isImportDeclaration(statement)),\n ],\n file.fileName,\n );\n }\n\n #createDefaultValue(signalClass: string, returnType?: TypeNode) {\n const { imports } = this.#dependencyManager;\n\n // If the signal class is a collection signal, we have no default value to\n // generate.\n if (COLLECTION_SIGNALS.includes(signalClass)) {\n return {};\n }\n\n // If we have the NumberSignal class, we can use `0` as the default.\n if (!GENERIC_SIGNALS.includes(signalClass)) {\n return signalClass.startsWith('NumberSignal') ? { defaultValue: '0' } : {};\n }\n\n // Extract the generic argument of the signal class to get the default\n // value type.\n const type = traverse(returnType!, (node) =>\n isTypeReferenceNode(node) &&\n isIdentifier(node.typeName) &&\n GENERIC_SIGNALS.includes(node.typeName.text) &&\n node.typeArguments\n ? node.typeArguments[0]\n : undefined,\n )!;\n\n // Import the model class for the signal type (or omitting the import if\n // the type is a nullable one).\n const modelId = traverse(type, (node) => {\n // In case the generic argument of a signal class is nullable (e.g.\n // `Signal<Person | undefined>`), the default value will be `undefined`.\n if (isUnionTypeNode(node) && node.types.length > 1 && node.types[1].kind === SyntaxKind.UndefinedKeyword) {\n return SyntaxKind.UndefinedKeyword;\n }\n\n // Otherwise, we have to import the model class.\n return this.#getModelId(node);\n })!;\n\n // If we have a signal class with a generic argument (but not a collection),\n // we need to provide a possibility to set the default value.\n const optionsMethodTypeId =\n imports.named.getIdentifier(HILLA_REACT_SIGNALS, 'SignalMethodOptions') ??\n imports.named.add(HILLA_REACT_SIGNALS, 'SignalMethodOptions');\n\n return {\n defaultValue:\n modelId === SyntaxKind.UndefinedKeyword\n ? (ast`options?.defaultValue`.node as PropertyAccessExpression)\n : (ast`options?.defaultValue ?? ${modelId}.createEmptyValue()`.node as BinaryExpression),\n defaultValueParameter: ast`function dummy( %{ options?: ${optionsMethodTypeId}<${type}> }% ) {}`\n .node as ParameterDeclaration,\n };\n }\n\n #getModelId(node: Node) {\n const { imports } = this.#dependencyManager;\n\n if (isIdentifier(node)) {\n // In case the node is an array type defined as `Array<T>` or\n // `ReadonlyArray<T>`, we need to import the `ArrayModel` class.\n if (ARRAY_TYPES.includes(node.text)) {\n return (\n imports.named.getIdentifier(HILLA_LIT_FORM, 'ArrayModel') ?? imports.named.add(HILLA_LIT_FORM, 'ArrayModel')\n );\n }\n\n // Otherwise, we calculate and import the model class.\n const [path] = Iterator.from(imports.default).find(([, id]) => id === node) ?? [];\n\n if (!path) {\n throw new Error(`Model not found for ${node.text}`);\n }\n\n const modelName = `${node.text}Model`;\n const modelPath = `${dirname(path)}/${modelName}${extname(path)}`;\n\n return imports.default.getIdentifier(modelPath) ?? imports.default.add(modelPath, modelName);\n } else if (isTypeNode(node)) {\n // If the node is a primitive type, we will import the corresponding\n // model class from `@vaadin/hilla-lit-form`.\n let modelName: string | undefined;\n\n switch (node.kind) {\n case SyntaxKind.StringKeyword:\n modelName = 'StringModel';\n break;\n case SyntaxKind.NumberKeyword:\n modelName = 'NumberModel';\n break;\n case SyntaxKind.BooleanKeyword:\n modelName = 'BooleanModel';\n break;\n case SyntaxKind.ArrayType:\n modelName = 'ArrayModel';\n break;\n default:\n return undefined;\n }\n\n return imports.named.getIdentifier(HILLA_LIT_FORM, modelName) ?? imports.named.add(HILLA_LIT_FORM, modelName);\n }\n\n return undefined;\n }\n}\n"],"version":3}
|
|
1
|
+
{"mappings":"AAAA,SAAS,SAAS,0BAA2B;AAE7C,SAAS,0EAA2E;AACpF,SAAS,sDAAuD;AAChE,OAAO,yEAA0E;AACjF,OAAO,wFAAyF;AAChG,OAAO,4EAA6E;AACpF,OAAO,OAAO,uCAAwC;AACtD,SACE,SACA,qBACA,cAOA,YACA,YACA,WACA,uBACA,qBACA,mCAEkB;AACpB,SAAS,aAAa,oBAAoB,mCAAoC;AAE9E,MAAM,sBAAsB;AAC5B,MAAM,iBAAiB;AAEvB,eAAe,MAAM,gBAAgB;CACnC,AAASA;CACT,AAASC;CACT,AAASC;CACT,AAASC;CACT,AAASC;CAET,YAAYC,SAAiBC,SAA8BC,YAAwBC,OAAe;AAChG,OAAKN,WAAW;AAChB,OAAKC,WAAW;AAChB,OAAKC,cAAc;AAC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F,KAAK,WAAW;AACd,iBAAY;AACZ;IACF,QACE,QAAO;GACV;AAED,UAAO,QAAQ,MAAM,cAAc,gBAAgB,UAAU,IAAI,QAAQ,MAAM,IAAI,gBAAgB,UAAU;EAC9G;AAED,SAAO;CACR;AACF","names":["#dependencyManager","#owner","#service","#methods","#sourceFile","service: string","methods: Map<string, string>","sourceFile: SourceFile","owner: Plugin","#createDefaultValue","signalClass: string","returnType?: TypeNode","#getModelId","node: Node","modelName: string | undefined"],"sources":["/opt/agent/work/649c11185a3798db/packages/ts/generator-plugin-signals/src/SignalProcessor.ts"],"sourcesContent":["import { dirname, extname } from 'node:path';\nimport type Plugin from '@vaadin/hilla-generator-core/Plugin.js';\nimport { simplifyFullyQualifiedName } from '@vaadin/hilla-generator-core/Schema.js';\nimport { traverse } from '@vaadin/hilla-generator-utils/ast.js';\nimport createSourceFile from '@vaadin/hilla-generator-utils/createSourceFile.js';\nimport DependencyManager from '@vaadin/hilla-generator-utils/dependencies/DependencyManager.js';\nimport PathManager from '@vaadin/hilla-generator-utils/dependencies/PathManager.js';\nimport ast, { createTransformer } from 'tsc-template';\nimport {\n factory,\n isTypeReferenceNode,\n isIdentifier,\n type ReturnStatement,\n type SourceFile,\n type TypeNode,\n type ParameterDeclaration,\n type BinaryExpression,\n type Node,\n SyntaxKind,\n isTypeNode,\n transform,\n isFunctionDeclaration,\n isImportDeclaration,\n isUnionTypeNode,\n type PropertyAccessExpression,\n} from 'typescript';\nimport { ARRAY_TYPES, COLLECTION_SIGNALS, GENERIC_SIGNALS } from './utils.js';\n\nconst HILLA_REACT_SIGNALS = '@vaadin/hilla-react-signals';\nconst HILLA_LIT_FORM = '@vaadin/hilla-lit-form';\n\nexport default class SignalProcessor {\n readonly #dependencyManager: DependencyManager;\n readonly #owner: Plugin;\n readonly #service: string;\n readonly #methods: Map<string, string>;\n readonly #sourceFile: SourceFile;\n\n constructor(service: string, methods: Map<string, string>, sourceFile: SourceFile, owner: Plugin) {\n this.#service = service;\n this.#methods = methods;\n this.#sourceFile = sourceFile;\n this.#owner = owner;\n this.#dependencyManager = new DependencyManager(new PathManager({ extension: '.js' }));\n this.#dependencyManager.imports.fromCode(this.#sourceFile);\n }\n\n process(): SourceFile {\n this.#owner.logger.debug(`Processing signals: ${this.#service}`);\n const { imports } = this.#dependencyManager;\n\n const [, connectClientId] = Iterator.from(imports.default).find(([path]) => path.includes('connect-client'))!;\n\n const initTypeId = imports.named.getIdentifier('@vaadin/hilla-frontend', 'EndpointRequestInit');\n let initTypeUsageCount = 0;\n\n const [file] = transform(this.#sourceFile, [\n createTransformer((node) => {\n if (isFunctionDeclaration(node)) {\n // Check if the function is a signal method.\n if (node.name && this.#methods.has(node.name.text)) {\n // Get the signal class ID that was already imported in\n // TransferTypes plugin.\n const signalId = imports.named.getIdentifier(\n HILLA_REACT_SIGNALS,\n simplifyFullyQualifiedName(this.#methods.get(node.name.text)!),\n )!;\n\n // Remove the `init` parameter.\n const params = node.parameters.filter(\n (p) => !(p.type && isTypeReferenceNode(p.type) && p.type.typeName === initTypeId),\n );\n\n // Calculate the default value for the signal class.\n const { defaultValue, defaultValueParameter } = this.#createDefaultValue(signalId.text, node.type);\n\n // Remove the `async` modifier if present.\n const modifiers = node.modifiers?.filter((m) => m.kind !== SyntaxKind.AsyncKeyword);\n\n // Remove the `Promise` type if present.\n const type =\n node.type &&\n isTypeReferenceNode(node.type) &&\n isIdentifier(node.type.typeName) &&\n node.type.typeName.text === 'Promise'\n ? node.type.typeArguments?.[0]\n : node.type;\n\n // Get the variable parameter names to be passed to the signal\n // class.\n const paramNames = params\n .map((p) => p.name)\n .filter((n) => isIdentifier(n))\n .map((n) => n.text);\n\n // Create the signal class method body applying all the data we've\n // gathered.\n const result = ast`function dummy() { %{\n return new ${signalId}(${defaultValue}${defaultValue ? ',' : ''}{\n client: ${connectClientId},\n endpoint: '${this.#service}',\n method: '${node.name.text}'\n ${paramNames.length ? `, params: { ${paramNames.join('\\n')} }` : ''} });\n }% });`;\n\n // Update the function declaration accordingly.\n return factory.updateFunctionDeclaration(\n node,\n modifiers,\n node.asteriskToken,\n node.name,\n node.typeParameters,\n defaultValueParameter ? [...params, defaultValueParameter] : params,\n type,\n factory.createBlock([result.node as ReturnStatement]),\n );\n } else if (\n node.parameters.some((p) => p.type && isTypeReferenceNode(p.type) && p.type.typeName === initTypeId)\n ) {\n // Count the number of times the `init` parameter is used to check\n // if the type import is necessary to be removed.\n initTypeUsageCount += 1;\n }\n }\n\n return node;\n }),\n ]).transformed;\n\n // Remove the `EndpointRequestInit` import if it is not used anymore.\n if (initTypeUsageCount === 0) {\n imports.named.remove('@vaadin/hilla-frontend', 'EndpointRequestInit');\n }\n\n return createSourceFile(\n [\n ...this.#dependencyManager.imports.toCode(),\n ...file.statements.filter((statement) => !isImportDeclaration(statement)),\n ],\n file.fileName,\n );\n }\n\n #createDefaultValue(signalClass: string, returnType?: TypeNode) {\n const { imports } = this.#dependencyManager;\n\n // If the signal class is a collection signal, we have no default value to\n // generate.\n if (COLLECTION_SIGNALS.includes(signalClass)) {\n return {};\n }\n\n // If we have the NumberSignal class, we can use `0` as the default.\n if (!GENERIC_SIGNALS.includes(signalClass)) {\n return signalClass.startsWith('NumberSignal') ? { defaultValue: '0' } : {};\n }\n\n // Extract the generic argument of the signal class to get the default\n // value type.\n const type = traverse(returnType!, (node) =>\n isTypeReferenceNode(node) &&\n isIdentifier(node.typeName) &&\n GENERIC_SIGNALS.includes(node.typeName.text) &&\n node.typeArguments\n ? node.typeArguments[0]\n : undefined,\n )!;\n\n // Import the model class for the signal type (or omitting the import if\n // the type is a nullable one).\n const modelId = traverse(type, (node) => {\n // In case the generic argument of a signal class is nullable (e.g.\n // `Signal<Person | undefined>`), the default value will be `undefined`.\n if (isUnionTypeNode(node) && node.types.length > 1 && node.types[1].kind === SyntaxKind.UndefinedKeyword) {\n return SyntaxKind.UndefinedKeyword;\n }\n\n // Otherwise, we have to import the model class.\n return this.#getModelId(node);\n })!;\n\n // If we have a signal class with a generic argument (but not a collection),\n // we need to provide a possibility to set the default value.\n const optionsMethodTypeId =\n imports.named.getIdentifier(HILLA_REACT_SIGNALS, 'SignalMethodOptions') ??\n imports.named.add(HILLA_REACT_SIGNALS, 'SignalMethodOptions');\n\n return {\n defaultValue:\n modelId === SyntaxKind.UndefinedKeyword\n ? (ast`options?.defaultValue`.node as PropertyAccessExpression)\n : (ast`options?.defaultValue ?? ${modelId}.createEmptyValue()`.node as BinaryExpression),\n defaultValueParameter: ast`function dummy( %{ options?: ${optionsMethodTypeId}<${type}> }% ) {}`\n .node as ParameterDeclaration,\n };\n }\n\n #getModelId(node: Node) {\n const { imports } = this.#dependencyManager;\n\n if (isIdentifier(node)) {\n // In case the node is an array type defined as `Array<T>` or\n // `ReadonlyArray<T>`, we need to import the `ArrayModel` class.\n if (ARRAY_TYPES.includes(node.text)) {\n return (\n imports.named.getIdentifier(HILLA_LIT_FORM, 'ArrayModel') ?? imports.named.add(HILLA_LIT_FORM, 'ArrayModel')\n );\n }\n\n // Otherwise, we calculate and import the model class.\n const [path] = Iterator.from(imports.default).find(([, id]) => id === node) ?? [];\n\n if (!path) {\n throw new Error(`Model not found for ${node.text}`);\n }\n\n const modelName = `${node.text}Model`;\n const modelPath = `${dirname(path)}/${modelName}${extname(path)}`;\n\n return imports.default.getIdentifier(modelPath) ?? imports.default.add(modelPath, modelName);\n } else if (isTypeNode(node)) {\n // If the node is a primitive type, we will import the corresponding\n // model class from `@vaadin/hilla-lit-form`.\n let modelName: string | undefined;\n\n switch (node.kind) {\n case SyntaxKind.StringKeyword:\n modelName = 'StringModel';\n break;\n case SyntaxKind.NumberKeyword:\n modelName = 'NumberModel';\n break;\n case SyntaxKind.BooleanKeyword:\n modelName = 'BooleanModel';\n break;\n case SyntaxKind.ArrayType:\n modelName = 'ArrayModel';\n break;\n default:\n return undefined;\n }\n\n return imports.named.getIdentifier(HILLA_LIT_FORM, modelName) ?? imports.named.add(HILLA_LIT_FORM, modelName);\n }\n\n return undefined;\n }\n}\n"],"version":3}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vaadin/hilla-generator-plugin-signals",
|
|
3
|
-
"version": "24.9.
|
|
3
|
+
"version": "24.9.14",
|
|
4
4
|
"description": "A Hilla TypeScript Generator plugin to add Shared Signals support",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -49,8 +49,8 @@
|
|
|
49
49
|
"access": "public"
|
|
50
50
|
},
|
|
51
51
|
"dependencies": {
|
|
52
|
-
"@vaadin/hilla-generator-core": "24.9.
|
|
53
|
-
"@vaadin/hilla-generator-utils": "24.9.
|
|
52
|
+
"@vaadin/hilla-generator-core": "24.9.14",
|
|
53
|
+
"@vaadin/hilla-generator-utils": "24.9.14",
|
|
54
54
|
"fast-deep-equal": "3.1.3",
|
|
55
55
|
"iterator-helpers-polyfill": "3.0.1",
|
|
56
56
|
"openapi-types": "12.1.3",
|
|
@@ -58,8 +58,8 @@
|
|
|
58
58
|
"typescript": "5.8.3"
|
|
59
59
|
},
|
|
60
60
|
"devDependencies": {
|
|
61
|
-
"@vaadin/hilla-generator-plugin-backbone": "24.9.
|
|
62
|
-
"@vaadin/hilla-generator-plugin-client": "24.9.
|
|
63
|
-
"@vaadin/hilla-generator-plugin-transfertypes": "24.9.
|
|
61
|
+
"@vaadin/hilla-generator-plugin-backbone": "24.9.14",
|
|
62
|
+
"@vaadin/hilla-generator-plugin-client": "24.9.14",
|
|
63
|
+
"@vaadin/hilla-generator-plugin-transfertypes": "24.9.14"
|
|
64
64
|
}
|
|
65
65
|
}
|