@ayu-sh-kr/dota-preloader-plugin 0.0.1

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/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) [2024] [Ayush Jaiswal]
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,10 @@
1
+ import { Plugin as Plugin_2 } from 'vite';
2
+
3
+ declare function dotaVitePreloader({ root }: PluginConfig): Plugin_2;
4
+ export default dotaVitePreloader;
5
+
6
+ declare type PluginConfig = {
7
+ root?: string;
8
+ };
9
+
10
+ export { }
package/dist/index.js ADDED
@@ -0,0 +1,160 @@
1
+ "use strict";
2
+ const fg = require("fast-glob");
3
+ const promises = require("node:fs/promises");
4
+ const core = require("@swc/core");
5
+ class ComponentScanPath {
6
+ static SOURCE_PAGE_DIRECTORY_SCAN_PATH = "./src/pages/**/*.page.ts";
7
+ static SOURCE_COMPONENT_DIRECTORY_SCAN_PATH = "./src/components/**/*.component.ts";
8
+ static SOURCE_ROOT_DIRECTORY_SCAN_PATH = "./src/**/*.component.ts";
9
+ }
10
+ class ASTFilterConstants {
11
+ static COMPONENT_DECORATOR_NAME = "Component";
12
+ static CLASS_RENDER_METHOD_NAME = "render";
13
+ static COMPONENT_TAG_NAME_PROPERTY = "selector";
14
+ }
15
+ class VirtualImportID {
16
+ static DOTA_COMPONENTS = "virtual:dota-components";
17
+ static RESOLVED_DOTA_COMPONENTS = "\0virtual:dota-components";
18
+ }
19
+ class ASTHelperUtils {
20
+ static getExportDeclarations(body) {
21
+ return body.filter((item) => item.type === "ExportDeclaration");
22
+ }
23
+ static getClassDeclarations(nodes) {
24
+ return nodes.filter((item) => item.declaration.type === "ClassDeclaration").map((item) => item.declaration);
25
+ }
26
+ static getDecorators(classDecl) {
27
+ return classDecl.decorators || [];
28
+ }
29
+ static getClassName(classDecl) {
30
+ if (classDecl.identifier) {
31
+ return classDecl.identifier.value;
32
+ }
33
+ return null;
34
+ }
35
+ static getDecoratorName(decorator) {
36
+ if (decorator.expression.type === "CallExpression") {
37
+ const callee = decorator.expression.callee;
38
+ if (callee.type === "Identifier") {
39
+ return callee.value;
40
+ }
41
+ }
42
+ if (decorator.expression.type === "Identifier") {
43
+ return decorator.expression.value;
44
+ }
45
+ return null;
46
+ }
47
+ static getDecoratorArguments(decorator) {
48
+ if (decorator.expression.type === "CallExpression") {
49
+ return decorator.expression.arguments;
50
+ }
51
+ return [];
52
+ }
53
+ static getKeyValuePropertyFromObject(expression, fieldName) {
54
+ for (const prop of expression.properties) {
55
+ if (prop.type === "KeyValueProperty" && prop.key.type === "Identifier" && prop.key.value === fieldName) {
56
+ return prop;
57
+ }
58
+ }
59
+ return null;
60
+ }
61
+ }
62
+ function extractComponentCandidateFromClassDeclaration(classDecl) {
63
+ const decorators = ASTHelperUtils.getDecorators(classDecl);
64
+ for (const decorator of decorators) {
65
+ const decoratorName = ASTHelperUtils.getDecoratorName(decorator);
66
+ if (decoratorName && decoratorName === ASTFilterConstants.COMPONENT_DECORATOR_NAME) {
67
+ const args = ASTHelperUtils.getDecoratorArguments(decorator);
68
+ if (args.length > 0 && args[0].expression.type === "ObjectExpression") {
69
+ const componentExpression = args[0].expression;
70
+ const componentSelector = ASTHelperUtils.getKeyValuePropertyFromObject(componentExpression, ASTFilterConstants.COMPONENT_TAG_NAME_PROPERTY);
71
+ if (componentSelector.value.type === "StringLiteral") {
72
+ const tagName = componentSelector.value.value;
73
+ const className = ASTHelperUtils.getClassName(classDecl);
74
+ if (className) {
75
+ return {
76
+ name: className,
77
+ filePath: "",
78
+ tagName
79
+ };
80
+ }
81
+ }
82
+ }
83
+ }
84
+ }
85
+ return null;
86
+ }
87
+ function extractComponentCandidateFromAst(ast) {
88
+ const body = ast.body;
89
+ const exportDeclarations = ASTHelperUtils.getExportDeclarations(body);
90
+ const classDeclarations = ASTHelperUtils.getClassDeclarations(exportDeclarations);
91
+ return classDeclarations.map((classDecl) => extractComponentCandidateFromClassDeclaration(classDecl));
92
+ }
93
+ async function scanDotaComponents(root) {
94
+ const files = await fg([
95
+ ComponentScanPath.SOURCE_ROOT_DIRECTORY_SCAN_PATH,
96
+ ComponentScanPath.SOURCE_COMPONENT_DIRECTORY_SCAN_PATH,
97
+ ComponentScanPath.SOURCE_PAGE_DIRECTORY_SCAN_PATH
98
+ ], { cwd: root, absolute: false });
99
+ const candidated = [];
100
+ for (const file of files) {
101
+ const code = await promises.readFile(file, "utf-8");
102
+ let ast;
103
+ try {
104
+ ast = await core.parse(code, { syntax: "typescript", decorators: true });
105
+ } catch (e) {
106
+ console.log(`Failed to parse ${file}: ${e}`);
107
+ continue;
108
+ }
109
+ const candidates = extractComponentCandidateFromAst(ast).map((candidate) => ({ ...candidate, filePath: file }));
110
+ candidated.push(...candidates);
111
+ }
112
+ return candidated;
113
+ }
114
+ async function prepareComponentImports(candidates) {
115
+ const importStatementTemplate = "import { %s } from '%s';";
116
+ return candidates.map((candidate) => {
117
+ const filePath = candidate.filePath;
118
+ const componentName = candidate.name;
119
+ const importPath = filePath.startsWith("./") ? filePath : `./${filePath}`;
120
+ return importStatementTemplate.replace("%s", componentName).replace("%s", importPath);
121
+ }).join("\n");
122
+ }
123
+ async function prepareComponentExports(candidates) {
124
+ const exportStatementTemplate = "export default [%s]";
125
+ const componentNames = candidates.map((candidate) => candidate.name).join(", ");
126
+ return exportStatementTemplate.replace("%s", componentNames);
127
+ }
128
+ async function resolveComponentExport(candidates) {
129
+ const imports = await prepareComponentImports(candidates);
130
+ const exports2 = await prepareComponentExports(candidates);
131
+ return `${imports}
132
+
133
+ ${exports2}`;
134
+ }
135
+ function dotaVitePreloader({ root = process.cwd() }) {
136
+ let cachedCandidates = null;
137
+ async function ensureCandidatesLoaded() {
138
+ if (!cachedCandidates) {
139
+ cachedCandidates = await scanDotaComponents(root);
140
+ }
141
+ return cachedCandidates;
142
+ }
143
+ return {
144
+ name: "vite-plugin-dota-preloader",
145
+ resolveId(id) {
146
+ if (id === VirtualImportID.DOTA_COMPONENTS) return VirtualImportID.RESOLVED_DOTA_COMPONENTS;
147
+ return null;
148
+ },
149
+ async load(id) {
150
+ if (id !== VirtualImportID.RESOLVED_DOTA_COMPONENTS) return null;
151
+ const candidates = await ensureCandidatesLoaded();
152
+ return await resolveComponentExport(candidates);
153
+ },
154
+ async buildStart() {
155
+ cachedCandidates = await scanDotaComponents(root);
156
+ console.log(`Loaded Dota Component Candidates: ${cachedCandidates.length} components found.`);
157
+ }
158
+ };
159
+ }
160
+ module.exports = dotaVitePreloader;
package/dist/index.mjs ADDED
@@ -0,0 +1,161 @@
1
+ import fg from "fast-glob";
2
+ import { readFile } from "node:fs/promises";
3
+ import { parse } from "@swc/core";
4
+ class ComponentScanPath {
5
+ static SOURCE_PAGE_DIRECTORY_SCAN_PATH = "./src/pages/**/*.page.ts";
6
+ static SOURCE_COMPONENT_DIRECTORY_SCAN_PATH = "./src/components/**/*.component.ts";
7
+ static SOURCE_ROOT_DIRECTORY_SCAN_PATH = "./src/**/*.component.ts";
8
+ }
9
+ class ASTFilterConstants {
10
+ static COMPONENT_DECORATOR_NAME = "Component";
11
+ static CLASS_RENDER_METHOD_NAME = "render";
12
+ static COMPONENT_TAG_NAME_PROPERTY = "selector";
13
+ }
14
+ class VirtualImportID {
15
+ static DOTA_COMPONENTS = "virtual:dota-components";
16
+ static RESOLVED_DOTA_COMPONENTS = "\0virtual:dota-components";
17
+ }
18
+ class ASTHelperUtils {
19
+ static getExportDeclarations(body) {
20
+ return body.filter((item) => item.type === "ExportDeclaration");
21
+ }
22
+ static getClassDeclarations(nodes) {
23
+ return nodes.filter((item) => item.declaration.type === "ClassDeclaration").map((item) => item.declaration);
24
+ }
25
+ static getDecorators(classDecl) {
26
+ return classDecl.decorators || [];
27
+ }
28
+ static getClassName(classDecl) {
29
+ if (classDecl.identifier) {
30
+ return classDecl.identifier.value;
31
+ }
32
+ return null;
33
+ }
34
+ static getDecoratorName(decorator) {
35
+ if (decorator.expression.type === "CallExpression") {
36
+ const callee = decorator.expression.callee;
37
+ if (callee.type === "Identifier") {
38
+ return callee.value;
39
+ }
40
+ }
41
+ if (decorator.expression.type === "Identifier") {
42
+ return decorator.expression.value;
43
+ }
44
+ return null;
45
+ }
46
+ static getDecoratorArguments(decorator) {
47
+ if (decorator.expression.type === "CallExpression") {
48
+ return decorator.expression.arguments;
49
+ }
50
+ return [];
51
+ }
52
+ static getKeyValuePropertyFromObject(expression, fieldName) {
53
+ for (const prop of expression.properties) {
54
+ if (prop.type === "KeyValueProperty" && prop.key.type === "Identifier" && prop.key.value === fieldName) {
55
+ return prop;
56
+ }
57
+ }
58
+ return null;
59
+ }
60
+ }
61
+ function extractComponentCandidateFromClassDeclaration(classDecl) {
62
+ const decorators = ASTHelperUtils.getDecorators(classDecl);
63
+ for (const decorator of decorators) {
64
+ const decoratorName = ASTHelperUtils.getDecoratorName(decorator);
65
+ if (decoratorName && decoratorName === ASTFilterConstants.COMPONENT_DECORATOR_NAME) {
66
+ const args = ASTHelperUtils.getDecoratorArguments(decorator);
67
+ if (args.length > 0 && args[0].expression.type === "ObjectExpression") {
68
+ const componentExpression = args[0].expression;
69
+ const componentSelector = ASTHelperUtils.getKeyValuePropertyFromObject(componentExpression, ASTFilterConstants.COMPONENT_TAG_NAME_PROPERTY);
70
+ if (componentSelector.value.type === "StringLiteral") {
71
+ const tagName = componentSelector.value.value;
72
+ const className = ASTHelperUtils.getClassName(classDecl);
73
+ if (className) {
74
+ return {
75
+ name: className,
76
+ filePath: "",
77
+ tagName
78
+ };
79
+ }
80
+ }
81
+ }
82
+ }
83
+ }
84
+ return null;
85
+ }
86
+ function extractComponentCandidateFromAst(ast) {
87
+ const body = ast.body;
88
+ const exportDeclarations = ASTHelperUtils.getExportDeclarations(body);
89
+ const classDeclarations = ASTHelperUtils.getClassDeclarations(exportDeclarations);
90
+ return classDeclarations.map((classDecl) => extractComponentCandidateFromClassDeclaration(classDecl));
91
+ }
92
+ async function scanDotaComponents(root) {
93
+ const files = await fg([
94
+ ComponentScanPath.SOURCE_ROOT_DIRECTORY_SCAN_PATH,
95
+ ComponentScanPath.SOURCE_COMPONENT_DIRECTORY_SCAN_PATH,
96
+ ComponentScanPath.SOURCE_PAGE_DIRECTORY_SCAN_PATH
97
+ ], { cwd: root, absolute: false });
98
+ const candidated = [];
99
+ for (const file of files) {
100
+ const code = await readFile(file, "utf-8");
101
+ let ast;
102
+ try {
103
+ ast = await parse(code, { syntax: "typescript", decorators: true });
104
+ } catch (e) {
105
+ console.log(`Failed to parse ${file}: ${e}`);
106
+ continue;
107
+ }
108
+ const candidates = extractComponentCandidateFromAst(ast).map((candidate) => ({ ...candidate, filePath: file }));
109
+ candidated.push(...candidates);
110
+ }
111
+ return candidated;
112
+ }
113
+ async function prepareComponentImports(candidates) {
114
+ const importStatementTemplate = "import { %s } from '%s';";
115
+ return candidates.map((candidate) => {
116
+ const filePath = candidate.filePath;
117
+ const componentName = candidate.name;
118
+ const importPath = filePath.startsWith("./") ? filePath : `./${filePath}`;
119
+ return importStatementTemplate.replace("%s", componentName).replace("%s", importPath);
120
+ }).join("\n");
121
+ }
122
+ async function prepareComponentExports(candidates) {
123
+ const exportStatementTemplate = "export default [%s]";
124
+ const componentNames = candidates.map((candidate) => candidate.name).join(", ");
125
+ return exportStatementTemplate.replace("%s", componentNames);
126
+ }
127
+ async function resolveComponentExport(candidates) {
128
+ const imports = await prepareComponentImports(candidates);
129
+ const exports = await prepareComponentExports(candidates);
130
+ return `${imports}
131
+
132
+ ${exports}`;
133
+ }
134
+ function dotaVitePreloader({ root = process.cwd() }) {
135
+ let cachedCandidates = null;
136
+ async function ensureCandidatesLoaded() {
137
+ if (!cachedCandidates) {
138
+ cachedCandidates = await scanDotaComponents(root);
139
+ }
140
+ return cachedCandidates;
141
+ }
142
+ return {
143
+ name: "vite-plugin-dota-preloader",
144
+ resolveId(id) {
145
+ if (id === VirtualImportID.DOTA_COMPONENTS) return VirtualImportID.RESOLVED_DOTA_COMPONENTS;
146
+ return null;
147
+ },
148
+ async load(id) {
149
+ if (id !== VirtualImportID.RESOLVED_DOTA_COMPONENTS) return null;
150
+ const candidates = await ensureCandidatesLoaded();
151
+ return await resolveComponentExport(candidates);
152
+ },
153
+ async buildStart() {
154
+ cachedCandidates = await scanDotaComponents(root);
155
+ console.log(`Loaded Dota Component Candidates: ${cachedCandidates.length} components found.`);
156
+ }
157
+ };
158
+ }
159
+ export {
160
+ dotaVitePreloader as default
161
+ };
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@ayu-sh-kr/dota-preloader-plugin",
3
+ "private": false,
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
7
+ "version": "0.0.1",
8
+ "description": "dota-vite-preloader-plugin is a Vite plugin that preloads Web Components for Dota Core Web Apps. It scans the specified directory for Web Component files, generates a preload script, and injects it into the HTML. This ensures that Web Components are loaded and ready to use when the application starts.",
9
+ "author": {
10
+ "name": "ayu-sh-kr",
11
+ "email": "akjaiswal2003@gmail.com",
12
+ "url": "https://linkedin.com/in/ayu-sh-kr"
13
+ },
14
+ "license": "MIT",
15
+ "keywords": [
16
+ "Dota Core Plugins",
17
+ "Web Components",
18
+ "Web Component Loader"
19
+ ],
20
+ "type": "module",
21
+ "types": "./dist/index.d.ts",
22
+ "main": "./dist/index.js",
23
+ "module": "./dist/index.mjs",
24
+ "exports": {
25
+ ".": {
26
+ "types": "./dist/index.d.ts",
27
+ "module": "./dist/index.mjs",
28
+ "import": "./dist/index.mjs",
29
+ "require": "./dist/index.js",
30
+ "default": "./dist/index.js"
31
+ }
32
+ },
33
+ "dependencies": {
34
+ "@swc/core": "^1.13.5",
35
+ "fast-glob": "^3.3.3",
36
+ "magic-string": "^0.30.21"
37
+ },
38
+ "files": [
39
+ "dist/",
40
+ "README.md",
41
+ "package.json"
42
+ ],
43
+ "scripts": {
44
+ "build": "tsc && vite build",
45
+ "release": "pnpm run build && changeset publish"
46
+ }
47
+ }