@htmlplus/element 0.4.4 → 0.4.7

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 (72) hide show
  1. package/bundler/index.d.ts +2 -0
  2. package/bundler/index.js +2 -0
  3. package/bundler/rollup.d.ts +7 -0
  4. package/bundler/rollup.js +21 -0
  5. package/bundler/vite.d.ts +7 -0
  6. package/bundler/vite.js +25 -0
  7. package/client/decorators/attributes.d.ts +1 -1
  8. package/client/decorators/element.d.ts +1 -1
  9. package/client/decorators/event.d.ts +1 -1
  10. package/client/decorators/listen.d.ts +1 -1
  11. package/client/decorators/method.d.ts +1 -1
  12. package/client/decorators/property.d.ts +1 -1
  13. package/client/decorators/state.d.ts +1 -1
  14. package/client/decorators/watch.d.ts +1 -1
  15. package/client/helpers/direction.d.ts +1 -1
  16. package/client/helpers/isRTL.d.ts +1 -1
  17. package/client/helpers/slots.d.ts +1 -1
  18. package/client/utils/getMemberType.d.ts +1 -1
  19. package/client/utils/getMembersKey.d.ts +1 -1
  20. package/client/utils/getStyles.d.ts +1 -1
  21. package/client/utils/host.d.ts +1 -1
  22. package/client/utils/request.d.ts +1 -1
  23. package/client/utils/request.js +2 -2
  24. package/compiler/compiler.d.ts +1 -1
  25. package/compiler/plugins/{external.d.ts → assets.d.ts} +4 -4
  26. package/compiler/plugins/assets.js +26 -0
  27. package/compiler/plugins/customElement.d.ts +1 -1
  28. package/compiler/plugins/customElement.js +32 -15
  29. package/compiler/plugins/customElementReact/customElementReact.d.ts +1 -1
  30. package/compiler/plugins/customElementReact/customElementReact.js +2 -5
  31. package/compiler/plugins/document.d.ts +2 -4
  32. package/compiler/plugins/document.js +219 -226
  33. package/compiler/plugins/extract.d.ts +1 -1
  34. package/compiler/plugins/extract.js +7 -6
  35. package/compiler/plugins/finish.d.ts +4 -0
  36. package/compiler/plugins/finish.js +7 -0
  37. package/compiler/plugins/index.d.ts +3 -1
  38. package/compiler/plugins/index.js +3 -1
  39. package/compiler/plugins/parse.d.ts +1 -1
  40. package/compiler/plugins/parse.js +1 -4
  41. package/compiler/plugins/read.d.ts +1 -1
  42. package/compiler/plugins/read.js +4 -8
  43. package/compiler/plugins/start.d.ts +4 -0
  44. package/compiler/plugins/start.js +7 -0
  45. package/compiler/plugins/style.d.ts +2 -4
  46. package/compiler/plugins/style.js +19 -21
  47. package/compiler/plugins/validate.d.ts +1 -1
  48. package/compiler/plugins/validate.js +1 -4
  49. package/compiler/plugins/visualStudioCode.d.ts +8 -0
  50. package/compiler/plugins/visualStudioCode.js +28 -0
  51. package/compiler/plugins/webTypes.d.ts +2 -2
  52. package/compiler/plugins/webTypes.js +6 -9
  53. package/compiler/utils/addDependency.d.ts +4 -0
  54. package/compiler/utils/addDependency.js +51 -0
  55. package/compiler/utils/getType.js +1 -1
  56. package/compiler/utils/index.d.ts +2 -0
  57. package/compiler/utils/index.js +2 -0
  58. package/compiler/utils/isDirectoryEmpty.js +1 -1
  59. package/compiler/utils/removeUnusedImport.d.ts +1 -0
  60. package/compiler/utils/removeUnusedImport.js +49 -0
  61. package/compiler/utils/renderTemplate.js +2 -4
  62. package/constants/index.d.ts +1 -0
  63. package/constants/index.js +2 -0
  64. package/package.json +5 -12
  65. package/types/context.d.ts +1 -0
  66. package/types/global.d.ts +1 -1
  67. package/types/index.d.ts +4 -4
  68. package/types/index.js +4 -4
  69. package/types/plugin.d.ts +1 -1
  70. package/compiler/plugins/external.js +0 -25
  71. package/compiler/plugins/vscode.d.ts +0 -11
  72. package/compiler/plugins/vscode.js +0 -83
@@ -0,0 +1,2 @@
1
+ export * from './rollup.js';
2
+ export * from './vite.js';
@@ -0,0 +1,2 @@
1
+ export * from './rollup.js';
2
+ export * from './vite.js';
@@ -0,0 +1,7 @@
1
+ import { Plugin } from '../types';
2
+ export declare const rollup: (...plugins: Array<Plugin>) => {
3
+ name: string;
4
+ buildStart(): Promise<void>;
5
+ load(id: any): Promise<string | undefined>;
6
+ buildEnd(): Promise<void>;
7
+ };
@@ -0,0 +1,21 @@
1
+ import compiler from '../compiler/index.js';
2
+ export const rollup = (...plugins) => {
3
+ const { start, next, finish } = compiler(...plugins);
4
+ return {
5
+ name: 'htmlplus',
6
+ async buildStart() {
7
+ await start();
8
+ },
9
+ async load(id) {
10
+ if (!id.endsWith('.tsx'))
11
+ return;
12
+ const { isInvalid, script } = await next(id);
13
+ if (isInvalid)
14
+ return;
15
+ return script;
16
+ },
17
+ async buildEnd() {
18
+ await finish();
19
+ }
20
+ };
21
+ };
@@ -0,0 +1,7 @@
1
+ import { Plugin } from '../types';
2
+ export declare const vite: (...plugins: Array<Plugin>) => {
3
+ name: string;
4
+ buildStart(): Promise<void>;
5
+ load(id: any): Promise<string | undefined>;
6
+ buildEnd(): Promise<void>;
7
+ };
@@ -0,0 +1,25 @@
1
+ import path from 'path';
2
+ import compiler from '../compiler/index.js';
3
+ export const vite = (...plugins) => {
4
+ const { start, next, finish } = compiler(...plugins);
5
+ return {
6
+ name: 'htmlplus',
7
+ async buildStart() {
8
+ await start();
9
+ },
10
+ async load(id) {
11
+ if (!id.endsWith('.tsx'))
12
+ return;
13
+ let { isInvalid, script, stylePath } = await next(id);
14
+ if (isInvalid)
15
+ return;
16
+ if (script && stylePath) {
17
+ script = script.replace(path.basename(stylePath), `${path.basename(stylePath)}?inline`);
18
+ }
19
+ return script;
20
+ },
21
+ async buildEnd() {
22
+ await finish();
23
+ }
24
+ };
25
+ };
@@ -1,2 +1,2 @@
1
- import { PlusElement } from '../../types/index.js';
1
+ import { PlusElement } from '../../types';
2
2
  export declare function Attributes(): (target: PlusElement, propertyKey: PropertyKey) => void;
@@ -1,2 +1,2 @@
1
- import { PlusElement } from '../../types/index.js';
1
+ import { PlusElement } from '../../types';
2
2
  export declare function Element(tag?: string): (constructor: PlusElement) => void;
@@ -1,4 +1,4 @@
1
- import { PlusElement } from '../../types/index.js';
1
+ import { PlusElement } from '../../types';
2
2
  export declare type EventEmitter<T = any> = (data?: T) => CustomEvent<T>;
3
3
  export interface EventOptions {
4
4
  /**
@@ -1,4 +1,4 @@
1
- import { PlusElement } from '../../types/index.js';
1
+ import { PlusElement } from '../../types';
2
2
  export interface ListenOptions {
3
3
  target?: 'host' | 'body' | 'document' | 'window';
4
4
  once?: boolean;
@@ -1,2 +1,2 @@
1
- import { PlusElement } from '../../types/index.js';
1
+ import { PlusElement } from '../../types';
2
2
  export declare function Method(): (target: PlusElement, propertyKey: PropertyKey) => void;
@@ -1,4 +1,4 @@
1
- import { PlusElement } from '../../types/index.js';
1
+ import { PlusElement } from '../../types';
2
2
  export interface PropertyOptions {
3
3
  /**
4
4
  * Whether property value is reflected back to the associated attribute. default is `false`.
@@ -1,2 +1,2 @@
1
- import { PlusElement } from '../../types/index.js';
1
+ import { PlusElement } from '../../types';
2
2
  export declare function State(): (target: PlusElement, propertyKey: PropertyKey) => void;
@@ -1,4 +1,4 @@
1
- import { PlusElement } from '../../types/index.js';
1
+ import { PlusElement } from '../../types';
2
2
  /**
3
3
  * Monitors properties and states to catch changes.
4
4
  * The decorated method will be invoked after any
@@ -1,3 +1,3 @@
1
- import { PlusElement } from '../../types/index.js';
1
+ import { PlusElement } from '../../types';
2
2
  export declare type Direction = 'ltr' | 'rtl';
3
3
  export declare const direction: (target: PlusElement) => Direction;
@@ -1,2 +1,2 @@
1
- import { PlusElement } from '../../types/index.js';
1
+ import { PlusElement } from '../../types';
2
2
  export declare const isRTL: (target: PlusElement) => boolean;
@@ -1,4 +1,4 @@
1
- import { PlusElement } from '../../types/index.js';
1
+ import { PlusElement } from '../../types';
2
2
  declare type Slots = {
3
3
  [key: string]: boolean;
4
4
  };
@@ -1,2 +1,2 @@
1
- import { PlusElement } from '../../types/index.js';
1
+ import { PlusElement } from '../../types';
2
2
  export declare const getMemberType: (target: PlusElement, key: string) => string;
@@ -1,2 +1,2 @@
1
- import { PlusElement } from '../../types/index.js';
1
+ import { PlusElement } from '../../types';
2
2
  export declare const getMembersKey: (target: PlusElement) => string[];
@@ -1,2 +1,2 @@
1
- import { PlusElement } from '../../types/index.js';
1
+ import { PlusElement } from '../../types';
2
2
  export declare const getStyles: (target: PlusElement) => string | undefined;
@@ -1,2 +1,2 @@
1
- import { PlusElement } from '../../types/index.js';
1
+ import { PlusElement } from '../../types';
2
2
  export declare const host: (target: PlusElement) => HTMLElement;
@@ -1,4 +1,4 @@
1
- import { PlusElement } from '../../types/index.js';
1
+ import { PlusElement } from '../../types';
2
2
  export declare const request: (target: PlusElement, state?: {
3
3
  [key: string]: [any, any];
4
4
  } | undefined) => Promise<boolean>;
@@ -1,6 +1,6 @@
1
1
  import * as CONSTANTS from '../../constants/index.js';
2
- import { call } from '../utils/call';
3
- import { task } from '../utils/task';
2
+ import { call } from '../utils/call.js';
3
+ import { task } from '../utils/task.js';
4
4
  import { html, render } from '../vendor/uhtml.js';
5
5
  import { getStyles } from './getStyles.js';
6
6
  import { host } from './host.js';
@@ -1,4 +1,4 @@
1
- import { Context, Plugin } from '../types/index.js';
1
+ import { Context, Plugin } from '../types';
2
2
  declare const _default: (...plugins: Array<Plugin>) => {
3
3
  start: () => Promise<void>;
4
4
  next: (filePath: string) => Promise<Context>;
@@ -1,9 +1,9 @@
1
- import { Context } from '../../types/index.js';
2
- export declare type ExternalOptions = {
3
- source?: (context: Context) => string;
1
+ import { Context } from '../../types';
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,4 +1,4 @@
1
- import { Context } from '../../types/index.js';
1
+ import { Context } from '../../types';
2
2
  export interface CustomElementOptions {
3
3
  typings?: boolean;
4
4
  }
@@ -1,7 +1,7 @@
1
1
  import t from '@babel/types';
2
2
  import { pascalCase } from 'change-case';
3
3
  import * as CONSTANTS from '../../constants/index.js';
4
- import { print, visitor } from '../utils/index.js';
4
+ import { addDependency, print, visitor } from '../utils/index.js';
5
5
  const defaults = {
6
6
  typings: true
7
7
  };
@@ -11,26 +11,43 @@ export const customElement = (options) => {
11
11
  options = Object.assign({}, defaults, options);
12
12
  const next = (context) => {
13
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(path, CONSTANTS.PACKAGE_NAME, CONSTANTS.UTILS_STYLE_MAP, 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
+ });
14
29
  // TODO
15
30
  visitor(ast, {
16
31
  ClassDeclaration(path) {
17
- if (path.node.id.name != context.className)
32
+ const { body, id } = path.node;
33
+ if (id.name != context.className)
18
34
  return;
19
- path.node.body.body.unshift(t.classProperty(t.identifier('uhtml')));
35
+ body.body.unshift(t.classProperty(t.identifier('uhtml')));
20
36
  }
21
37
  });
22
- // replace className
38
+ // replace 'className' attribute to 'class'
23
39
  visitor(ast, {
24
40
  JSXAttribute(path) {
25
- if (path.node.name.name != 'className')
41
+ const { name, value } = path.node;
42
+ if (name.name != 'className')
26
43
  return;
27
44
  const hasClass = path.parentPath.node.attributes.some((attribute) => attribute.name.name == 'class');
28
45
  if (hasClass)
29
46
  return path.remove();
30
- path.replaceWith(t.jsxAttribute(t.jsxIdentifier('class'), path.node.value));
47
+ path.replaceWith(t.jsxAttribute(t.jsxIdentifier('class'), value));
31
48
  }
32
49
  });
33
- // jsx to uhtml syntax
50
+ // TODO: convert 'jsx' to 'uhtml' syntax
34
51
  visitor(ast, {
35
52
  JSXAttribute: {
36
53
  exit(path) {
@@ -81,9 +98,10 @@ export const customElement = (options) => {
81
98
  // attach members
82
99
  visitor(ast, {
83
100
  ClassDeclaration(path) {
84
- if (path.node.id.name != context.className)
101
+ const { body, id } = path.node;
102
+ if (id.name != context.className)
85
103
  return;
86
- 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([
87
105
  ...context.classProperties.map((property) => {
88
106
  var _a, _b;
89
107
  const type = (_b = (_a = property.typeAnnotation) === null || _a === void 0 ? void 0 : _a.typeAnnotation) === null || _b === void 0 ? void 0 : _b.type;
@@ -117,7 +135,8 @@ export const customElement = (options) => {
117
135
  if (options === null || options === void 0 ? void 0 : options.typings) {
118
136
  visitor(ast, {
119
137
  Program(path) {
120
- path.node.body.push(t.exportNamedDeclaration(t.tsInterfaceDeclaration(
138
+ const { body } = path.node;
139
+ body.push(t.exportNamedDeclaration(t.tsInterfaceDeclaration(
121
140
  // TODO
122
141
  t.identifier(context.componentClassName + 'JSX'), null, [], t.tsInterfaceBody([
123
142
  ...context.classProperties.map((property) => Object.assign(t.tSPropertySignature(property.key, property.typeAnnotation), {
@@ -136,7 +155,7 @@ export const customElement = (options) => {
136
155
  });
137
156
  })
138
157
  ]))));
139
- path.node.body.push(Object.assign(t.tsModuleDeclaration(t.identifier('global'), t.tsModuleBlock([
158
+ body.push(Object.assign(t.tsModuleDeclaration(t.identifier('global'), t.tsModuleBlock([
140
159
  t.tsInterfaceDeclaration(t.identifier(context.componentInterfaceName), null, [
141
160
  t.tSExpressionWithTypeArguments(t.identifier('HTMLElement')) // TODO
142
161
  ], t.tsInterfaceBody([
@@ -177,10 +196,8 @@ export const customElement = (options) => {
177
196
  }
178
197
  });
179
198
  }
199
+ // TODO
180
200
  context.script = print(ast);
181
201
  };
182
- return {
183
- name,
184
- next
185
- };
202
+ return { name, next };
186
203
  };
@@ -1,4 +1,4 @@
1
- import { Global } from '../../../types/index.js';
1
+ import { Global } from '../../../types';
2
2
  export interface CustomElementReactOptions {
3
3
  compact?: boolean;
4
4
  destination: string;
@@ -15,8 +15,8 @@ const defaults = {
15
15
  };
16
16
  export const customElementReact = (options) => {
17
17
  const name = 'customElementReact';
18
+ options = Object.assign({}, defaults, options);
18
19
  const finish = (global) => {
19
- options = Object.assign(Object.assign({}, defaults), options);
20
20
  // TODO
21
21
  const globalNew = {
22
22
  contexts: global.contexts.reduce((previous, current) => (Object.assign(Object.assign({}, previous), { [current.filePath]: current })), {}),
@@ -126,8 +126,5 @@ export const customElementReact = (options) => {
126
126
  renderTemplate(patterns, options.destination, config)(globalNew);
127
127
  }
128
128
  };
129
- return {
130
- name,
131
- finish
132
- };
129
+ return { name, finish };
133
130
  };
@@ -1,10 +1,8 @@
1
- import { Context } from '../../types/index.js';
1
+ import { Global } from '../../types';
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
  };