@htmlplus/element 0.4.3 → 0.4.5

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.
Files changed (33) hide show
  1. package/compiler/plugins/{external.d.ts → assets.d.ts} +3 -3
  2. package/compiler/plugins/assets.js +26 -0
  3. package/compiler/plugins/customElement.js +66 -34
  4. package/compiler/plugins/customElementReact/customElementReact.js +10 -9
  5. package/compiler/plugins/customElementReact/templates/src/components/{{fileName}}.ts.hbs +1 -1
  6. package/compiler/plugins/document.d.ts +2 -4
  7. package/compiler/plugins/document.js +219 -226
  8. package/compiler/plugins/extract.js +7 -6
  9. package/compiler/plugins/index.d.ts +2 -1
  10. package/compiler/plugins/index.js +2 -1
  11. package/compiler/plugins/parse.js +1 -4
  12. package/compiler/plugins/read.js +4 -8
  13. package/compiler/plugins/style.d.ts +1 -3
  14. package/compiler/plugins/style.js +19 -21
  15. package/compiler/plugins/validate.js +1 -4
  16. package/compiler/plugins/visualStudioCode.d.ts +8 -0
  17. package/compiler/plugins/visualStudioCode.js +28 -0
  18. package/compiler/plugins/webTypes.d.ts +11 -0
  19. package/compiler/plugins/webTypes.js +79 -0
  20. package/compiler/utils/addDependency.d.ts +2 -0
  21. package/compiler/utils/addDependency.js +41 -0
  22. package/compiler/utils/getType.js +1 -1
  23. package/compiler/utils/index.d.ts +1 -0
  24. package/compiler/utils/index.js +1 -0
  25. package/compiler/utils/isDirectoryEmpty.js +1 -1
  26. package/compiler/utils/renderTemplate.js +2 -4
  27. package/constants/index.d.ts +1 -0
  28. package/constants/index.js +2 -0
  29. package/package.json +1 -2
  30. package/types/context.d.ts +1 -0
  31. package/compiler/plugins/external.js +0 -25
  32. package/compiler/plugins/vscode.d.ts +0 -11
  33. package/compiler/plugins/vscode.js +0 -83
@@ -1,9 +1,9 @@
1
1
  import { Context } from '../../types/index.js';
2
- export declare type ExternalOptions = {
3
- source?: (context: Context) => string;
2
+ export declare type AssetsOptions = {
4
3
  destination: (context: Context) => string;
4
+ source?: (context: Context) => string;
5
5
  };
6
- export declare const external: (options: ExternalOptions) => {
6
+ export declare const assets: (options: AssetsOptions) => {
7
7
  name: string;
8
8
  next: (context: Context) => void;
9
9
  };
@@ -0,0 +1,26 @@
1
+ import fs from 'fs-extra';
2
+ import path from 'path';
3
+ const defaults = {
4
+ destination(context) {
5
+ return `assets/${context.fileName}`;
6
+ },
7
+ source(context) {
8
+ return path.join(context.directoryPath, 'assets');
9
+ }
10
+ };
11
+ export const assets = (options) => {
12
+ const name = 'assets';
13
+ options = Object.assign({}, defaults, options);
14
+ const next = (context) => {
15
+ var _a, _b;
16
+ const destination = (_a = options.destination) === null || _a === void 0 ? void 0 : _a.call(options, context);
17
+ const source = (_b = options.source) === null || _b === void 0 ? void 0 : _b.call(options, context);
18
+ if (!source)
19
+ return;
20
+ if (!fs.existsSync(source))
21
+ return;
22
+ fs.copySync(source, destination);
23
+ context.assets = source;
24
+ };
25
+ return { name, next };
26
+ };
@@ -1,6 +1,7 @@
1
1
  import t from '@babel/types';
2
+ import { pascalCase } from 'change-case';
2
3
  import * as CONSTANTS from '../../constants/index.js';
3
- import { print, visitor } from '../utils/index.js';
4
+ import { addDependency, print, visitor } from '../utils/index.js';
4
5
  const defaults = {
5
6
  typings: true
6
7
  };
@@ -10,26 +11,43 @@ export const customElement = (options) => {
10
11
  options = Object.assign({}, defaults, options);
11
12
  const next = (context) => {
12
13
  const ast = t.cloneNode(context.fileAST, true);
14
+ // attach style mapper for 'style' attribute
15
+ visitor(ast, {
16
+ JSXAttribute(path) {
17
+ const { name, value } = path.node;
18
+ if (name.name != 'style')
19
+ return;
20
+ if (!value)
21
+ return;
22
+ if (value.type != 'JSXExpressionContainer')
23
+ return;
24
+ const local = addDependency(ast, CONSTANTS.PACKAGE_NAME, CONSTANTS.UTILS_STYLE_MAP);
25
+ path.replaceWith(t.jsxAttribute(t.jsxIdentifier('style'), t.jsxExpressionContainer(t.callExpression(t.identifier(local), [value.expression]))));
26
+ path.skip();
27
+ }
28
+ });
13
29
  // TODO
14
30
  visitor(ast, {
15
31
  ClassDeclaration(path) {
16
- if (path.node.id.name != context.className)
32
+ const { body, id } = path.node;
33
+ if (id.name != context.className)
17
34
  return;
18
- path.node.body.body.unshift(t.classProperty(t.identifier('uhtml')));
35
+ body.body.unshift(t.classProperty(t.identifier('uhtml')));
19
36
  }
20
37
  });
21
- // replace className
38
+ // replace 'className' attribute to 'class'
22
39
  visitor(ast, {
23
40
  JSXAttribute(path) {
24
- if (path.node.name.name != 'className')
41
+ const { name, value } = path.node;
42
+ if (name.name != 'className')
25
43
  return;
26
44
  const hasClass = path.parentPath.node.attributes.some((attribute) => attribute.name.name == 'class');
27
45
  if (hasClass)
28
46
  return path.remove();
29
- path.replaceWith(t.jsxAttribute(t.jsxIdentifier('class'), path.node.value));
47
+ path.replaceWith(t.jsxAttribute(t.jsxIdentifier('class'), value));
30
48
  }
31
49
  });
32
- // jsx to uhtml syntax
50
+ // TODO: convert 'jsx' to 'uhtml' syntax
33
51
  visitor(ast, {
34
52
  JSXAttribute: {
35
53
  exit(path) {
@@ -80,9 +98,10 @@ export const customElement = (options) => {
80
98
  // attach members
81
99
  visitor(ast, {
82
100
  ClassDeclaration(path) {
83
- if (path.node.id.name != context.className)
101
+ const { body, id } = path.node;
102
+ if (id.name != context.className)
84
103
  return;
85
- path.node.body.body.unshift(t.classProperty(t.identifier(CONSTANTS.STATIC_MEMBERS), t.objectExpression([
104
+ body.body.unshift(t.classProperty(t.identifier(CONSTANTS.STATIC_MEMBERS), t.objectExpression([
86
105
  ...context.classProperties.map((property) => {
87
106
  var _a, _b;
88
107
  const type = (_b = (_a = property.typeAnnotation) === null || _a === void 0 ? void 0 : _a.typeAnnotation) === null || _b === void 0 ? void 0 : _b.type;
@@ -116,13 +135,33 @@ export const customElement = (options) => {
116
135
  if (options === null || options === void 0 ? void 0 : options.typings) {
117
136
  visitor(ast, {
118
137
  Program(path) {
119
- path.node.body.push(Object.assign(t.tsModuleDeclaration(t.identifier('global'), t.tsModuleBlock([
138
+ const { body } = path.node;
139
+ body.push(t.exportNamedDeclaration(t.tsInterfaceDeclaration(
140
+ // TODO
141
+ t.identifier(context.componentClassName + 'JSX'), null, [], t.tsInterfaceBody([
142
+ ...context.classProperties.map((property) => Object.assign(t.tSPropertySignature(property.key, property.typeAnnotation), {
143
+ optional: property.optional,
144
+ leadingComments: t.cloneNode(property, true).leadingComments
145
+ })),
146
+ ...context.classEvents.map((event) => {
147
+ var _a, _b;
148
+ return Object.assign(t.tSPropertySignature(t.identifier('on' + pascalCase(event.key['name'])), t.tsTypeAnnotation(t.tsFunctionType(undefined, [
149
+ Object.assign({}, t.identifier('event'), {
150
+ typeAnnotation: t.tsTypeAnnotation(t.tsTypeReference(t.identifier('CustomEvent'), (_b = (_a = event.typeAnnotation) === null || _a === void 0 ? void 0 : _a['typeAnnotation']) === null || _b === void 0 ? void 0 : _b.typeParameters))
151
+ })
152
+ ], t.tsTypeAnnotation(t.tsVoidKeyword())))), {
153
+ optional: true,
154
+ leadingComments: t.cloneNode(event, true).leadingComments
155
+ });
156
+ })
157
+ ]))));
158
+ body.push(Object.assign(t.tsModuleDeclaration(t.identifier('global'), t.tsModuleBlock([
120
159
  t.tsInterfaceDeclaration(t.identifier(context.componentInterfaceName), null, [
121
160
  t.tSExpressionWithTypeArguments(t.identifier('HTMLElement')) // TODO
122
161
  ], t.tsInterfaceBody([
123
162
  ...context.classProperties.map((property) => Object.assign(t.tSPropertySignature(property.key, property.typeAnnotation), {
124
163
  optional: property.optional,
125
- leadingComments: property.leadingComments
164
+ leadingComments: t.cloneNode(property, true).leadingComments
126
165
  }))
127
166
  ])),
128
167
  t.variableDeclaration('var', [
@@ -135,37 +174,30 @@ export const customElement = (options) => {
135
174
  ]),
136
175
  t.tsInterfaceDeclaration(t.identifier('HTMLElementTagNameMap'), null, [], t.tsInterfaceBody([
137
176
  t.tSPropertySignature(t.stringLiteral(context.componentTag), t.tSTypeAnnotation(t.tSIntersectionType([t.tSTypeReference(t.identifier(context.componentInterfaceName))])))
177
+ ])),
178
+ t.tSModuleDeclaration(t.identifier('JSX'), t.tSModuleBlock([
179
+ t.tSInterfaceDeclaration(t.identifier('IntrinsicElements'), undefined, undefined, t.tSInterfaceBody([
180
+ t.tSPropertySignature(t.stringLiteral(context.componentTag), t.tSTypeAnnotation(t.tSIntersectionType([
181
+ t.tSTypeReference(t.identifier(context.componentClassName + 'JSX')),
182
+ t.tSTypeLiteral([
183
+ t.tSIndexSignature([
184
+ Object.assign({}, t.identifier('key'), {
185
+ typeAnnotation: t.tSTypeAnnotation(t.tSStringKeyword())
186
+ })
187
+ ], t.tSTypeAnnotation(t.tSAnyKeyword()))
188
+ ])
189
+ ])))
190
+ ]))
138
191
  ]))
139
192
  ])), {
140
193
  declare: true,
141
194
  global: true
142
195
  }));
143
- path.node.body.push(t.exportNamedDeclaration(t.tsInterfaceDeclaration(
144
- // TODO
145
- t.identifier(context.componentClassName + 'JSX'), null, [], t.tsInterfaceBody([
146
- ...context.classProperties.map((property) => Object.assign(t.tSPropertySignature(property.key, property.typeAnnotation), {
147
- optional: property.optional,
148
- leadingComments: property.leadingComments
149
- })),
150
- ...context.classEvents.map((event) => {
151
- var _a, _b;
152
- return Object.assign(t.tSPropertySignature(event.key, t.tsTypeAnnotation(t.tsFunctionType(undefined, [
153
- Object.assign({}, t.identifier('event'), {
154
- typeAnnotation: t.tsTypeAnnotation(t.tsTypeReference(t.identifier('CustomEvent'), (_b = (_a = event.typeAnnotation) === null || _a === void 0 ? void 0 : _a['typeAnnotation']) === null || _b === void 0 ? void 0 : _b.typeParameters))
155
- })
156
- ], t.tsTypeAnnotation(t.tsVoidKeyword())))), {
157
- optional: true,
158
- leadingComments: event.leadingComments
159
- });
160
- })
161
- ]))));
162
196
  }
163
197
  });
164
198
  }
199
+ // TODO
165
200
  context.script = print(ast);
166
201
  };
167
- return {
168
- name,
169
- next
170
- };
202
+ return { name, next };
171
203
  };
@@ -1,8 +1,11 @@
1
+ import { pascalCase } from 'change-case';
1
2
  import { __dirname, isDirectoryEmpty, renderTemplate } from '../../utils/index.js';
2
3
  const defaults = {
3
4
  compact: false,
4
5
  destination: '',
5
- eventName: undefined,
6
+ eventName(eventName) {
7
+ return eventName;
8
+ },
6
9
  importerComponent(context) {
7
10
  return `YOUR_CORE_PACKAGE_NAME#${context.componentClassName}`;
8
11
  },
@@ -12,8 +15,8 @@ const defaults = {
12
15
  };
13
16
  export const customElementReact = (options) => {
14
17
  const name = 'customElementReact';
18
+ options = Object.assign({}, defaults, options);
15
19
  const finish = (global) => {
16
- options = Object.assign(Object.assign({}, defaults), options);
17
20
  // TODO
18
21
  const globalNew = {
19
22
  contexts: global.contexts.reduce((previous, current) => (Object.assign(Object.assign({}, previous), { [current.filePath]: current })), {}),
@@ -36,9 +39,10 @@ export const customElementReact = (options) => {
36
39
  };
37
40
  };
38
41
  const classEvents = context.classEvents.map((classEvent) => {
39
- var _a;
40
- const name = ((_a = options.eventName) === null || _a === void 0 ? void 0 : _a.call(options, classEvent.key.name)) || classEvent.key.name;
41
- return Object.assign(Object.assign({}, classEvent), { converted: 'on' + name.charAt(0).toUpperCase() + name.slice(1) });
42
+ const from = 'on' + pascalCase(classEvent.key.name);
43
+ const to = options.eventName(from);
44
+ return Object.assign(Object.assign({}, classEvent), { from,
45
+ to });
42
46
  });
43
47
  const fileName = context.fileName;
44
48
  const importerComponent = parse(options.importerComponent(context));
@@ -122,8 +126,5 @@ export const customElementReact = (options) => {
122
126
  renderTemplate(patterns, options.destination, config)(globalNew);
123
127
  }
124
128
  };
125
- return {
126
- name,
127
- finish
128
- };
129
+ return { name, finish };
129
130
  };
@@ -11,7 +11,7 @@ type Rename<T, R extends { [K in keyof R]: K extends keyof T ? PropertyKey : "Er
11
11
 
12
12
  type Renamed = Rename<{{importerComponentType.variable}}, {
13
13
  {{#each classEvents}}
14
- {{key.name}}: '{{converted}}',
14
+ {{from}}: '{{to}}',
15
15
  {{/each}}
16
16
  }>
17
17
 
@@ -1,10 +1,8 @@
1
- import { Context } from '../../types/index.js';
1
+ import { Global } from '../../types/index.js';
2
2
  export interface DocumentOptions {
3
3
  destination: string;
4
4
  }
5
5
  export declare const document: (options: DocumentOptions) => {
6
6
  name: string;
7
- start: (global: any) => void;
8
- next: (context: Context, global: any) => void;
9
- finish: (global: any) => void;
7
+ finish: (global: Global) => void;
10
8
  };