@htmlplus/element 0.4.5 → 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 (54) 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/assets.d.ts +1 -1
  26. package/compiler/plugins/customElement.d.ts +1 -1
  27. package/compiler/plugins/customElement.js +1 -1
  28. package/compiler/plugins/customElementReact/customElementReact.d.ts +1 -1
  29. package/compiler/plugins/document.d.ts +1 -1
  30. package/compiler/plugins/extract.d.ts +1 -1
  31. package/compiler/plugins/finish.d.ts +4 -0
  32. package/compiler/plugins/finish.js +7 -0
  33. package/compiler/plugins/index.d.ts +2 -0
  34. package/compiler/plugins/index.js +2 -0
  35. package/compiler/plugins/parse.d.ts +1 -1
  36. package/compiler/plugins/read.d.ts +1 -1
  37. package/compiler/plugins/start.d.ts +4 -0
  38. package/compiler/plugins/start.js +7 -0
  39. package/compiler/plugins/style.d.ts +1 -1
  40. package/compiler/plugins/style.js +1 -1
  41. package/compiler/plugins/validate.d.ts +1 -1
  42. package/compiler/plugins/visualStudioCode.d.ts +1 -1
  43. package/compiler/plugins/webTypes.d.ts +1 -1
  44. package/compiler/utils/addDependency.d.ts +3 -1
  45. package/compiler/utils/addDependency.js +25 -15
  46. package/compiler/utils/index.d.ts +1 -0
  47. package/compiler/utils/index.js +1 -0
  48. package/compiler/utils/removeUnusedImport.d.ts +1 -0
  49. package/compiler/utils/removeUnusedImport.js +49 -0
  50. package/package.json +5 -11
  51. package/types/global.d.ts +1 -1
  52. package/types/index.d.ts +4 -4
  53. package/types/index.js +4 -4
  54. package/types/plugin.d.ts +1 -1
@@ -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,4 +1,4 @@
1
- import { Context } from '../../types/index.js';
1
+ import { Context } from '../../types';
2
2
  export declare type AssetsOptions = {
3
3
  destination: (context: Context) => string;
4
4
  source?: (context: Context) => string;
@@ -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
  }
@@ -21,7 +21,7 @@ export const customElement = (options) => {
21
21
  return;
22
22
  if (value.type != 'JSXExpressionContainer')
23
23
  return;
24
- const local = addDependency(ast, CONSTANTS.PACKAGE_NAME, CONSTANTS.UTILS_STYLE_MAP);
24
+ const local = addDependency(path, CONSTANTS.PACKAGE_NAME, CONSTANTS.UTILS_STYLE_MAP, CONSTANTS.UTILS_STYLE_MAP);
25
25
  path.replaceWith(t.jsxAttribute(t.jsxIdentifier('style'), t.jsxExpressionContainer(t.callExpression(t.identifier(local), [value.expression]))));
26
26
  path.skip();
27
27
  }
@@ -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;
@@ -1,4 +1,4 @@
1
- import { Global } from '../../types/index.js';
1
+ import { Global } from '../../types';
2
2
  export interface DocumentOptions {
3
3
  destination: string;
4
4
  }
@@ -1,4 +1,4 @@
1
- import { Context } from '../../types/index.js';
1
+ import { Context } from '../../types';
2
2
  export interface ExtractOptions {
3
3
  prefix?: string;
4
4
  }
@@ -0,0 +1,4 @@
1
+ export declare const finish: (callback: () => void) => {
2
+ name: string;
3
+ finish: () => void;
4
+ };
@@ -0,0 +1,7 @@
1
+ export const finish = (callback) => {
2
+ const name = 'finish';
3
+ const finish = () => {
4
+ callback();
5
+ };
6
+ return { name, finish };
7
+ };
@@ -3,8 +3,10 @@ export * from './customElementReact/index.js';
3
3
  export * from './customElement.js';
4
4
  export * from './document.js';
5
5
  export * from './extract.js';
6
+ export * from './finish.js';
6
7
  export * from './parse.js';
7
8
  export * from './read.js';
9
+ export * from './start.js';
8
10
  export * from './style.js';
9
11
  export * from './validate.js';
10
12
  export * from './webTypes.js';
@@ -3,8 +3,10 @@ export * from './customElementReact/index.js';
3
3
  export * from './customElement.js';
4
4
  export * from './document.js';
5
5
  export * from './extract.js';
6
+ export * from './finish.js';
6
7
  export * from './parse.js';
7
8
  export * from './read.js';
9
+ export * from './start.js';
8
10
  export * from './style.js';
9
11
  export * from './validate.js';
10
12
  export * from './webTypes.js';
@@ -1,4 +1,4 @@
1
- import { Context } from '../../types/index.js';
1
+ import { Context } from '../../types';
2
2
  export declare const parse: () => {
3
3
  name: string;
4
4
  next: (context: Context) => void;
@@ -1,4 +1,4 @@
1
- import { Context } from '../../types/index.js';
1
+ import { Context } from '../../types';
2
2
  export declare const read: () => {
3
3
  name: string;
4
4
  next: (context: Context) => void;
@@ -0,0 +1,4 @@
1
+ export declare const start: (callback: () => void) => {
2
+ name: string;
3
+ start: () => void;
4
+ };
@@ -0,0 +1,7 @@
1
+ export const start = (callback) => {
2
+ const name = 'start';
3
+ const start = () => {
4
+ callback();
5
+ };
6
+ return { name, start };
7
+ };
@@ -1,4 +1,4 @@
1
- import { Context } from '../../types/index.js';
1
+ import { Context } from '../../types';
2
2
  export declare type StyleOptions = {
3
3
  references?: (context: Context) => string[];
4
4
  };
@@ -26,7 +26,7 @@ export const style = (options) => {
26
26
  }
27
27
  if (!context.stylePath)
28
28
  return;
29
- const local = addDependency(context.fileAST, context.stylePath, 'AUTO_IMPORT_STYLE', undefined, true);
29
+ const local = addDependency(context.fileAST, context.stylePath, 'AUTO_IMPORT_STYLE');
30
30
  const property = t.classProperty(t.identifier(CONSTANTS.STATIC_STYLES), t.identifier(local), undefined, null, undefined, true);
31
31
  context.class.body.body.unshift(property);
32
32
  };
@@ -1,4 +1,4 @@
1
- import { Context } from '../../types/index.js';
1
+ import { Context } from '../../types';
2
2
  export declare const validate: () => {
3
3
  name: string;
4
4
  next: (context: Context) => void;
@@ -1,4 +1,4 @@
1
- import { Global } from '../../types/index.js';
1
+ import { Global } from '../../types';
2
2
  export interface VisualStudioCodeOptions {
3
3
  destination: string;
4
4
  }
@@ -1,4 +1,4 @@
1
- import { Global } from '../../types/index.js';
1
+ import { Global } from '../../types';
2
2
  export interface WebTypesOptions {
3
3
  destination: string;
4
4
  packageName: string;
@@ -1,2 +1,4 @@
1
1
  import { File } from '@babel/types';
2
- export declare const addDependency: (file: File, source: string, imported: string, local?: string, isDefault?: boolean) => string;
2
+ export declare function addDependency(path: File | any, source: string): void;
3
+ export declare function addDependency(path: File | any, source: string, local: string): string;
4
+ export declare function addDependency(path: File | any, source: string, local: string, imported: string): string;
@@ -1,41 +1,51 @@
1
1
  import t from '@babel/types';
2
2
  import { visitor } from './visitor.js';
3
- export const addDependency = (file, source, imported, local, isDefault) => {
4
- let node;
3
+ export function addDependency(path, source, local, imported) {
4
+ const isDefault = local && !imported;
5
+ const isImport = local && imported;
6
+ const isNormal = !local && !imported;
7
+ let declaration;
8
+ let file = path;
9
+ while (file.parentPath)
10
+ file = file.parentPath;
11
+ file = file.node || file;
5
12
  visitor(file, {
6
13
  ImportDeclaration(path) {
7
14
  if (path.node.source.value != source)
8
15
  return;
9
- node = path.node;
16
+ declaration = path.node;
10
17
  }
11
18
  });
12
- let specifier = node === null || node === void 0 ? void 0 : node.specifiers.find((specifier) => {
19
+ if (isNormal && declaration)
20
+ return;
21
+ let specifier = declaration === null || declaration === void 0 ? void 0 : declaration.specifiers.find((specifier) => {
13
22
  var _a;
14
23
  if (isDefault) {
15
24
  return specifier.type == 'ImportDefaultSpecifier';
16
25
  }
17
- else {
26
+ else if (isImport) {
18
27
  return ((_a = specifier.imported) === null || _a === void 0 ? void 0 : _a.name) == imported;
19
28
  }
20
29
  });
21
30
  if (specifier)
22
31
  return specifier.local.name;
23
32
  if (isDefault) {
24
- specifier = t.importDefaultSpecifier(t.identifier(imported));
33
+ specifier = t.importDefaultSpecifier(t.identifier(local));
25
34
  }
26
- else {
27
- specifier = t.importSpecifier(t.identifier(local !== null && local !== void 0 ? local : imported), t.identifier(imported));
35
+ else if (isImport) {
36
+ specifier = t.importSpecifier(t.identifier(local), t.identifier(imported));
28
37
  }
29
- if (node) {
38
+ if (declaration) {
30
39
  if (isDefault) {
31
- node.specifiers.unshift(specifier);
40
+ declaration.specifiers.unshift(specifier);
32
41
  }
33
- else {
34
- node.specifiers.push(specifier);
42
+ else if (isImport) {
43
+ declaration.specifiers.push(specifier);
35
44
  }
36
45
  }
37
46
  else {
38
- file.program.body.unshift(t.importDeclaration([specifier], t.stringLiteral(source)));
47
+ // TODO
48
+ (file.program || file).body.unshift(t.importDeclaration(specifier ? [specifier] : [], t.stringLiteral(source)));
39
49
  }
40
- return isDefault ? imported : local !== null && local !== void 0 ? local : imported;
41
- };
50
+ return local;
51
+ }
@@ -7,6 +7,7 @@ export * from './hasDecorator.js';
7
7
  export * from './isDirectoryEmpty.js';
8
8
  export * from './printType.js';
9
9
  export * from './print.js';
10
+ export * from './removeUnusedImport.js';
10
11
  export * from './renderTemplate.js';
11
12
  export * from './tags.js';
12
13
  export * from './visitor.js';
@@ -7,6 +7,7 @@ export * from './hasDecorator.js';
7
7
  export * from './isDirectoryEmpty.js';
8
8
  export * from './printType.js';
9
9
  export * from './print.js';
10
+ export * from './removeUnusedImport.js';
10
11
  export * from './renderTemplate.js';
11
12
  export * from './tags.js';
12
13
  export * from './visitor.js';
@@ -0,0 +1 @@
1
+ export declare const removeUnusedImport: (ast: any) => void;
@@ -0,0 +1,49 @@
1
+ import t from '@babel/types';
2
+ import { visitor } from './visitor.js';
3
+ // TODO
4
+ export const removeUnusedImport = (ast) => {
5
+ visitor(ast, {
6
+ Program: {
7
+ exit(path) {
8
+ for (const entry of Object.entries(path.scope.bindings)) {
9
+ let { kind, path, referenced, referencePaths } = entry[1];
10
+ if (kind !== 'module')
11
+ continue;
12
+ if (referenced) {
13
+ referenced = false;
14
+ for (const referencePath of referencePaths) {
15
+ let find = true;
16
+ let parent = referencePath;
17
+ while (parent) {
18
+ if (!parent.node) {
19
+ find = false;
20
+ break;
21
+ }
22
+ parent = parent.parentPath;
23
+ }
24
+ if (!find)
25
+ continue;
26
+ referenced = true;
27
+ break;
28
+ }
29
+ }
30
+ if (referenced)
31
+ continue;
32
+ const source = path.parentPath.get('source');
33
+ if (!t.isStringLiteral(source))
34
+ continue;
35
+ if (path.isImportSpecifier()) {
36
+ path.remove();
37
+ if (path.parentPath.node.specifiers.length)
38
+ continue;
39
+ path.parentPath.remove();
40
+ continue;
41
+ }
42
+ if (!path.parentPath)
43
+ continue;
44
+ path.parentPath.remove();
45
+ }
46
+ }
47
+ }
48
+ });
49
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@htmlplus/element",
3
- "version": "0.4.5",
3
+ "version": "0.4.7",
4
4
  "license": "MIT",
5
5
  "author": "Masood Abdolian <m.abdolian@gmail.com>",
6
6
  "description": "Compiler of HTMLPlus",
@@ -8,13 +8,7 @@
8
8
  "main": "client/index.js",
9
9
  "types": "client/index.d.ts",
10
10
  "files": [
11
- "client",
12
- "compiler",
13
- "constants",
14
- "runtime",
15
- "types",
16
- "package.json",
17
- "README.md"
11
+ "**/*.*"
18
12
  ],
19
13
  "publishConfig": {
20
14
  "directory": "dist"
@@ -25,12 +19,12 @@
25
19
  "keywords": [],
26
20
  "repository": {
27
21
  "type": "git",
28
- "url": "git+https://github.com/htmlplus/element.git"
22
+ "url": "git+https://github.com/htmlplus/htmlplus.git"
29
23
  },
30
24
  "bugs": {
31
- "url": "https://github.com/htmlplus/element/issues"
25
+ "url": "https://github.com/htmlplus/htmlplus/issues"
32
26
  },
33
- "homepage": "https://github.com/htmlplus/element#readme",
27
+ "homepage": "https://github.com/htmlplus/htmlplus#readme",
34
28
  "dependencies": {
35
29
  "@babel/generator": "^7.18.9",
36
30
  "@babel/parser": "^7.18.9",
package/types/global.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Context } from './context.js';
1
+ import { Context } from './context';
2
2
  export interface Global {
3
3
  contexts: Array<Context>;
4
4
  }
package/types/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export * from './context.js';
2
- export * from './global.js';
3
- export * from './plugin.js';
4
- export * from './plusElement.js';
1
+ export * from './context';
2
+ export * from './global';
3
+ export * from './plugin';
4
+ export * from './plusElement';
package/types/index.js CHANGED
@@ -1,4 +1,4 @@
1
- export * from './context.js';
2
- export * from './global.js';
3
- export * from './plugin.js';
4
- export * from './plusElement.js';
1
+ export * from './context';
2
+ export * from './global';
3
+ export * from './plugin';
4
+ export * from './plusElement';
package/types/plugin.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Context } from './context.js';
1
+ import { Context } from './context';
2
2
  import { Global } from './global';
3
3
  export declare type Return<T> = void | T | Promise<T>;
4
4
  export declare type Plugin = {