@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
@@ -1,36 +1,34 @@
1
1
  import t from '@babel/types';
2
- import fs from 'fs';
2
+ import fs from 'fs-extra';
3
3
  import path from 'path';
4
4
  import * as CONSTANTS from '../../constants/index.js';
5
+ import { addDependency } from '../utils/index.js';
5
6
  const defaults = {
6
- extensions: ['scss', 'css'],
7
- directory(context) {
8
- return context.directoryPath;
9
- },
10
- filename(context) {
11
- return context.fileName;
7
+ references(context) {
8
+ return [
9
+ path.join(context.directoryPath, `${context.fileName}.css`),
10
+ path.join(context.directoryPath, `${context.fileName}.less`),
11
+ path.join(context.directoryPath, `${context.fileName}.sass`),
12
+ path.join(context.directoryPath, `${context.fileName}.scss`),
13
+ path.join(context.directoryPath, `${context.fileName}.styl`)
14
+ ];
12
15
  }
13
16
  };
14
17
  export const style = (options) => {
15
18
  const name = 'style';
16
- options = Object.assign(Object.assign({}, defaults), options);
19
+ options = Object.assign({}, defaults, options);
17
20
  const next = (context) => {
18
- const filename = options.filename(context);
19
- const directory = options.directory(context);
20
- for (let extension of options.extensions) {
21
- const stylePath = path.join(directory, `${filename}.${extension}`);
22
- if (!fs.existsSync(stylePath))
21
+ const references = options.references(context);
22
+ for (const reference of references) {
23
+ if (!fs.existsSync(reference))
23
24
  continue;
24
- context.stylePath = stylePath;
25
- break;
25
+ context.stylePath = reference;
26
26
  }
27
27
  if (!context.stylePath)
28
28
  return;
29
- context.fileAST.program.body.unshift(t.importDeclaration([t.importDefaultSpecifier(t.identifier('AUTO_IMPORT_STYLE'))], t.stringLiteral(context.stylePath)));
30
- context.class.body.body.unshift(t.classProperty(t.identifier(CONSTANTS.STATIC_STYLES), t.identifier('AUTO_IMPORT_STYLE'), undefined, null, undefined, true));
31
- };
32
- return {
33
- name,
34
- next
29
+ const local = addDependency(context.fileAST, context.stylePath, 'AUTO_IMPORT_STYLE');
30
+ const property = t.classProperty(t.identifier(CONSTANTS.STATIC_STYLES), t.identifier(local), undefined, null, undefined, true);
31
+ context.class.body.body.unshift(property);
35
32
  };
33
+ return { name, next };
36
34
  };
@@ -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;
@@ -33,8 +33,5 @@ export const validate = () => {
33
33
  });
34
34
  context.isInvalid = !hasValidImport || !hasValidExport;
35
35
  };
36
- return {
37
- name,
38
- next
39
- };
36
+ return { name, next };
40
37
  };
@@ -0,0 +1,8 @@
1
+ import { Global } from '../../types';
2
+ export interface VisualStudioCodeOptions {
3
+ destination: string;
4
+ }
5
+ export declare const visualStudioCode: (options: VisualStudioCodeOptions) => {
6
+ name: string;
7
+ finish: (global: Global) => void;
8
+ };
@@ -0,0 +1,28 @@
1
+ const defaults = {};
2
+ export const visualStudioCode = (options) => {
3
+ const name = 'visualStudioCode';
4
+ options = Object.assign({}, defaults, options);
5
+ const finish = (global) => {
6
+ // TODO
7
+ // {
8
+ // version: 1.1,
9
+ // tags: [
10
+ // {
11
+ // name: context.componentKey,
12
+ // description: {
13
+ // kind: 'markdown',
14
+ // value: description
15
+ // },
16
+ // attributes: properties,
17
+ // references: [
18
+ // {
19
+ // name: 'Source code',
20
+ // url: `https://github.com/htmlplus/core/tree/main/src/components/${context.directoryName}/${context.fileName}.tsx`
21
+ // }
22
+ // ]
23
+ // }
24
+ // ]
25
+ // };
26
+ };
27
+ return { name, finish };
28
+ };
@@ -1,9 +1,9 @@
1
- import { Global } from '../../types/index.js';
1
+ import { Global } from '../../types';
2
2
  export interface WebTypesOptions {
3
3
  destination: string;
4
- docUrl: () => string;
5
4
  packageName: string;
6
5
  packageVersion: string;
6
+ docUrl: () => string;
7
7
  }
8
8
  export declare const webTypes: (options: WebTypesOptions) => {
9
9
  name: string;
@@ -1,9 +1,11 @@
1
1
  import { camelCase, paramCase } from 'change-case';
2
- import fs from 'fs';
2
+ import fs from 'fs-extra';
3
3
  import path from 'path';
4
4
  import { getTags, print } from '../utils/index.js';
5
+ const defaults = {};
5
6
  export const webTypes = (options) => {
6
7
  const name = 'webTypes';
8
+ options = Object.assign({}, defaults, options);
7
9
  const finish = (global) => {
8
10
  const json = {
9
11
  '$schema': 'http://json.schemastore.org/web-types',
@@ -70,13 +72,8 @@ export const webTypes = (options) => {
70
72
  }
71
73
  };
72
74
  const dirname = path.dirname(options.destination);
73
- if (!fs.existsSync(dirname))
74
- fs.mkdirSync(dirname, { recursive: true });
75
- const raw = JSON.stringify(json, null, 2);
76
- fs.writeFileSync(options.destination, raw, 'utf8');
77
- };
78
- return {
79
- name,
80
- finish
75
+ fs.ensureDirSync(dirname);
76
+ fs.writeJSONSync(options.destination, json, { encoding: 'utf8', spaces: 2 });
81
77
  };
78
+ return { name, finish };
82
79
  };
@@ -0,0 +1,4 @@
1
+ import { File } from '@babel/types';
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;
@@ -0,0 +1,51 @@
1
+ import t from '@babel/types';
2
+ import { visitor } from './visitor.js';
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;
12
+ visitor(file, {
13
+ ImportDeclaration(path) {
14
+ if (path.node.source.value != source)
15
+ return;
16
+ declaration = path.node;
17
+ }
18
+ });
19
+ if (isNormal && declaration)
20
+ return;
21
+ let specifier = declaration === null || declaration === void 0 ? void 0 : declaration.specifiers.find((specifier) => {
22
+ var _a;
23
+ if (isDefault) {
24
+ return specifier.type == 'ImportDefaultSpecifier';
25
+ }
26
+ else if (isImport) {
27
+ return ((_a = specifier.imported) === null || _a === void 0 ? void 0 : _a.name) == imported;
28
+ }
29
+ });
30
+ if (specifier)
31
+ return specifier.local.name;
32
+ if (isDefault) {
33
+ specifier = t.importDefaultSpecifier(t.identifier(local));
34
+ }
35
+ else if (isImport) {
36
+ specifier = t.importSpecifier(t.identifier(local), t.identifier(imported));
37
+ }
38
+ if (declaration) {
39
+ if (isDefault) {
40
+ declaration.specifiers.unshift(specifier);
41
+ }
42
+ else if (isImport) {
43
+ declaration.specifiers.push(specifier);
44
+ }
45
+ }
46
+ else {
47
+ // TODO
48
+ (file.program || file).body.unshift(t.importDeclaration(specifier ? [specifier] : [], t.stringLiteral(source)));
49
+ }
50
+ return local;
51
+ }
@@ -1,5 +1,5 @@
1
1
  import { parse } from '@babel/parser';
2
- import fs from 'fs';
2
+ import fs from 'fs-extra';
3
3
  import { dirname, resolve } from 'path';
4
4
  import { visitor } from './visitor.js';
5
5
  // TODO: return type
@@ -1,4 +1,5 @@
1
1
  export * from './__dirname.js';
2
+ export * from './addDependency.js';
2
3
  export * from './getInitializer.js';
3
4
  export * from './getType.js';
4
5
  export * from './getTypeReference.js';
@@ -6,6 +7,7 @@ export * from './hasDecorator.js';
6
7
  export * from './isDirectoryEmpty.js';
7
8
  export * from './printType.js';
8
9
  export * from './print.js';
10
+ export * from './removeUnusedImport.js';
9
11
  export * from './renderTemplate.js';
10
12
  export * from './tags.js';
11
13
  export * from './visitor.js';
@@ -1,4 +1,5 @@
1
1
  export * from './__dirname.js';
2
+ export * from './addDependency.js';
2
3
  export * from './getInitializer.js';
3
4
  export * from './getType.js';
4
5
  export * from './getTypeReference.js';
@@ -6,6 +7,7 @@ export * from './hasDecorator.js';
6
7
  export * from './isDirectoryEmpty.js';
7
8
  export * from './printType.js';
8
9
  export * from './print.js';
10
+ export * from './removeUnusedImport.js';
9
11
  export * from './renderTemplate.js';
10
12
  export * from './tags.js';
11
13
  export * from './visitor.js';
@@ -1,4 +1,4 @@
1
- import fs from 'fs';
1
+ import fs from 'fs-extra';
2
2
  export const isDirectoryEmpty = (directory) => {
3
3
  try {
4
4
  const files = fs.readdirSync(directory);
@@ -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
+ };
@@ -1,5 +1,5 @@
1
1
  import glob from 'fast-glob';
2
- import fs from 'fs';
2
+ import fs from 'fs-extra';
3
3
  import handlebars from 'handlebars';
4
4
  import path from 'path';
5
5
  export const renderTemplate = (source, destination, options) => (context) => {
@@ -17,9 +17,7 @@ export const renderTemplate = (source, destination, options) => (context) => {
17
17
  const directory = path.dirname(to);
18
18
  const raw = fs.readFileSync(from, 'utf8');
19
19
  const template = handlebars.compile(raw)(context);
20
- if (!fs.existsSync(directory)) {
21
- fs.mkdirSync(directory, { recursive: true });
22
- }
20
+ fs.ensureDirSync(directory);
23
21
  fs.writeFileSync(to, template, 'utf8');
24
22
  }
25
23
  };
@@ -20,3 +20,4 @@ export declare const TYPE_DATE = "date";
20
20
  export declare const TYPE_FUNCTION = "function";
21
21
  export declare const TYPE_STRING = "string";
22
22
  export declare const TYPE_NUMBER = "number";
23
+ export declare const UTILS_STYLE_MAP = "styles";
@@ -27,3 +27,5 @@ export const TYPE_DATE = 'date';
27
27
  export const TYPE_FUNCTION = 'function';
28
28
  export const TYPE_STRING = 'string';
29
29
  export const TYPE_NUMBER = 'number';
30
+ // utils
31
+ export const UTILS_STYLE_MAP = 'styles';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@htmlplus/element",
3
- "version": "0.4.4",
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",
@@ -41,7 +35,6 @@
41
35
  "fast-glob": "^3.2.11",
42
36
  "fs-extra": "^10.1.0",
43
37
  "handlebars": "^4.7.7",
44
- "listr2": "^5.0.1",
45
38
  "ora": "^6.1.2",
46
39
  "typescript": "^4.7.4"
47
40
  },
@@ -1,5 +1,6 @@
1
1
  import { ClassBody, ClassDeclaration, ClassMethod, ClassProperty, File } from '@babel/types';
2
2
  export interface Context {
3
+ assets?: string;
3
4
  customElementNames?: Array<string>;
4
5
  dependencies?: Array<Context>;
5
6
  dependenciesUnresolved?: Array<string>;
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 = {
@@ -1,25 +0,0 @@
1
- import fs from 'fs-extra';
2
- import path from 'path';
3
- const defaults = {
4
- source(context) {
5
- return path.join(context.directoryPath, 'external');
6
- },
7
- destination(context) {
8
- return '';
9
- }
10
- };
11
- export const external = (options) => {
12
- const name = 'external';
13
- options = Object.assign(Object.assign({}, defaults), options);
14
- const next = (context) => {
15
- var _a;
16
- const source = (_a = options.source) === null || _a === void 0 ? void 0 : _a.call(options, context);
17
- if (!source || !fs.existsSync(source))
18
- return;
19
- fs.copySync(source, options.destination(context));
20
- };
21
- return {
22
- name,
23
- next
24
- };
25
- };
@@ -1,11 +0,0 @@
1
- import { Context } from '../../types/index.js';
2
- export interface VscodeOptions {
3
- dist: string;
4
- prefix: string;
5
- }
6
- export declare const vscode: (options: VscodeOptions) => {
7
- name: string;
8
- start: (global: any) => void;
9
- next: (context: Context, global: any) => void;
10
- finish: (global: any) => void;
11
- };
@@ -1,83 +0,0 @@
1
- import { paramCase } from 'change-case';
2
- import fs from 'fs';
3
- import path from 'path';
4
- import { getTags, getType, printType } from '../utils/index.js';
5
- export const vscode = (options) => {
6
- const name = 'vscode';
7
- const start = (global) => {
8
- global.vscode = {
9
- version: 1.1,
10
- tags: []
11
- };
12
- };
13
- const next = (context, global) => {
14
- const readme = (() => {
15
- try {
16
- const source = path.resolve(context.directoryPath || '', `${context.fileName}.md`);
17
- return fs.readFileSync(source, 'utf8');
18
- }
19
- catch (_a) { }
20
- })();
21
- const description = (() => {
22
- const content = readme || '';
23
- if (!content.startsWith('# '))
24
- return '';
25
- const sections = content.split('\n');
26
- for (let i = 1; i < sections.length; i++) {
27
- const section = sections[i].trim();
28
- if (!section)
29
- continue;
30
- return section;
31
- }
32
- return '';
33
- })();
34
- const properties = (context.classProperties || []).map((property) => {
35
- var _a;
36
- const name = paramCase(property.key['name']);
37
- const description = (_a = getTags(property).find((tag) => !tag.key)) === null || _a === void 0 ? void 0 : _a.value;
38
- const attribute = {
39
- name,
40
- description
41
- };
42
- // TODO
43
- let { members = [] } = (() => {
44
- const ast = getType(context.fileAST, (property.typeAnnotation || {})['typeAnnotation'], {
45
- directory: context.directoryPath
46
- });
47
- return printType(ast);
48
- })();
49
- // TODO
50
- members = members.filter((member) => member.key !== member.type).map((member) => ({ name: member.key }));
51
- if (members.length)
52
- attribute.values = members;
53
- return attribute;
54
- });
55
- global.vscode.tags.push({
56
- name: context.componentKey,
57
- description: {
58
- kind: 'markdown',
59
- value: description
60
- },
61
- attributes: properties,
62
- references: [
63
- {
64
- name: 'Source code',
65
- url: `https://github.com/htmlplus/core/tree/main/src/components/${context.directoryName}/${context.fileName}.tsx`
66
- }
67
- ]
68
- });
69
- };
70
- const finish = (global) => {
71
- global.vscode.tags = global.vscode.tags.sort((a, b) => (a.name > b.name ? 1 : -1));
72
- // TODO
73
- // fs.ensureDirSync(path.dirname(options.dist));
74
- // TODO
75
- // fs.writeJSONSync(options.dist, global.vscode, { replacer: null, spaces: 2 });
76
- };
77
- return {
78
- name,
79
- start,
80
- next,
81
- finish
82
- };
83
- };